[Tutor] Masking operation
Kent Johnson
kent37 at tds.net
Thu Oct 15 12:54:57 CEST 2009
On Wed, Oct 14, 2009 at 10:48 PM, Wayne <srilyk at gmail.com> wrote:
> Using zip is redundant for me, this is what my function looks like now:
> def crypt(msg, mask):
> m = itertools.cycle(mask)
> word = ''
> for l in msg:
> word += chr(ord(l) ^ ord(m.next()))
> return word
With zip() and a generator expression you can write it as
def crypt(msg, mask):
mask = itertools.cycle(mask)
chars = zip(msg, mask)
return ''.join(chr(ord(l) ^ ord(m)) for l, m in chars)
which could be reduced to a one-liner if you like.
Kent
More information about the Tutor
mailing list