def findPair(students, Robert):
#...
if len(students) == 2:
#do something
return
else:
for friend in friendsOfRobert:
print(students)
leftStudents = removePairFromStudents(students, friend, Robert)
if len(leftStudents) != 0:
findPair(leftStudents, leftStudents[0])
return
I don't quite understand why students is modified as it loops inside for. It is not even global variable. The following is just to help you see the structure of the code.
- ... part : find
friendsOfRobertfromstudents for loop
(1) suppose one
friendandRobertwas paired.(2)
leftStudent: Remove thefriendandRobertfromstudents(3) Repeat
findPairbut this time without thefriendandRobert. The next equivalent ofRobertis randomly selected (leftStudents[0])
On the side note, I solved the issue by remembering the pair previously removed, and rebuilding the original students set each time (with code below) before it dives into the next loop.
if len(students) == 2:
if len(justPaired) != 0:
students.append(justPaired[0])
students.append(justPaired[1])
# do something
return
edit : removed unnecessary example