aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAbigail <abigail@abigail.be>2021-01-19 20:22:03 +0100
committerAbigail <abigail@abigail.be>2021-01-19 20:22:03 +0100
commit8ed013c57480f076ff5da48f18b2ca86a86c4bba (patch)
treeff87bed9e4715725524e14245b2ae0fa0f624029
parent8efd5eca4066257d8d39aa1f25481b09e8bc7689 (diff)
downloadperlweeklychallenge-club-8ed013c57480f076ff5da48f18b2ca86a86c4bba.tar.gz
perlweeklychallenge-club-8ed013c57480f076ff5da48f18b2ca86a86c4bba.tar.bz2
perlweeklychallenge-club-8ed013c57480f076ff5da48f18b2ca86a86c4bba.zip
Ruby solution for week 96, part 2
-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