aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-096/abigail/README.md3
-rw-r--r--challenge-096/abigail/ruby/ch-2.rb31
2 files changed, 33 insertions, 1 deletions
diff --git a/challenge-096/abigail/README.md b/challenge-096/abigail/README.md
index 3582807374..d715618651 100644
--- a/challenge-096/abigail/README.md
+++ b/challenge-096/abigail/README.md
@@ -27,7 +27,7 @@ Output: "family same the of part are Raku and Perl"
* [Node.js](node/ch-1.js)
* [Perl](perl/ch-1.pl)
* [Python](python/ch-1.py)
-* [Ruby](ruby/ch-1.py)
+* [Ruby](ruby/ch-1.rb)
### Blog
@@ -66,5 +66,6 @@ Operation 2: replace 'u' with 'o'
* [Node.js](node/ch-2.js)
* [Perl](perl/ch-2.pl)
* [Python](python/ch-2.py)
+* [Ruby](ruby/ch-2.rb)
### Blog
diff --git a/challenge-096/abigail/ruby/ch-2.rb b/challenge-096/abigail/ruby/ch-2.rb
new file mode 100644
index 0000000000..65a57bd6d3
--- /dev/null
+++ b/challenge-096/abigail/ruby/ch-2.rb
@@ -0,0 +1,31 @@
+def LevenshteinDistance (first, second)
+ n = first . length
+ m = second . length
+ if n < m
+ first, second = second, first
+ n, m = m, n
+ end
+ distances = []
+ for i in 0 .. n do
+ distances [i] = []
+ for j in 0 .. m do
+ distances [i] [j] = i == 0 || j == 0 ? i + j
+ : [distances [i - 1] [j] + 1,
+ distances [i] [j - 1] + 1,
+ distances [i - 1] [j - 1] +
+ (first [i - 1] == second [j - 1] ? 0 : 1)] . min
+ end
+ #
+ # Release memory
+ #
+ if i > 1
+ distances [i - 1] = nil
+ end
+ end
+ return distances [n] [m]
+end
+
+ARGF . each_line do |_|
+ words = (_ . split (/\s+/)) . grep (/\S/)
+ puts (LevenshteinDistance words [0], words [1])
+end