Josh Yagy wrote:
>Wow, that code is much more compact, thanks for the help! But as far as the
>original question goes, I think I worded my problem wrong. The output you got
>with your binary string is almost what I Want, but not quite. I need each
>subset of binary strings to be n bits long (in the dummy problem you ran, 4
>bits). The last subset is two bits short, but for the sake of the
>cryptosystem, I can't pad it with zeros at the right, I need to pad it to the
>left. for instance:
>Your output was:
> print binaryString(b, 4) -> ['0101', '0011', '0001', '1100', '0011', '11']
>What I'm looking for is for the output to be:
>print binaryString(b, 4) -> ['0001', '0100', '1100', '0111', '0000', '1111']
>
>notice that my desired output is the same as yours, only all the bits are
>moved back two places to fill up the last subset, and the extra space is
>filled with 0's in the first subset.
>
>Thanks again for the help
>
Why don't you pad it before processing?
def padleft(b, bits):
bitsshort = len(b)%bits
if bitsshort: #so that if len(b) == bits you don't get extra 0s
amount = bits - bitsshort
return "0"*amount + b
return b
>>> padleft("10011", 5)
'10011'
>>> padleft("101101", 4)
'00101101'
>>> padleft("1010101", 6)
'000001010101'
HTH!
Ismael
_______________________________________________
Tutor maillist - [email protected]
http://mail.python.org/mailman/listinfo/tutor