XORing long strings opimization?
Noen
not.available at na.no
Tue Nov 4 15:46:06 EST 2003
Peter Otten wrote:
> 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
nope, thought the Argument check would do it, but I used and opeartor
instead of or. Well, here is XOR v.0.3b :P Any comments how to make it
faster?
def XOR(s1,s2):
""" XOR string s1 with s2 """
output = ""
# Argument check
if (type(s1) or 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
More information about the Python-list
mailing list