Python Forum

Full Version: Four digit combinations
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

I have a school problem to design and write a program that displays all the possible combinations for any four numerical digits entered by a user. The program should avoid displaying the same combination more than once.

I am completely stuck on how to find all the possible combinations so any help at all is greatly appreciated.

Thanks!
Read the following: https://docs.python.org/3.6/library/iter...rmutations
It has examples.
itertools.permutations shall do it.

But if your numerical digits repeat [eg (1224)]. You'll get some permutations twice.
For that use Sets.

Code example:
import itertools
set(list(itertools.permutations([1, 2, 2, 4])))
I expect his professor wants him to work it out for himself, not just use itertools. In that case, some nested for loops would work to get all of the combinations. Putting them in a set would prevent you from printing the same one twice.
Hi,

Thank you for all your replies, and I think my teacher is going to be very impressed if I try to show I can use either method, so I have already done a version of the code using the intertools method and I'm just trying to work out the currently very confusing order the loops go in.

Thanks for the help