-
Notifications
You must be signed in to change notification settings - Fork 2
/
random_groups.py
114 lines (98 loc) · 3.85 KB
/
random_groups.py
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import random
import os
import copy
import json
from sys import argv
class RandomGroups:
# Comment
def __init__(self, number_per_group=2):
self.number_per_group = number_per_group
self.adjectives = []
self.nouns = []
self.groups = []
self.students = []
self.run_setup_functions()
def run_setup_functions(self):
""" Functions to be run during initilization """
self.set_adjectives()
self.set_nouns()
self.set_students()
random.shuffle(self.students)
self.student_list_copy = copy.deepcopy(self.students)
self.set_group()
def set_adjectives(self):
with open('adjectives.txt') as inputAdjectives:
self.adjectives = inputAdjectives
""" Strip Off Newlines"""
self.adjectives = [line.rstrip('\n').split(',') for line in self.adjectives]
""" Flatten List """
self.adjectives = [item for sublist in self.adjectives for item in sublist ]
def set_nouns(self):
with open('nouns.txt') as inputNouns:
self.nouns = inputNouns
""" Strip Off Newlines"""
self.nouns = [line.rstrip('\n').split(',') for line in self.nouns]
""" Flatten List """
self.nouns = [item for sublist in self.nouns for item in sublist ]
def set_students(self):
with open('students.json', 'r') as f:
self.students = json.load(f)
def set_group(self):
""" Main function to gather the groups """
while len(self.student_list_copy) > 0:
if len(self.student_list_copy) >= self.number_per_group:
choices = self.random_add_to_group()
elif len(self.student_list_copy) is 1:
choices = self.add_one_to_group()
else:
choices = self.add_remaining_to_groups()
self.remove(choices)
self.add_teams_to_groups()
def add_remaining_to_groups(self):
""" Add leftover students to groups if there are
more than one remaining students but not enough
for a full group """
choices = copy.deepcopy(self.student_list_copy)
self.groups.append(choices)
return choices
def random_add_to_group(self):
""" Randomly add students to group based on the
passed in number per group"""
choices = random.sample(self.student_list_copy, self.number_per_group)
self.groups.append(choices)
return choices
def add_one_to_group(self):
""" Add leftover student to an existing group at random"""
choices = [self.student_list_copy[0]]
smallest_group_index = self.groups.index(min(self.groups, key=len))
self.groups[smallest_group_index].append(self.student_list_copy[0])
return choices
def remove(self, choices):
""" Remove selected students from the selection list """
for choice in choices:
self.student_list_copy.remove(choice)
def add_teams_to_groups(self):
self.teamed_groups = []
for group in self.groups:
adj = random.choice(self.adjectives).capitalize()
noun = random.choice(self.nouns).capitalize()
str_group = ", ".join(group)
teams = "Team {0} {1}: {2}".format(adj, noun, str_group)
self.teamed_groups.append(teams)
def print_groups(self):
""" Print the groups to the terminal """
os.system('clear')
print("\n" * 8)
for group in self.teamed_groups:
print(group)
print(len(group) * '-' + "\n")
def run():
""" Fuction that instantiates the class and calls print groups"""
try:
number_per_group = int(argv[1])
groups = RandomGroups(number_per_group)
except:
groups = RandomGroups()
groups.print_groups()
if __name__ == "__main__":
run()