aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKang-min Liu <gugod@gugod.org>2021-01-20 09:31:52 +0900
committerKang-min Liu <gugod@gugod.org>2021-01-20 09:31:52 +0900
commit875a4d6a86955b958217169be7209f1f079e98c0 (patch)
treee768de3e5922b4375a562b11fa49d8d785891057
parent770c854468fbad1c785769367d1c5142981cc81b (diff)
downloadperlweeklychallenge-club-875a4d6a86955b958217169be7209f1f079e98c0.tar.gz
perlweeklychallenge-club-875a4d6a86955b958217169be7209f1f079e98c0.tar.bz2
perlweeklychallenge-club-875a4d6a86955b958217169be7209f1f079e98c0.zip
a solution to pwc 096.2 in Raku. (Levinstein edit distance)
-rw-r--r--challenge-096/gugod/raku/ch-2.raku26
1 files changed, 26 insertions, 0 deletions
diff --git a/challenge-096/gugod/raku/ch-2.raku b/challenge-096/gugod/raku/ch-2.raku
new file mode 100644
index 0000000000..bf2e2a1357
--- /dev/null
+++ b/challenge-096/gugod/raku/ch-2.raku
@@ -0,0 +1,26 @@
+sub MAIN {
+ my @examples = (
+ [ 'kitten', 'sitting' ],
+ [ 'sunday', 'monday' ],
+ );
+
+ for @examples -> ($S1, $S2) {
+ say "Input: \$S1 = $S1, \$S2 = $S2";
+ say "Output: " ~ edit-distance($S1, $S2);
+ }
+}
+
+sub edit-distance (Str $S1, Str $S2 --> Int) {
+ my sub lev ($a, $b) {
+ return $a.chars if $b.chars == 0;
+ return $b.chars if $a.chars == 0;
+ return lev( $a.substr(1), $b.substr(1) ) if $a.substr(0,1) eq $b.substr(0,1);
+ return 1 + (
+ lev($a, $b.substr(1)),
+ lev($a.substr(1), $b),
+ lev($a.substr(1), $b.substr(1)),
+ ).min;
+ };
+
+ return lev($S1, $S2);
+}