Peter Hansen <[EMAIL PROTECTED]> 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 <[EMAIL PROTECTED]> -- http://www.craig-wood.com/nick
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to