Input: List of last names. The names in the list are unique.

Output: A dictionary containing pairs of surnames. Pairs "Last Name 1 - Last Name 2" are unique, that is, none of the last names in the couple are repeated in other pairs.

"Business" - meaning: Create a generator to play a secret Santa.

What algorithm I took as a basis: 1. Checking an item from a list in the dictionary for a key or value position. If the item is - go to the next. 2. If there is no element, we find a random name from the list. If the surname matches the checked surname OR the found surname is in one of the pairs of the dictionary - we look for the surname again. 3. If the last name is unique - add a couple of the checked and found last names to the dictionary and move on.

What I can not do: 1. Check for the presence of the name to be checked, not only in the dictionary key, but also in the meaning. 2. Make the search cycle random surnames. I do not understand how to designate a condition that restarts the randomizer.

The code I reached:

import random colleagues = ['A', 'B', 'C', 'D','E', 'F', 'G'] ThiefSanta = {} pair = [] for colleague in colleagues: if colleague in ThiefSanta.items(): continue else: if random.choice(colleagues) == colleague: continue; else: friend = random.choice(colleagues) pair.append(colleague) pair.append(friend) ThiefSanta.update([pair]) pair.clear() print(ThiefSanta) 

Please help!

    1 answer 1

    A solution was found on the Internet:

     import random colleagues = ['A', 'B', 'C', 'D','E', 'F', 'G'] random.shuffle(colleagues) offset = [colleagues[-1]] + colleagues[:-1] for santa, receiver in zip(colleagues, offset): print(santa, "buys for", receiver)