Guest User

Untitled

a guest
Feb 19th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.23 KB | None | 0 0
  1. import random
  2.  
  3. data = {
  4. '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'],
  5.  
  6. 'project_1' : [['brendanmcurran', 'arielhaney', 'awootton44'], ['AndreaAngquist','gshapiro','andrew.c.chao'],['achung','arielhaney','donghyuk-jung'],['gahernandez','icheung','Matt Acalin','allie'],['gaurav.shetti','vimalkini'],
  7. ['sam','Elliot','jenmwang'],
  8. ['natarajan','karthikv'],
  9. ['rami','deepaks','saghar'],],
  10.  
  11. 'project_2' : [['sam','allie','andrew.c.chao'],['gahernandez','arielhaney','jenmwang'],['vimalkini','icheung'],
  12. ['brendanmcurran','karthikv','Matt Acalin'], ['natarajan','gaurav.shetti'],['AndrewAngquist','Elliot','awootton44','rami'],['deepaks','emilybarabas','gshapiro','donghyuk-jung']],
  13.  
  14. 'project_3' : [['rami','icheung','awootton44'],['brendanmcurran','arielhaney','natarajan'],['Elliot','jenmwang','andrew.c.chao'],['deepaks','allie','gaurav.shetti'],['sam','gahernandez','gshapiro'],
  15. ['Matt Acalin','AndreaAngquist','awootton44'],
  16. ['vimalkini','donghyuk-jung','karthikv']]
  17. }
  18.  
  19. students_list = data['students']
  20.  
  21. group_size = 3
  22.  
  23. def main():
  24. '''Outputs a list of entirely novel, semi-random groups based on the student data, and/or returns an unprocessable remainder'''
  25. print "New groups: "
  26. while len(students_list) > group_size:
  27. root_student = students_list.pop(0)
  28. make_group([root_student])
  29. print "Remainder: " + serial(students_list)
  30.  
  31.  
  32. def make_group(current_group,counter=0):
  33. if len(current_group) == group_size:
  34. print serial(current_group)
  35. elif counter > 10 or len(students_list) < 1:
  36. print 'Stalled on group [' + serial(current_group) + '] with remainder [' + serial(students_list) + ']'
  37. else:
  38. index = random.randint(0,len(students_list)-1)
  39. new_student = students_list[index]
  40. if new_to_group(new_student,current_group):
  41. current_group.append(new_student)
  42. students_list.pop(index)
  43. make_group(current_group,counter)
  44. else:
  45. make_group(current_group,counter+1)
  46.  
  47. def new_to_group(new_student,group):
  48. '''Checks if a given student has worked with any of the members of a given group before'''
  49. is_new = True
  50. for student in group:
  51. if have_worked_together(student,new_student):
  52. is_new = False
  53. return is_new
  54.  
  55. def have_worked_together(student1, student2):
  56. '''Takes two students, returns True if they've worked together in the past'''
  57. fact = False
  58. # if any function call returns True, fact will become True
  59. fact = fact or work_together_helper(data['project_1'],student1,student2)
  60. fact = fact or work_together_helper(data['project_2'],student1,student2)
  61. fact = fact or work_together_helper(data['project_3'],student1,student2)
  62. return fact
  63.  
  64. def work_together_helper(project, student1, student2):
  65. for team in project:
  66. if student1 in team:
  67. if student2 in team:
  68. return True
  69. break
  70. return False
  71.  
  72. def has_worked_with(student):
  73. '''Returns a list of people that a given student has worked with in the past, checking for repeats'''
  74. # Not called by the main function in any way, but useful for checking results
  75. projects=[data['project_1'],data['project_2'],data['project_3']]
  76. worked_with_dico = {}
  77. worked_with_list = []
  78. for project in projects:
  79. for team in project:
  80. if student in team:
  81. for person in team:
  82. if person != student and not person in worked_with_dico:
  83. worked_with_list.append(person)
  84. worked_with_dico[person] = 1
  85. return worked_with_list
  86.  
  87. def serial(list):
  88. '''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'''
  89. if type(list) == type(str()):
  90. return list
  91. elif list:
  92. s = ''
  93. for item in list:
  94. s = s + item + ', '
  95. return s
  96. else:
  97. return ""
Add Comment
Please, Sign In to add comment