aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2023-04-23 23:22:23 +0100
committerGitHub <noreply@github.com>2023-04-23 23:22:23 +0100
commit2288bd049f6999a145fe3b3b09bac09e6079dccb (patch)
tree9d9b4f8002fc274ea7364d0f3b1ba46a8600661d
parentfbb03a72e34eee9d61362201f730464d8a6910b0 (diff)
parentb6227b6cf3b0f0860a85211bbfa23f64d9ce0905 (diff)
downloadperlweeklychallenge-club-2288bd049f6999a145fe3b3b09bac09e6079dccb.tar.gz
perlweeklychallenge-club-2288bd049f6999a145fe3b3b09bac09e6079dccb.tar.bz2
perlweeklychallenge-club-2288bd049f6999a145fe3b3b09bac09e6079dccb.zip
Merge pull request #7957 from wambash/challenge-week-213
solutions week 213
-rw-r--r--challenge-213/wambash/raku/ch-1.raku17
-rw-r--r--challenge-213/wambash/raku/ch-2.raku43
2 files changed, 60 insertions, 0 deletions
diff --git a/challenge-213/wambash/raku/ch-1.raku b/challenge-213/wambash/raku/ch-1.raku
new file mode 100644
index 0000000000..362d391bd1
--- /dev/null
+++ b/challenge-213/wambash/raku/ch-1.raku
@@ -0,0 +1,17 @@
+#!/usr/bin/env raku
+
+sub fun-sort (+@list) {
+ @list.sort: {$_ % 2 => $_}
+}
+
+multi MAIN (Bool :test($)!) {
+ use Test;
+ is fun-sort(1..6),(2,4,6,1,3,5);
+ is fun-sort(1,2),(2,1);
+ is fun-sort(1),(1);
+ done-testing;
+}
+
+multi MAIN (*@list) {
+ say fun-sort @list
+}
diff --git a/challenge-213/wambash/raku/ch-2.raku b/challenge-213/wambash/raku/ch-2.raku
new file mode 100644
index 0000000000..251f6cf38a
--- /dev/null
+++ b/challenge-213/wambash/raku/ch-2.raku
@@ -0,0 +1,43 @@
+#!/usr/bin/env raku
+
+use v6.e.*;
+
+sub make-graph (+@routes) {
+ @routes
+ andthen |$_,|.map: *.reverse
+ andthen .map: |*.rotor: 2 => -1
+ andthen .classify: *.[0], as => *.[1]
+}
+
+sub step-depth (+@route, :%graph) {
+ %graph{@route.tail}
+ andthen .grep: * ∉ @route
+ andthen .map: { |@route, $_ }\
+ andthen .Slip
+}
+
+
+sub shortest-route (+@routes,:$source!,:$destination!) {
+ my %graph = make-graph @routes;
+ (($source,),), { .map: &step-depth.assuming: :%graph } ... :!elems
+ andthen .map: *.cache.grep: *.tail eqv $destination
+ andthen .first: *.elems
+}
+
+multi MAIN (Bool :test($)!) {
+ use Test;
+ my %graph := make-graph( (1,2,3,5), (2, 4) );
+ is %graph.gist, :{1 => [2], 2 => [3,4,1], 3 => [5,2], 4 => [2], 5 => [3]}.gist;
+ is step-depth( 1,2,3, :%graph), (1,2,3,5);
+ is step-depth( 1, :%graph), (1,2);
+ is step-depth( 2,1, :%graph).elems, 0;
+ is-deeply step-depth( 1,2, :%graph), slip((1,2,3),(1,2,4));
+ is shortest-route((1,2,6),(5,6,7),:1source,:7destination),(1,2,6,7);
+ is shortest-route((1,2,3),(4,5,6,7),:1source,:7destination),Nil;
+ is shortest-route([1,2,3], [4,5,6], [3,8,9], [7,8], :1source,:7destination), (1,2,3,8,7);
+ done-testing;
+}
+
+multi MAIN (*@routes,Str :s(:$source)!,Str :d(:$destination)!) {
+ say shortest-route |@routes.map( *.split(',').cache ), :source($source.Str), :destination($destination.Str)
+}