# List Functions

Apr 25th, 2021
51
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. from random import choice
2. from random import randint
3. from random import shuffle
4. from random import sample
5.
6. def randomSelection(players,teamA,teamB):
7.     currentTeam = 'A'
8.     listLength = len(players)
9.     listNotEmpty = (listLength > 0)
10.     while listNotEmpty:
11.         player = choice(players)
12.         if currentTeam == 'A':
13.             teamA.append(player)
14.             currentTeam = 'B'
15.         else:
16.             teamB.append(player)
17.             currentTeam = 'A'
18.
19.         players.remove(player)
20.         listLength = len(players)
21.         listNotEmpty = listLength > 0
22.     return teamA,teamB
23.
24. def RandomSelectionMultiple(players,teams):
25.     currentTeam = 0
26.     listLength = len(players)
27.     listNotEmpty = (listLength > 0)
28.     while listNotEmpty:
29.         player = choice(players)
30.         teams[currentTeam].append(player)
31.         if currentTeam == len(teams) - 1:
32.             currentTeam = 0
33.         else:
34.             currentTeam = currentTeam + 1
35.         players.remove(player)
36.         listLength = len(players)
37.         listNotEmpty = listLength > 0
38.     return teams
39.
40. def shufflePlayers(players,teamA,teamB):
41.     shuffle(players)
42.     currentTeam = 'A'
43.     listNotEmpty = len(players) > 0
44.     while listNotEmpty:
45.         player = players.pop()
46.         if currentTeam == 'A':
47.             teamA.append(player)
48.             currentTeam = 'B'
49.         else:
50.             teamB.append(player)
51.             currentTeam = 'A'
52.
53.         listNotEmpty = len(players) > 0
54.     return teamA,teamB
55.
56. def ShufflePlayersMultiple(players,teams):
57.     #shuffle players list
58.     #for each player remove from players add to current teamlist
59.     #move to next team in teamlist
60.     shuffle(players)
61.     currentTeam = 0
62.     listNotEmpty = len(players) > 0
63.     while listNotEmpty:
64.         player = players.pop()
65.         teams[currentTeam].append(player)
66.         if currentTeam == len(teams) - 1:
67.             currentTeam = 0
68.         else:
69.             currentTeam = currentTeam + 1
70.         listNotEmpty = len(players) > 0
71.     return teams
72.
73. def iteratePlayers(players,teamA,teamB):
74.     for player in players:
75.         if randint(0,1) == 0:
76.             teamA.append(player)
77.         else:
78.             teamB.append(player)
79.     return teamA,teamB
80.
81. def IteratePlayersMultiple(players,teams):
82.     #itereate through players
83.     #randomly select a team
85.     for player in players:
86.         team  = randint(0,len(teams)-1)
87.         teams[team].append(player)
88.     return teams
89.
90. def samplePlayers(players,teamA,teamB):
91.     listLength = int(len(players) / 2)
92.     teamA = sample(players,listLength)
93.     for player in players:
94.         if player not in teamA:
95.             teamB.append(player)
96.     return teamA, teamB
97.
98. def SamplePlayersMultiple(players,teams):
99.     #for each team
100.     #take a random sample from players list and adds to team
101.     #remove sampled players from players list
102.     listLength = int(len(players)/len(teams))
103.     for team in range(len(teams)):
104.         teams[team] = sample(players,listLength)
105.         for player in teams[team]:
106.             players.remove(player)
107.     return teams
108.
109. def CreateEmptyLists(teams,iTeams):
110.     for team in range(iTeams):
111.         teams.append([])
112.     return teams
113.
114.
115. #initialise lists
116. players = ['Alpha','Bravo','Charlie','Delta','Echo','Foxtrot','Golf','Hotel','Indigo','Juliet','Kilo','Lima']
117. teamA = []
118. teamB = []
119.
120. #TWO TEAMS
121. #teamA,teamB = randomSelection(players,teamA,teamB)
122. #teamA,teamB = shufflePlayers(players,teamA,teamB)
123. #teamA,teamB = iteratePlayers(players,teamA,teamB)
124. #teamA, teamB = samplePlayers(players,teamA,teamB)
125.
126. #print("Team A: {}".format(teamA))
127. #print("Team B: {}".format(teamB))
128.
129.
130. #MULTIPLE TEAMS
131. iTeams = int(input("Enter number of teams: "))
132. teams = []
133. if iTeams > 0:
134.     teams = CreateEmptyLists(teams,iTeams)
135.     teams = RandomSelectionMultiple(players,teams)
136.     #teams = ShufflePlayersMultiple(players,teams)
137.     #teams = IteratePlayersMultiple(players,teams)
138.     #teams = SamplePlayersMultiple(players,teams)
139. print("Teams : {}".format(teams))
140.