Hi,
I’m new to Julia and trying to solve the cryptopals challenge: Challenge 6 Set 1 - The Cryptopals Crypto Challenges
This challenge requires to compute the hamming distance between two strings:
str1 = "this is a test"
str2 = "wokka wokka!!!"
The Hamming distance is the number of differing bits. In the above case the distance must be 37.
My approach was the following:
Vector{UInt8}(str1) .!= Vector{UInt8}(str2)
but this is wrong (results in a distance of 14)
I get the same results using the hammond
function in the Distances
package.
To confirm that 37 is correct I converted the strings to their bit values using an ASCII table (by hand)
s1 = [0,1,1,1,0,1,0,0,0,1,1,0,1,0,0,0,0,1,1,0,1,0,0,1,0,1,1,1,0,0,1,1,0,0,1,0,0,0,0,0,0,1,1,0,1,0,0,1,0,1,1,1,0,0,1,1,0,0,1,0,0,0,0,0,0,1,1,0,0,0,0,1,0,0,1,0,0,0,0,0,0,1,1,1,0,1,0,0,0,1,1,0,0,1,0,1,0,1,1,1,0,0,1,1,0,1,1,1,0,1,0,0]
s2 = [0,1,1,1,0,1,1,1,0,1,1,0,1,1,1,1,0,1,1,0,1,0,1,1,0,1,1,0,1,0,1,1,0,1,1,0,0,0,0,1,0,0,1,0,0,0,0,0,0,1,1,1,0,1,1,1,0,1,1,0,1,1,1,1,0,1,1,0,1,0,1,1,0,1,1,0,1,0,1,1,0,1,1,0,0,0,0,1,0,0,1,0,0,0,0,1,0,0,1,0,0,0,0,1,0,0,1,0,0,0,0,1]
now the distance is indeed 37:
julia> sum(abs.(s1 .- s2))
37
What is a more elegant solution to this problem?