XORing long strings opimization?

Peter Otten __peter__ at web.de
Tue Nov 4 14:50:06 EST 2003


Noen wrote:

> Oh, didnt notice that as I wrote it. Thanks...
> Anyway, this is how it should be.
> 
> def XOR(s1,s2):
>      """ XOR string s1 with s2 """
>      output = ""
>      # Argument check
>      if (type(s1) and type(s2)) != type(""):
>          raise TypeError, "Arguments are not strings"
>      if len(s1) != len(s2):
>          raise ValueError, "Size differs in strings"
>      # Xoring
>      for i in range(len(s1)):
>          output += chr(ord(s1[i]) ^ ord(s2[i]))
>      return output

Oops, I missed one: 

>>> xor2.XOR(1, "")
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "xor2.py", line 7, in XOR
    if len(s1) != len(s2):
TypeError: len() of unsized object

Did you expect this?

Peter




More information about the Python-list mailing list