dimanche 17 février 2019

applying conditions on the process of randomly picking up element

I need to pick 4 groups of 2 students don't talk the same languge, without repitition. Each student only appears once.

I have this list

import random
from itertools import permutations
seq = [['Sham','Arabic'],['Amina', 'Arabic'], ['Bill', 'French'], ['Qing','Hindi']]

I need to pick 4 groups of 2 students don't talk the same languge, without repitition.

I have implement this Function:

def group (x, y):
    res=[]
    #N = len(seq)
    for i in range( y):
        while len(res) < y:
            res = random.sample(x,k=y)
            while res[i][1] == res[i-1][1]:
                return group(x,y)
    return res

Then I appley this conditions to get the final results, the one students must appear in only one group:

final = []
for i in range(4):
    pick = group (seq, 2)

    if pick in final or pick[::-1]  in final:
        group(seq, 2)
        pass

    for i in pick:
        for j in i:
            if j  in final:
                group(seq, 2)
                pass
    else:
        final.append(pick)

print (final)

Aucun commentaire:

Enregistrer un commentaire