forked from huangs1/gatorgrouper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgroup_random.py
78 lines (65 loc) · 2.4 KB
/
group_random.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
""" group using randomization approach """
import copy
import logging
import itertools
from random import shuffle
from group_scoring import score_groups
def group_random_group_size(responses, grpsize):
""" group responses using randomization approach """
# use itertools to chunk the students into groups
iterable = iter(responses)
groups = list(iter(lambda: list(itertools.islice(iterable, grpsize)), []))
# deal with the last, potentially partial group
last_group_index = len(groups) - 1
if len(groups[last_group_index]) < grpsize:
# distribute them throughout the other groups
logging.info(
"Partial group identified; distributing across other groups.")
lastgroup = groups[last_group_index]
outliers = copy.deepcopy(lastgroup)
groups.remove(lastgroup)
while outliers:
for group in groups:
if outliers:
group.append(outliers[0])
outliers = outliers[1:]
else:
break
# scoring and return
scores, ave = [], 0
scores, ave = score_groups(groups)
logging.info("scores: " + str(scores))
logging.info("average: " + str(ave))
return groups
def group_random_num_group(responses, numgrp):
""" group responses using randomization approach """
# number of students placed into a group
stunum = 0
iterable = iter(responses)
# number of students in each group (without overflow)
grpsize = int(len(responses) / numgrp)
groups = list()
for i in range(0, numgrp):
group = list()
while len(group) is not grpsize and stunum < len(responses):
group.append(next(iterable))
stunum = stunum + 1
groups.append(group)
# deal with the last remaining students
if len(responses) % stunum is not 0:
logging.info(
"Overflow students identified; distributing into groups.")
for x in range(0, len(responses) % stunum):
groups[x].append(next(iterable))
stunum = stunum + 1
# scoring and return
scores, ave = [], 0
scores, ave = score_groups(groups)
logging.info("scores: " + str(scores))
logging.info("average: " + str(ave))
return groups
def shuffle_students(responses):
""" Shuffle the responses """
shuffled_responses = responses[:]
shuffle(shuffled_responses)
return shuffled_responses