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
friendsOfRobert
fromstudents
for loop
(1) suppose one
friend
andRobert
was paired.(2)
leftStudent
: Remove thefriend
andRobert
fromstudents
(3) Repeat
findPair
but this time without thefriend
andRobert
. The next equivalent ofRobert
is 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