All possible ways of having x digit number with only 2 possible numbers (python) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

All possible ways of having x digit number with only 2 possible numbers (python)

Let's say we have 2 digit number with only possible numbers of zero and one : 00, 01, 10, 11 Is there a way I can have all possible numbers with same condition for a x digit number on python?

26th Aug 2021, 6:32 PM
pedram ch
pedram ch - avatar
19 Answers
+ 4
Sounds like you want itertools permutations. Example of all permutations of 'abc' You can easily covert this so it works with your digits and only returns the full length permutations. https://docs.python.org/3/library/itertools.html https://code.sololearn.com/cuAr5wOk4UV5/?ref=app
26th Aug 2021, 6:56 PM
ChaoticDawg
ChaoticDawg - avatar
+ 2
What are those two numbers in your example above ? Is it 11 or 22 or just 2 ?
26th Aug 2021, 6:40 PM
Abhay
Abhay - avatar
+ 2
You can do it by using nested loops to build up the sequences as strings and then convert to int. The tricky part is that you don't know in advance how many loops you need.
26th Aug 2021, 6:46 PM
Simon Sauter
Simon Sauter - avatar
+ 2
Hey! pedram ch yes you can do it using nested loop and itertools permautation. for i in range(0,5) for j in range(0,5) print(str(i)+str(j)) If you want 5 digit long number you need to create 5 loops. Or you can do it using itertools which is included in python course. I myself had created a wordlist of all possible number of 4 digit 1111,1112,1123……..
26th Aug 2021, 7:09 PM
Iftekhar Ahmed Pranto
Iftekhar Ahmed Pranto - avatar
+ 1
✩✮★✮✩ that was brilliant and helped me understand concept of permutations ... thank you so much 🙏🙏
26th Aug 2021, 7:48 PM
pedram ch
pedram ch - avatar
+ 1
pedram ch You can make those conversions and then do a permutation of them as well and add them to the set of previous permutations. The code for which will depend on how your digits should be converted. For instance if you're only using 1's and 0's and given the digits as 100 or 001 or 010 should it also permutate 110 or 011 and/or 111?
26th Aug 2021, 7:49 PM
ChaoticDawg
ChaoticDawg - avatar
+ 1
Vasiliy that just calculates how many permutations there are. It does not give the permutations.
26th Aug 2021, 8:09 PM
Simon Sauter
Simon Sauter - avatar
0
Abhay the two numbers(I think I should have said digits) are "one" and "zero" Which as I said for a 2 digit number would make 00,11,01,10 But I want it for a x digit number like 3 (000, 001, 011....) or more
26th Aug 2021, 6:43 PM
pedram ch
pedram ch - avatar
0
Simon Sauter Ops .. so there is no math or binary method which could do this?
26th Aug 2021, 6:52 PM
pedram ch
pedram ch - avatar
0
I doubt it, since it's not a math problem.
26th Aug 2021, 6:55 PM
Simon Sauter
Simon Sauter - avatar
0
ChaoticDawg Pranto I checked out permutations. As said some conversions should be made and I need to find out how. Thank you so much for the hint
26th Aug 2021, 7:28 PM
pedram ch
pedram ch - avatar
0
ChaoticDawg yes every possible ways of them. I was trying to figure out how does it work since passed '10' as argument and 4 as length but I understood the first arguments length should be as much as defined length in Secound argument . That was just what I was looking for
26th Aug 2021, 7:57 PM
pedram ch
pedram ch - avatar
0
Mathematics is such a science that with its help it is possible to solve any problem, there would be knowledge. ☺️ an = a**n Two digits - 0 and 1 in a two-digit number including 00: an = 2**2 Two digits - 0 and 1 in a three-digit number including 000: an = 2**3 Etc. ☺️
26th Aug 2021, 8:02 PM
Solo
Solo - avatar
0
Simon Sauter I gave an answer for the number of iterations required. Write a "permutation" function and call it in a loop, or recursively, of your choice. ☺️
26th Aug 2021, 8:16 PM
Solo
Solo - avatar
0
If you want to give it a try by yourself, try using recursion. This suggestion should point you in the right direction.
28th Aug 2021, 5:20 PM
Nboumakis
0
Vasiliy Not every problem... Ask Turing or Gödel.
28th Aug 2021, 5:22 PM
Nboumakis
0
Nboumakis 🤣🤣🤣 yes, they know the answer for sure. The main thing is not to confuse tasks with problems. 🤣🤣🤣
28th Aug 2021, 6:41 PM
Solo
Solo - avatar
0
Vasiliy No, if that were the case, there would exist an algorithm capable of solving the "Halting problem". But there is not. Anyway, since this is a different discussion, if you want to elaborate, you could contact me through the Direct Message feature.
29th Aug 2021, 10:32 AM
Nboumakis
- 2
Je sais comment ça marche
28th Aug 2021, 6:19 AM
Mell Seka Douglas
Mell Seka Douglas - avatar