Skip to content

Instantly share code, notes, and snippets.

@kremerben
Last active October 9, 2019 02:56
Show Gist options
  • Save kremerben/afa2225b024276fc027d2d60e8dc2d5a to your computer and use it in GitHub Desktop.
Save kremerben/afa2225b024276fc027d2d60e8dc2d5a to your computer and use it in GitHub Desktop.
Pick a group of 3-5 people for lunch
import random
people = ['alice', 'bob', 'carl', 'dave', 'asdf', 'sdfg', 'sdffg', 'erty', 'ertry']
people = [1,2,3,4,5,6,7,8,9]
def make_groups(people):
result_groups = []
current_group = []
# while I have people to choose from
# remove a person from the list
# add them to temp list
# if 4 people in templist add to master list
# if less than 4 people left add them all
while len(people):
# choice = random.choice(people)
choice = people[0]
print('>>',people)
print(choice)
print(current_group)
print(result_groups)
if len(current_group) < 4 and len(current_group) + len(people) <= 4:
current_group.extend(people)
result_groups.append(current_group)
current_group = []
break
elif len(current_group) < 4:
current_group.append(choice)
else:
result_groups.append(current_group)
# result_groups.append([choice])
current_group = [c]
people.remove(choice)
choice = ""
# result_groups[-1].append(choice)
return result_groups
make_groups(people)
#################### Working solution.
import random
people = ['alice', 'bob', 'carl', 'dave', 'earl', 'fred', 'grant', 'henry', 'irving']
# people = [1,2,3,4,5,6,7,8,9]
def make_groups_correct(people):
result_groups = []
current_group = []
# while I have people to choose from
# remove a person from the list
# add them to temp list
# if 4 people in templist add to master list
# if less than 4 people left add them all
while len(people):
choice = random.choice(people)
current_group.append(choice)
people.remove(choice)
if len(current_group) == 4:
result_groups.append(current_group)
current_group = []
result_groups.append(current_group)
return result_groups
make_groups_correct(people)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment