Skip to content

Instantly share code, notes, and snippets.

@kirel
Created September 13, 2009 10:15
Show Gist options
  • Select an option

  • Save kirel/186139 to your computer and use it in GitHub Desktop.

Select an option

Save kirel/186139 to your computer and use it in GitHub Desktop.
class String
def - other
@___m = Array.new(self.size) { [] }
___d(self, other)
end
private
def ___d first, other
n, m = first.size-1, other.size-1
return [first.size, other.size].max if first.empty? || other.empty?
# return 0 if self.empty? # Yeah?
@___m[n][m] ||= if first[n] != other[m]
1 + [
# insert
___d(first[0..-2], other[0..-1]),
# substitute
___d(first[0..-2], other[0..-2]),
# delete
___d(first[0..-1], other[0..-2]),
].min
else
___d(first[0..-2], other[0..-2])
end
end
end
puts "WTF my Interwebz?" - "GTFO my Interwebz!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment