Skip to content

Instantly share code, notes, and snippets.

@DimaSamodurov
Created December 1, 2011 12:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DimaSamodurov/1416313 to your computer and use it in GitHub Desktop.
Save DimaSamodurov/1416313 to your computer and use it in GitHub Desktop.
def distance(str1, str2)
str1.downcase!
pairs1 = (0..str1.length-2).collect {|i| str1[i,2]}.reject {
|pair| pair.include? " "}
str2.downcase!
pairs2 = (0..str2.length-2).collect {|i| str2[i,2]}.reject {
|pair| pair.include? " "}
union = pairs1.size + pairs2.size
intersection = 0
pairs1.each do |p1|
0.upto(pairs2.size-1) do |i|
if p1 == pairs2[i]
intersection += 1
pairs2.slice!(i)
break
end
end
end
(2.0 * intersection) / union
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment