May-31-2018, 11:53 PM
I am trying to create random pairings from a list of players.
I first need to randomize the entire list into pairs and then divide that list into halves.
I then want to calculate which of the fist group of pairings will play against the others.
example. List 1,2,3,4,5,6,7,8,9,10
Random Pairs 2 and 4, 1 and 3,5 and 9, 10 and 7, 6 and 8,
Then divide this list in two and create new pair of pairs
Example 2 and 4 play 1 and 3, 5 and 9 play 10 and 7 and so on.....
I have worked out the first pairs, but am stuck on the second part.
How do I complete the code?
I first need to randomize the entire list into pairs and then divide that list into halves.
I then want to calculate which of the fist group of pairings will play against the others.
example. List 1,2,3,4,5,6,7,8,9,10
Random Pairs 2 and 4, 1 and 3,5 and 9, 10 and 7, 6 and 8,
Then divide this list in two and create new pair of pairs
Example 2 and 4 play 1 and 3, 5 and 9 play 10 and 7 and so on.....
I have worked out the first pairs, but am stuck on the second part.
How do I complete the code?
import random # Input the actual number of players registered to play today num_players = int(input("How many players are there today? ")) # Add 1 to num_players to make the number correct num_players += 1 # Create the list of players from "1" to number entered in input my_list = list(range(1, num_players)) l = my_list # Create pairs pairs = {} while len(l) > 1: #Using the randomly created indices, respective elements are popped out r1 = random.randrange(0, len(l)) elem1 = l.pop(r1) r2 = random.randrange(0, len(l)) elem2 = l.pop(r2) # now the selected elements are paired in a dictionary pairs[elem1] = elem2 #The variable 'pairs' is now a dictionary of randomised pairs ##We can now print the elements of the dictionary in your desired format: i = 1 for key, value in pairs.items(): # Print out the result print("Rink {:2d}: {:6d} and {:3d}".format(i, key, value)) i += 1