aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJan Krňávek <Jan.Krnavek@gmail.com>2021-01-24 10:33:02 +0100
committerJan Krňávek <Jan.Krnavek@gmail.com>2021-01-24 10:33:02 +0100
commit7094faca22638ec0316ea07fb381684faf29fb1e (patch)
tree41a12fb430fac1c910568ebd6045c13869ee29f1
parent54b6e0bcb79778b08fe097ac3e6d2a4931d74ae2 (diff)
downloadperlweeklychallenge-club-7094faca22638ec0316ea07fb381684faf29fb1e.tar.gz
perlweeklychallenge-club-7094faca22638ec0316ea07fb381684faf29fb1e.tar.bz2
perlweeklychallenge-club-7094faca22638ec0316ea07fb381684faf29fb1e.zip
solutions week 096
-rw-r--r--challenge-096/wambash/raku/ch-1.raku16
-rw-r--r--challenge-096/wambash/raku/ch-2.raku52
2 files changed, 68 insertions, 0 deletions
diff --git a/challenge-096/wambash/raku/ch-1.raku b/challenge-096/wambash/raku/ch-1.raku
new file mode 100644
index 0000000000..85dc36ce68
--- /dev/null
+++ b/challenge-096/wambash/raku/ch-1.raku
@@ -0,0 +1,16 @@
+#!/usr/bin/env raku
+
+sub reverse-words ( $s ) {
+ ~$s.words.reverse
+}
+
+multi MAIN ( Bool :$test! ) {
+ use Test;
+ is reverse-words('The Weekly Challenge'), 'Challenge Weekly The';
+ is reverse-words(' Perl and Raku are part of the same family '), 'family same the of part are Raku and Perl';
+ done-testing;
+}
+
+multi MAIN ( *@s ) {
+ say reverse-words ~@s
+}
diff --git a/challenge-096/wambash/raku/ch-2.raku b/challenge-096/wambash/raku/ch-2.raku
new file mode 100644
index 0000000000..0c219047d6
--- /dev/null
+++ b/challenge-096/wambash/raku/ch-2.raku
@@ -0,0 +1,52 @@
+#!/usr/bin/env raku
+
+multi edit-distance-iter ( @a, @b where :!elems, :$distance = 0, ) {
+ \( Empty,Empty, distance => $distance + @a.elems, :end )
+}
+
+multi edit-distance-iter ( @a where :!elems, @b, :$distance = 0, ) {
+ \( Empty,Empty, distance => $distance + @b.elems, :end )
+}
+
+multi edit-distance-iter ( @a, @b, :$distance = 0, ) {
+ (
+ \(@a.skip($_), @b, distance => $distance + $_ ) with @a.first(* eqv @b.head,:k);
+ \(@a, @b.skip($_), distance => $distance + $_ ) with @b.first(* eqv @a.head,:k);
+ \(@a.skip, @b.skip, distance => $distance + 1);
+ )
+}
+
+multi edit-distance-iter ( @a, @b where { (@a.head eqv @b.head)}, :$distance = 0 ) {
+ \(@a.skip, @b.skip, :$distance )
+}
+
+multi edit-distance-iter ( Capture $c ) {
+ edit-distance-iter |$c
+}
+
+multi edit-distance-iter ( @a ) {
+ (slip(edit-distance-iter @a.head), |@a.skip).sort(*.<distance>)
+}
+
+multi edit-distance (@a,@b, :$distance = 0 ) {
+ [\(@a,@b, :$distance),], *.&edit-distance-iter ... *.cache.head.<end>
+ andthen .tail.head.<distance>
+}
+
+multi MAIN ( $a, $b ) {
+ say edit-distance $a.comb, $b.comb
+}
+
+multi MAIN (Bool :$test!) {
+ use Test;
+ is edit-distance-iter('1234'.comb, Empty).<distance>, 4;
+ is edit-distance-iter('1234'.comb, Empty).<end>, True;
+ is edit-distance-iter(Empty, '12345'.comb).<distance>, 5;
+ is edit-distance-iter(Empty, Empty).<distance>, 0;
+ is edit-distance-iter(edit-distance-iter('1234'.comb,'122'.comb)).[1],2;
+ is edit-distance( 1234.comb, 34561.comb), 5;
+ is edit-distance(1234.comb, 1234.comb), 0;
+ is edit-distance('kitten'.comb, 'sitting'.comb),3;
+ is edit-distance('sunday'.comb,'monday'.comb),2;
+ done-testing;
+}