-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
87 lines (69 loc) · 2.31 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package main
import (
"bufio"
"fmt"
"math/rand"
"os"
"strings"
"time"
)
func main() {
var participants []string
scanner := bufio.NewScanner(os.Stdin)
var teamSize int
fmt.Println("Enter the desired team size (minimum 2):")
_, err := fmt.Scanf("%d", &teamSize)
if err != nil || teamSize < 2 {
fmt.Println("Invalid input for team size. Please enter an integer greater than 1.")
return
}
fmt.Println("Enter participant names (press Enter without typing a name to finish):")
for {
scanner.Scan()
name := scanner.Text()
name = strings.TrimSpace(name)
if name == "" {
break
}
if len(name) == 0 {
fmt.Println("Invalid name entered. Please enter a non-empty name.")
continue
}
participants = append(participants, name)
}
if len(participants) < 3 {
fmt.Println("Not enough participants. Please enter at least 3 names.")
return
}
randGen := rand.New(rand.NewSource(time.Now().UnixNano()))
randGen.Shuffle(len(participants), func(i, j int) {
participants[i], participants[j] = participants[j], participants[i]
})
numTeams := len(participants) / teamSize
remainder := len(participants) % teamSize
fmt.Printf("\nTotal Participants: %d\n", len(participants))
fmt.Printf("Forming %d teams of %d members each.\n", numTeams, teamSize)
for i := 0; i < numTeams; i++ {
team := participants[i*teamSize : (i+1)*teamSize]
fmt.Printf("Team %d: %v\n", i+1, team)
}
if remainder > 0 {
remainingTeam := participants[numTeams*teamSize:]
fmt.Printf("Remaining Participants: %v\n", remainingTeam)
fmt.Println("You can choose to redistribute the remaining members or create a smaller team.")
redistributeParticipants(&participants, numTeams, remainder)
}
if len(participants)%teamSize != 0 {
fmt.Println("\nNote: The last team might have fewer participants than the others.")
}
}
func redistributeParticipants(participants *[]string, numTeams, remainder int) {
if remainder > 0 {
fmt.Println("\nRedistributing remaining participants into teams...")
for i := 0; i < remainder; i++ {
teamIndex := rand.Intn(numTeams)
*participants = append((*participants)[:teamIndex*len(*participants)/numTeams], append([]string{(*participants)[numTeams*len(*participants)/numTeams]}, (*participants)[teamIndex*len(*participants)/numTeams:]...)...)
}
fmt.Println("Redistribution completed.")
}
}