To begin, I have this list (suggested in the exercise):
Members = [('Tessa','G1'),('Evan','G2'),('Tom','G3'),
('Mia','G3'),('Claire','G3'),('Billie','G4'),('Adrian',
'G2'),('Maddie','G1'),('Lewis','G1'),('Tony','G2'),('Joyce',
'G1'),('Julian','G5'),('Joshua','G2')('Warren','G3')]
At the end, I have to write a randomMember(L) function with parameter L which is the list Members defined above to make pairs of two people with the following constraint:
Two members of the same group (G1, G2, ...) cannot meet each other.
For example, using the list Members, "Tessa" and "Lewis" cannot meet each other.
So, I have already written this (the base code has been provided, so the location of the loops should not be changed)
def randomMember(L):
n = len(L)
Perm = permutation(n)
while checkConstrainst(Perm,random(L)) == False:
Perm = permutation(n)
LCouples=[]
for k in range (n):
LCouples.append((L[k],L[Perm[k]]))
return LCouples
To explain more, checkConstrainst function takes two parameters. Perm, which is a list of integers and L is a list which represents a draw made in the Members list previously.
The purpose of this function is to ensure that 2 people are not from the same group. The input L looks like: [(('Tessa', 'G1'), ('Lewis', 'G1')), (('Joshua', 'G2'), ('Billie', 'G4'))]
def checkConstrainst(Perm,L):
for i in range(len(Perm)):
PeopleGroup_1, PeopleGroup_2 = L[i]
People1, Group1 = PeopleGroup_1
People2, Group2 = PeopleGroup_2
if Group1 == Group2:
return False
return True
I have to use permutation to make this working... I don't know how to use the list inputed to make sure Perm verify the constraint for the moment.
For the the list [(('Tessa', 'G1'), ('Lewis', 'G1')), (('Joshua', 'G2'), ('Billie', 'G4'))], checkConstraint returns False and for the list [(('Tessa', 'G1'), ('Joshua', 'G2')), (('Billie', 'G4'), ('Lewis', 'G1'))] returns True because each couple is not from the same group.
The function permutation is just here to generate random integers.
Here is the code if you need:
def permutation(n):
L=[]
Perm=[]
j=0
for i in range(n):
L.append(i)
for k in range(n):
alea = rd.randint(0,len(L)-1)
j=L.pop(alea)
Perm.append(j)
return Perm
In RandomMember, you also have a Random function which takes the Members list as parameter. This function is making couple of people WITHOUT checking if they are from the same group (this is the goal of RandomMember). Random returns something like this : [(('Tessa', 'G1'), ('Lewis', 'G1')), (('Joshua', 'G2'), ('Billie', 'G4'))].
So, my issue is that I've tested checkConstrainst and it works well.
But, in the randomMember function, while building the LCouples list, there is 2 peoples of the same group in a couple...
Here if you need is the random, derangement and isDerangement function :
def isDerangement(Perm):
for i in range(len(Perm)):
if Perm[i]==i:
return False
return True
def derangement(n):
Perm=permutation(n)
while isDerangement(Perm)==False:
Perm=permutation(n)
return Perm
def random(L):
n=len(L)
Couples=[]
D=derangement(n)
for k in range(n):
Couples.append((L[k],L[D[k]]))
return Couples
And I don't understand where is my error despite several hours of looking...
For randomMember(L), here is the basic code I should complete:
def randomMember(L):
.............................................
Perm =........................................
while ......................................:
Perm =....................................
LCouples =[]
for k in range (n):
LCouples . append (.......................).
return LCouples