[Tutor] Bit Strings
Thomi Richards
thomi at imail.net.nz
Wed Oct 22 00:53:11 EDT 2003
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Hi,
>
> Any suggestions?
>
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?
here's a sampel run of your code: (using 3 bits / iterations):
>>> bitStrings = []
>>> for a0 in [0,1]:
... for a1 in [0,1]:
... for a2 in [0,1]:
... oneString = str(a0)+str(a1)+str(a2)
... bitStrings.append(oneString)
...
>>> bitStrings
['000', '001', '010', '011', '100', '101', '110', '111']
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
...
which leaves us with the task of coding a decimal to binary converter. This is
actually quite simple...One way to do it is to use a dictionary to convert
from a hex value to a binary one (AFAIK, python doesn't have a binary
converter. It does however have a hex converter).
However, there are a couple of complications in this function:
1.- we could/should? pad the binary values with zero's.. so binary 1 becomes
001, or 00000001 (for an 8 bit number)
2.- we can't do a direct map from decimal->hex-> binary, because we might be
given a decimal larger than 15, which will result in a two digit hex
number...
So, something like this:
>>> def binary(decimal):
... bin_string = ''
... map_dict = {
... '0' : '0000',
... '1' : '0001',
... '2' : '0010',
... '3' : '0011',
... '4' : '0100',
... '5' : '0101',
... '6' : '0110',
... '7' : '0111',
... '8' : '1000',
... '9' : '1001',
... 'a' : '1010',
... 'b' : '1011',
... 'c' : '1100',
... 'd' : '1101',
... 'e' : '1110',
... 'f' : '1111' }
... for hexdigit in hex(decimal)[2:]: #ignore the leading '0x'
... bin_string += map_dict[hexdigit]
... return bin_string
to test our decimal -> binary converter:
>>> binary(5)
'0101'
>>> binary(50)
'00110010'
Now we can test the whole thing:
bitStrings()
(this uses the default, 3 places)
for more places:
>>> 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.
I've been doing this all in the interactive window, and it's getting a bit
cumbersome. Perhaps it's time for someone else to continue this ;P
It's pretty much there, and it's prettyextendable as well. (I would appreciate
some feedback on this from some of the more experienced tutor list members as
well ;)
anyway, i hope this helps!
Thanks,
- --
Thomi Richards,
http://once.sourceforge.net/
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.3 (GNU/Linux)
iD8DBQE/lg032tSuYV7JfuERAjW7AKCQXafrATb5HcJwtBWdheoGFDQWCQCfUZ1X
8dKMIJ4hZvSueTUSfvhycUQ=
=bMx+
-----END PGP SIGNATURE-----
More information about the Tutor
mailing list