XOR on string
Nick Craig-Wood
nick at craig-wood.com
Thu Jan 27 10:30:01 EST 2005
Peter Hansen <peter at engcorp.com> wrote:
> snacktime wrote:
> > I need to calculate the lrc of a string using an exclusive or on each
> > byte in the string. How would I do this in python?
>
> lrc == Linear Redundancy Check? or Longitudinal? Note that
> such terms are not precisely defined... generally just acronyms
> people make up and stick in their user manuals for stuff. :-)
>
> import operator
> lrc = reduce(operator.xor, [ord(c) for c in string])
Or for the full functional programming effect...
lrc = reduce(operator.xor, map(ord, string))
which is slightly faster and shorter...
$ python2.4 -m timeit -s'import operator; string = "abcdefghij13123kj12l3k1j23lk12j3l12kj3"' \
'reduce(operator.xor, [ord(c) for c in string])'
10000 loops, best of 3: 20.3 usec per loop
$ python2.4 -m timeit -s'import operator; string = "abcdefghij13123kj12l3k1j23lk12j3l12kj3"' \
'reduce(operator.xor, map(ord, string))'
100000 loops, best of 3: 15.6 usec per loop
--
Nick Craig-Wood <nick at craig-wood.com> -- http://www.craig-wood.com/nick
More information about the Python-list
mailing list