Python Forum
Four digit combinations
Thread Rating:
  • 2 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Four digit combinations
#1
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!
Reply
#2
Read the following: https://docs.python.org/3.6/library/iter...rmutations
It has examples.
Reply
#3
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])))
Reply
#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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  list digit into number Voldyy 2 1,496 Jul-10-2022, 06:13 PM
Last Post: deanhystad
  Get 5 most unique combinations of elements in a 2D list wanttolearn 1 2,281 Sep-24-2020, 02:26 PM
Last Post: buran
  Frequency in first digit from csv file, NO IMPORT bryan0901 6 2,787 May-28-2020, 09:50 AM
Last Post: bryan0901
  Four-digit number text translation. soz 3 2,650 May-13-2019, 03:02 PM
Last Post: buran
  Combinations mnnewick 1 2,573 Dec-17-2018, 02:35 PM
Last Post: ichabod801
  create three digit numbers Krszt 4 4,398 Dec-09-2018, 03:12 PM
Last Post: ThePhi
  Sum of digit in a string MEH012 1 9,333 Apr-20-2018, 02:13 AM
Last Post: Larz60+
  Allow only digit penoxcz 3 3,749 Nov-14-2017, 03:04 PM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020