Nov-19-2023, 05:03 PM
Hi everyone,
This is my first post on this forum so please bear with me. At the very beginning I'd like to let you know that I'm a complete rookie and before you start hammering me that I haven't tried to solve the problem myself, that it's basics etc. please know that I did some research into this kind of a problem, mainly on how to extract unique pairs from a list however most of the solutions I've come across are a bit too complex for me to follow and had a bit different application as they were strictly integer based rather than string based. I literally just started my adventure with coding.
Problem:
I want to write a program which will select two random names from a list and by doing so will create a unique pair of names. The two people selected will be nominated to buy Christmas presents for each other. We always have a problem in my family when it comes to buying Christmas presents so I thought it'd be nice to randomize things a little.
Thanks in advance.
This is my first post on this forum so please bear with me. At the very beginning I'd like to let you know that I'm a complete rookie and before you start hammering me that I haven't tried to solve the problem myself, that it's basics etc. please know that I did some research into this kind of a problem, mainly on how to extract unique pairs from a list however most of the solutions I've come across are a bit too complex for me to follow and had a bit different application as they were strictly integer based rather than string based. I literally just started my adventure with coding.
Problem:
I want to write a program which will select two random names from a list and by doing so will create a unique pair of names. The two people selected will be nominated to buy Christmas presents for each other. We always have a problem in my family when it comes to buying Christmas presents so I thought it'd be nice to randomize things a little.
import random names = ["Adam","Anna","Jessica","Stephen","Claude","Gary"] for i in names: k = random.sample(names, 2) j =k if j == k: k = random.sample(names,2) print(k)The main problem now is that I don't know how to create a proper check loop which would compare the newly generated pairs with the names stored in the original list so I can avoid having the same or repetitive pairs. For that reason I used the if statement to generate another set of pairs but I realise it is not the proper way of doing it, therefore any guidance on how proceed would be much appreciated.
Thanks in advance.