2

Ok so, I'm working on a program that counts in binary up to 3 bytes. I did this to find out the total number of combinations for 3 bytes, just for personal knowledge. Currently, I have

def test(): #nested forloop binary counter;
input1 = 2
counter = 1
for a in range(input1):
    for b in range(input1):
        for c in range(input1):
            for d in range(input1):
                for e in range(input1):
                    for f in range(input1):
                        for g in range(input1):
                            for h in range(input1): #H = 8, 1 Byte
                                for i in range(input1):
                                    for j in range(input1):
                                        for k in range(input1):
                                            for l in range(input1):
                                                for m in range(input1):
                                                    for n in range(input1):
                                                        for o in range(input1):
                                                            for p in range(input1): #P = 16, 2 Bytes
                                                                for q in range(input1):
                                                                    for r in range(input1):
                                                                        for s in range(input1):
                                                                            for t in range(input1):
                                                                                for u in range(input1):
                                                                                    for v in range(input1):
                                                                                        for w in range(input1):
                                                                                            for x in range(input1): #X = 24, 3 Bytes
                                                                                                print(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,'    ',counter)
                                                                                                counter = counter + 1

When I try to run this, it gives me an error: "too many statically nested blocks". So is there any way to get around this with the same effect, but without all the nested for-loops?

Mark Costello
  • 145
  • 10
  • 3
    As appalling as it looks, it's clear he's manually toggling 24 individual bits. – Harvey Feb 06 '17 at 18:34
  • 1
    And it's functionally sound, no matter how bad it looks! – Mad Physicist Feb 06 '17 at 18:35
  • 1
    This is the worst code I have seen on a totally legitimate question that actually shows a lot of effort on OP's part that I have seen in a while. – Mad Physicist Feb 06 '17 at 18:36
  • If all you are looking for is the number of possible combinations, you can calculate that using `2^N`, where `N` is the number of bits. But from a functional standpoint in Python coding, this is actually an interesting question. – tmwilson26 Feb 06 '17 at 18:37
  • please, don't do that again, but you made me laugh – bottaio Feb 06 '17 at 18:41
  • Since marked duplicate, I can't add this, but this code does the same thing as yours but is much shorter (but still a bit ugly): `def test(counter=[0], bits=[], stop=24): if len(bits) == stop: print(*bits, ' ', counter[0]) counter[0] += 1 return bits.append(0) test(counter, bits) bits[-1] = 1 test(counter, bits) bits.pop()`. [Gist](https://gist.github.com/sr105/f3a522b3f76f80a719120f7f3dd89f1a) – Harvey Feb 06 '17 at 19:02

1 Answers1

5

Use itertools.product:

>>> from itertools import product
>>> for comb in product('01', repeat=24):
...     print(''.join(comb))
...
000000000000000000000000
000000000000000000000001
000000000000000000000010
000000000000000000000011
000000000000000000000100
...

or even better, binary conversion:

>>> for i in range(2**24):
...     print(bin(i)[2:].zfill(24))
...
000000000000000000000000
000000000000000000000001
000000000000000000000010
000000000000000000000011
000000000000000000000100
...
Uriel
  • 15,579
  • 6
  • 25
  • 46