1

I am trying to print the max number of bits (n) and also the max number those number of bits are able to represent. I feel as though the problem is in the x = x%2

input:

sortbit(3)

output:

000
111
000
111
000
111
000
111

expected output"

000
001
010
011
100
101
110
111

my code:

def sortbit(n):
    max_num = 2**n

    for x in range(0,max_num):
        stringy = []
        a = 0
        while a < n:
            a += 1
            x = x % 2
            if x == 0:
                stringy.insert(a,'0')
            else:
                stringy.insert(a,'1')
        t = ''.join(stringy)
        print t
5
  • 1
    Want to take a shortcut? ["".join(item) for item in itertools.product(("01"), repeat=3)] Commented Oct 8, 2013 at 6:54
  • correctly if I am using this term wrong. This is quite pythonic? lol Commented Oct 8, 2013 at 6:57
  • 2
    Or use format: for i in range(2**3): print format(i,'03b') Commented Oct 8, 2013 at 6:58
  • @MartijnPieters, Nope...check again. Commented Oct 8, 2013 at 7:16
  • 3
    @MarkTolonen: I'll go wear a paper bag now.. Commented Oct 8, 2013 at 7:17

3 Answers 3

2

You are turning any number into either 1 or 0 with x = x % 2. You may as well just print str(x % 2) * n.

You need to use integer division instead, and test separately for even or odd.

Even better, you could just append the output of the modulus test, as string, to stringy:

stringy.insert(0, str(x % 2))
x = x // 2

Demo with the code simplified a bit:

>>> def sortbit(n):
...     max_num = 2**n
...     for x in range(max_num):
...         stringy = []
...         for a in range(n):
...             stringy.append(str(x % 2))
...             x //= 2
...         print ''.join(reversed(stringy))
... 
>>> sortbit(3)
000
001
010
011
100
101
110
111

You could also bitshift the value; the >> operator moves the bits to the right for you by a given number of steps; x >> 1 shifts the bits in x one step over, essentially dividing by two.

You could also look into the bin() function (returns the value as a binary string, starting with 0b), and the format() function, together with the str.format() method, which lets you format values as binary strings as well using the b output format. Printing your values in a loop could be as simple as:

def sortbit(n):
    for i in range(2**n):
        print '{:0{}b}'.format(i, n)

Last but not least, you are simply producing the product of the digits 0 and 1, n times. You could express that with itertools.product() as well:

>>> from itertools import product
>>> for bits in product('01', repeat=3):
...     print ''.join(bits)
... 
000
001
010
011
100
101
110
111

but that could be seen as cheating. :-)

Sign up to request clarification or add additional context in comments.

4 Comments

how are you so good at python... when will i reach this level
range(n**2-1) is wrong...n of 4 will be print 0-14 decimal.
This page is changing too quickly :)
Do try to keep up! :-P
2

You've got answers for your specific code, so here's a solution using Python's format to print n digits of binary with the appropriate number of leading zeroes:

>>> def sortbit(n):
...  for i in range(2**n):
...   print '{0:0{1}b}'.format(i,n)
...
>>> sortbit(2)
00
01
10
11
>>> sortbit(3)
000
001
010
011
100
101
110
111

2 Comments

@MartijnPieters That's actual output.
@MartijnPieters, LOL actually 2**3 == 2**3. Time to go to bed ;^)
1

You should use x % 2 in the if, not assigning back to x

if x % 2 == 0:
  ...

also at each iteration you should move over to next bit, otherwise you're adding the same bit over and over:

x = x >> 1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.