[Tutor] Masking operation

Wayne srilyk at gmail.com
Thu Oct 15 04:48:38 CEST 2009


On Wed, Oct 14, 2009 at 6:31 PM, Rich Lovely <roadierich at googlemail.com>wrote:

>
> Specifically, cycle and izip.  itertools.cycle is the trick here, izip
> is a more efficient (in this case) version of zip.


Ahah! Cycle is exactly what I wanted - I thought there was something like
this but I forgot where I found it.

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

and works quite perfectly.

Alan - I wish I had known this a few days ago when I was trying to use masks
to set debug levels. Oh well - having experimented with what I've learned in
my discrete structures class about and, or, xor, I've learned what I need to
know, and reviewing your tutorial really helps put some of the pieces
together that I wasn't able to come up with on my own.

Thanks all!
-Wayne
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091014/bd9632b6/attachment.htm>


More information about the Tutor mailing list