[Tutor] Bit Strings

Timothy M. Brauch tbrauch at mindless.com
Wed Oct 22 02:13:34 EDT 2003


From: "Thomi Richards" <thomi at imail.net.nz>
>
> I may have an idea ;)
>
> essentially, what your code does is a binary count from 0 to 2^n (where n
is
> the number of iterations / bits). right?
>
> Unless I'm mistaken, couldn't you do something like this:
>
> >>> def bitStrings(iterations=3):
> ...     binary_strlist = []
> ...     for num in range(2**iterations):
> ...             binary_strlist.append(binary(num))
> ...     return binary_strlist
> ...
> >>> bitStrings(5)
> ['0000', '0001', '0010', '0011', '0100', '0101', '0110', '0111', '1000',
> '1001', '1010', '1011', '1100', '1101', '1110', '1111', '00010000',
> '00010001', '00010010', '00010011', '00010100', '00010101', '00010110',
> '00010111', '00011000', '00011001', '00011010', '00011011', '00011100',
> '00011101', '00011110', '00011111']
>
> As you can see, there's still some problems here.
>
>
> - -- 
> Thomi Richards,
> http://once.sourceforge.net/

Ah, very good, very good indeed.  Your result for bitStrings(5) was so close
to being right.  The only thing left to do would be to clean up the strings
so that they all have 5 digits.  Thus '0000' becomes '00000' and '00011001'
becomes '11001' and so forth.  I think that can be done pretty easily.
Something like the following before we append...

bits = binary(num)
while len(bits) < iterations:
    bits = '0' + bits
while len(bits) > iterations:
    if bits[0] != '1':  #check to be sure first digit isn't a one,
        bits = bits[1:] #but it shouldn't be anyway
binary_strlist.append(bits)

Let's put it all together and see what we get...

def bitStrings(iterations=3):
    binary_strlist = []
    for num in range(2**iterations):
        bits = binary(num)
        while len(bits) < iterations:
            bits = '0' + bits
        while len(bits) > iterations:
            if bits[0] != '1':  #check to be sure first digit isn't a one,
                bits = bits[1:] #but it shouldn't be anyway
        binary_strlist.append(bits)
    return binary_strlist

>>> bitStrings(5)
['00000', '00001', '00010', '00011', '00100', '00101', '00110', '00111',
'01000', '01001', '01010', '01011', '01100', '01101', '01110', '01111',
'10000', '10001', '10010', '10011', '10100', '10101', '10110', '10111',
'11000', '11001', '11010', '11011', '11100', '11101', '11110', '11111']

Checking bitStrings(6) and bitStrings(7) gives the correct answer as well.
Perfect.  I'm sure the padding/stripping part could be written a little more
effective, but it fits my needs for right now.  Thanks.

The next task would be what if I needed strings that used 0, 1, and 2.  We'd
need a decimal to trinary function.  Or if I needed strings that used all
digits from 0 to (n-1).  Hmm, I think I know how I will be spending my
weekend.

 - Tim


---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.528 / Virus Database: 324 - Release Date: 10/17/2003




More information about the Tutor mailing list