Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- data = {
- 'students' : ['allie','AndreaAngquist', 'andrew.c.chao','arielhaney','deepaks', 'donghyuk-jung', 'Elliot', 'emilybarabas', 'gahernandez', 'gshapiro','icheung','jenmwang', 'Matt Acalin','natarajan','sam', 'vimalkini', 'brendanmcurran','awootton44','karthikv','gaurav.shetti', 'rami'],
- 'project_1' : [['brendanmcurran', 'arielhaney', 'awootton44'], ['AndreaAngquist','gshapiro','andrew.c.chao'],['achung','arielhaney','donghyuk-jung'],['gahernandez','icheung','Matt Acalin','allie'],['gaurav.shetti','vimalkini'],
- ['sam','Elliot','jenmwang'],
- ['natarajan','karthikv'],
- ['rami','deepaks','saghar'],],
- 'project_2' : [['sam','allie','andrew.c.chao'],['gahernandez','arielhaney','jenmwang'],['vimalkini','icheung'],
- ['brendanmcurran','karthikv','Matt Acalin'], ['natarajan','gaurav.shetti'],['AndrewAngquist','Elliot','awootton44','rami'],['deepaks','emilybarabas','gshapiro','donghyuk-jung']],
- 'project_3' : [['rami','icheung','awootton44'],['brendanmcurran','arielhaney','natarajan'],['Elliot','jenmwang','andrew.c.chao'],['deepaks','allie','gaurav.shetti'],['sam','gahernandez','gshapiro'],
- ['Matt Acalin','AndreaAngquist','awootton44'],
- ['vimalkini','donghyuk-jung','karthikv']]
- }
- students_list = data['students']
- group_size = 3
- def main():
- '''Outputs a list of entirely novel, semi-random groups based on the student data, and/or returns an unprocessable remainder'''
- print "New groups: "
- while len(students_list) > group_size:
- root_student = students_list.pop(0)
- make_group([root_student])
- print "Remainder: " + serial(students_list)
- def make_group(current_group,counter=0):
- if len(current_group) == group_size:
- print serial(current_group)
- elif counter > 10 or len(students_list) < 1:
- print 'Stalled on group [' + serial(current_group) + '] with remainder [' + serial(students_list) + ']'
- else:
- index = random.randint(0,len(students_list)-1)
- new_student = students_list[index]
- if new_to_group(new_student,current_group):
- current_group.append(new_student)
- students_list.pop(index)
- make_group(current_group,counter)
- else:
- make_group(current_group,counter+1)
- def new_to_group(new_student,group):
- '''Checks if a given student has worked with any of the members of a given group before'''
- is_new = True
- for student in group:
- if have_worked_together(student,new_student):
- is_new = False
- return is_new
- def have_worked_together(student1, student2):
- '''Takes two students, returns True if they've worked together in the past'''
- fact = False
- # if any function call returns True, fact will become True
- fact = fact or work_together_helper(data['project_1'],student1,student2)
- fact = fact or work_together_helper(data['project_2'],student1,student2)
- fact = fact or work_together_helper(data['project_3'],student1,student2)
- return fact
- def work_together_helper(project, student1, student2):
- for team in project:
- if student1 in team:
- if student2 in team:
- return True
- break
- return False
- def has_worked_with(student):
- '''Returns a list of people that a given student has worked with in the past, checking for repeats'''
- # Not called by the main function in any way, but useful for checking results
- projects=[data['project_1'],data['project_2'],data['project_3']]
- worked_with_dico = {}
- worked_with_list = []
- for project in projects:
- for team in project:
- if student in team:
- for person in team:
- if person != student and not person in worked_with_dico:
- worked_with_list.append(person)
- worked_with_dico[person] = 1
- return worked_with_list
- def serial(list):
- '''Takes either a string or a list of strings; if it's a string, returns the string, if it's a list, turns it into a comma-delineated string'''
- if type(list) == type(str()):
- return list
- elif list:
- s = ''
- for item in list:
- s = s + item + ', '
- return s
- else:
- return ""
Add Comment
Please, Sign In to add comment