jeudi 26 février 2015

Creating random pairs from lists

I am trying to create a program that will print pairs of the elements in a list. I need to create a dictionary (that is empty at first) where I can store values, Loop through the list to make a pair and make sure there are no duplicates.


When I loop in the list, I need to get a random number and I can use it to remove an element. Remove the randomly selected element from the list using pop method, store the element to a variable, say element1. Repeat it to create element2.


Map the element1 to element2 by inserting the element1 as key to the pairs dictionary, and setting its value to element2, that is, if we call pairs[element1] later it should give us the value of element2.


Print the result using dictionary’s items() and keys() methods.


The catch is, I can't use the random module :(


Example is this:



list = ["Mother", "Father", "Aunt", "Uncle", "Brother", "Sister" ]


Sample run of the program, this creates 3 pairs because there are 6 elements in the list.



Pair 1: Mother and Aunt
Pair 2: Uncle and Sister
Pair 3: Brother and Father


Here is my program for now:



family = ["Mother", "Father", "Aunt", "Uncle", "Brother", "Sister" ]

for x in family:


pairs = {}


How can I improve/add to this code?





Aucun commentaire:

Enregistrer un commentaire