aboutsummaryrefslogtreecommitdiff
path: root/challenge-096
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-02-23 19:52:18 +0000
committerGitHub <noreply@github.com>2021-02-23 19:52:18 +0000
commitba6b7e08de27b356170de2e1158a76d73b971721 (patch)
treedc2bbf134914bb87e0f950f9e0fa12e4c9d9dc28 /challenge-096
parent349d250703c8d9c3bf670b0491e0278c9302e7eb (diff)
parent3cc47d00aee13d50c872499cecf574539a4cbe2a (diff)
downloadperlweeklychallenge-club-ba6b7e08de27b356170de2e1158a76d73b971721.tar.gz
perlweeklychallenge-club-ba6b7e08de27b356170de2e1158a76d73b971721.tar.bz2
perlweeklychallenge-club-ba6b7e08de27b356170de2e1158a76d73b971721.zip
Merge pull request #3607 from seaker/master
challenge 101, Feng Chang's Raku solutions
Diffstat (limited to 'challenge-096')
-rwxr-xr-xchallenge-096/feng-chang/ch-1.raku4
-rwxr-xr-xchallenge-096/feng-chang/ch-2.raku27
2 files changed, 31 insertions, 0 deletions
diff --git a/challenge-096/feng-chang/ch-1.raku b/challenge-096/feng-chang/ch-1.raku
new file mode 100755
index 0000000000..a8271c123a
--- /dev/null
+++ b/challenge-096/feng-chang/ch-1.raku
@@ -0,0 +1,4 @@
+#!/bin/env raku
+
+multi MAIN(Str:D $S) { put $S.words.reverse.join(' ') }
+multi MAIN(*@S) { MAIN(@S.join(' ')) }
diff --git a/challenge-096/feng-chang/ch-2.raku b/challenge-096/feng-chang/ch-2.raku
new file mode 100755
index 0000000000..a84a74b7d7
--- /dev/null
+++ b/challenge-096/feng-chang/ch-2.raku
@@ -0,0 +1,27 @@
+#!/bin/env raku
+
+sub levenshtein-distance(Str:D $s, Str:D $t) {
+ my @S = $s.comb; my $m = @S.elems;
+ my @T = $t.comb; my $n = @T.elems;
+
+ my Array @a = (0 xx ($m+1)).Array xx ($n+1);
+ @a[0;1..$m] = 1..$m;
+ @a[1..$n;0] = 1..$n;
+
+ for ^$n -> $j {
+ for ^@S.elems -> $i {
+ my $subst-cost = @S[$i] eq @T[$j] ?? 0 !! 1;
+
+ @a[$j+1;$i+1] = min(
+ @a[$j;$i+1] + 1, # deletion
+ @a[$j+1;$i] + 1, # insertion
+ @a[$j;$i] + $subst-cost # substitution
+ );
+ }
+ }
+
+ @a[$n;$m], @a.item;
+}
+
+put levenshtein-distance('kitten', 'sitting')[0];
+put levenshtein-distance('Saturday', 'Sunday')[0];