Re: [Tutor] Problems padding a string with 0's while deconcatonating
On 06/12/05, Josh Yagy <[EMAIL PROTECTED]> 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: Perhaps you could pad the original string! How much do we pad it by? Well, if n is the word size, we're not interested in how many complete chunks of n bits there are. We're only interested in how many are left over, once we cast out all complete pieces. This is equivalent to asking for the remainder, after dividing the length by n. eg: >>> b = '01010011010110101101101' >>> len(b) 23 >>> len(b)//4# The // means we want integer division, not real division. 5 >>> len(b)-4*(len(b)//4) 3 So, if we split b into 4 bit pieces, there will be 5 of them, and 3 bits left over. But there's an easier way to do that calculation --- the modulo operator: >>> len(b) % 4 3 That's how many extra bits we have, so we can subtract that from n to get the number of '0's we need to pad with. Does that help? -- John. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Problems padding a string with 0's while deconcatonating
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', '', ''] > >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) '01010101' HTH! Ismael ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Problems padding a string with 0's while deconcatonating
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', '', ''] 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 -----Original Message----- From: Kent Johnson <[EMAIL PROTECTED]> Date: Mon, 05 Dec 2005 13:05:46 -0500 Subject: Re: [Tutor] Problems padding a string with 0's while deconcatonating Josh Yagy wrote: >I have the following code: > >def binaryString(b, n): > s=[] > j=0 > while j < len(b): > temp = b[j:j+n] > s = s + [ temp ] > j = j + n > return s > >which works for all intents and purposes, but I can't figure out a way to get >it to pad the left of the string with zeros so that each of the subsets is n >bits long. As is, it will deconcatonate a given string into n-substrings, but >not all of the substrings are n bits long. Any help would be greatly >appreciated and sorry again if I didn't include anything I should. > Josh, I don't think I understand the problem. When I try your function I get b= '01010011000111' print binaryString(b, 4) -> ['0101', '0011', '0001', '1100', '0011', '11'] How does this differ from what you want? I am not losing any leading zeros, just the last substring is short. BTW your function can be written much more compactly. I'll show you how in a few steps. First, notice that the sequence of values in j can be generated with the range() function, e.g. >>> range(0, len(b), 4) [0, 4, 8, 12, 16, 20] This lets you replace the calculation of j with a simple for statement: def binaryString2(b, n): s=[] for j in range(0, len(b), n): temp = b[j:j+n] s = s + [ temp ] return s Next, use list.append() to add to s and get rid of temp: def binaryString3(b, n): s=[] for j in range(0, len(b), n): s.append(b[j:j+n]) return s Now this can be easily replaced with a single list comprehension: def binaryString4(b, n): return [b[j:j+n] for j in range(0, len(b), n)] (This version appears in the Python Cookbook at http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/425044) Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Problems padding a string with 0's while deconcatonating
Josh Yagy wrote: >I have the following code: > >def binaryString(b, n): > s=[] > j=0 > while j < len(b): > temp = b[j:j+n] > s = s + [ temp ] > j = j + n > return s > >which works for all intents and purposes, but I can't figure out a way to get >it to pad the left of the string with zeros so that each of the subsets is n >bits long. As is, it will deconcatonate a given string into n-substrings, but >not all of the substrings are n bits long. Any help would be greatly >appreciated and sorry again if I didn't include anything I should. > Josh, I don't think I understand the problem. When I try your function I get b= '01010011000111' print binaryString(b, 4) -> ['0101', '0011', '0001', '1100', '0011', '11'] How does this differ from what you want? I am not losing any leading zeros, just the last substring is short. BTW your function can be written much more compactly. I'll show you how in a few steps. First, notice that the sequence of values in j can be generated with the range() function, e.g. >>> range(0, len(b), 4) [0, 4, 8, 12, 16, 20] This lets you replace the calculation of j with a simple for statement: def binaryString2(b, n): s=[] for j in range(0, len(b), n): temp = b[j:j+n] s = s + [ temp ] return s Next, use list.append() to add to s and get rid of temp: def binaryString3(b, n): s=[] for j in range(0, len(b), n): s.append(b[j:j+n]) return s Now this can be easily replaced with a single list comprehension: def binaryString4(b, n): return [b[j:j+n] for j in range(0, len(b), n)] (This version appears in the Python Cookbook at http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/425044) Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Problems padding a string with 0's while deconcatonating
Hello everyone, this is my first time using the tutor e-mail, so if I mess up any common format or something just let me know :). Alright, for a little background, I'm in the process of writing a merkle-hellman cryptosystem for my CS15 class. I'm doing fine till I get to the decatonation of the long binary string into n-strings which will then be used to match up to the public key. I have the following code: def binaryString(b, n): s=[] j=0 while j < len(b): temp = b[j:j+n] s = s + [ temp ] j = j + n return s which works for all intents and purposes, but I can't figure out a way to get it to pad the left of the string with zeros so that each of the subsets is n bits long. As is, it will deconcatonate a given string into n-substrings, but not all of the substrings are n bits long. Any help would be greatly appreciated and sorry again if I didn't include anything I should. -Josh ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor