Python Forum
Four digit combinations - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Four digit combinations (/thread-4392.html)



Four digit combinations - EHod - Aug-13-2017

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!


RE: Four digit combinations - Larz60+ - Aug-13-2017

Read the following: https://docs.python.org/3.6/library/itertools.html#itertools.permutations
It has examples.


RE: Four digit combinations - hbknjr - Aug-13-2017

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])))



RE: Four digit combinations - ichabod801 - Aug-13-2017

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.


RE: Four digit combinations - EHod - Aug-13-2017

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