diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2023-04-23 23:25:51 +0100 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2023-04-23 23:25:51 +0100 |
| commit | 7e15962220dc92871be55d8916c4dc520c5bcedd (patch) | |
| tree | 9798977364b2067343d84faa90abc62ff40032e4 | |
| parent | d9124c9362d5ae722a744018239dd6d068936235 (diff) | |
| parent | 0d32ddc16932a6e9c3011bef128e5a735ce3b34e (diff) | |
| download | perlweeklychallenge-club-7e15962220dc92871be55d8916c4dc520c5bcedd.tar.gz perlweeklychallenge-club-7e15962220dc92871be55d8916c4dc520c5bcedd.tar.bz2 perlweeklychallenge-club-7e15962220dc92871be55d8916c4dc520c5bcedd.zip | |
Merge branch 'master' of https://github.com/manwar/perlweeklychallenge-club
| -rw-r--r-- | challenge-213/0rir/raku/ch-1.raku | 43 | ||||
| -rw-r--r-- | challenge-213/wambash/raku/ch-1.raku | 17 | ||||
| -rw-r--r-- | challenge-213/wambash/raku/ch-2.raku | 43 |
3 files changed, 103 insertions, 0 deletions
diff --git a/challenge-213/0rir/raku/ch-1.raku b/challenge-213/0rir/raku/ch-1.raku new file mode 100644 index 0000000000..57f4091f40 --- /dev/null +++ b/challenge-213/0rir/raku/ch-1.raku @@ -0,0 +1,43 @@ +#!/usr/bin/env raku +# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉ ≡ ≢ « » ∴ +use v6.d; +use Test; + +=begin comment +213-1: Fun Sort Submitted by: Mohammad S Anwar +Given a list of positive integers, sort the all even integers first then +all odds in ascending order. + +Example 1 +Input: @list = (1,2,3,4,5,6) +Output: (2,4,6,1,3,5) +Example 2 +Input: @list = (1,2) +Output: (2,1) +Example 3 +Input: @list = (1) +Output: (1) +=end comment + +my @Test = + # @in @exp + [], [], + [1,2,3,4,5,6], [2,4,6,1,3,5], + [1,2], [2,1], + [1], [1], + [100,100,99,9,10,2,3], [2,10,100,100,3,9,99], + [1,2,3,-9,10], [2,10,-9,1,3], + [ -100,100,-99,-9,-10,2,3], [ -100,-10,2,100,-99,-9,3], + [2,5,4,3,1,1,3,10,22,44,66,88,110,5,9,7], + [2,4,10,22,44,66,88,110,1,1,3,3,5,5,7,9], +; +plan @Test ÷ 2; + +for @Test -> @in, @exp { + is @in.sort( { $_ % 2, $_}), @exp, "@in[] --> @exp[]"; +} +my @list = 100,100,99,9,10,2,3; +say "\nInput: @list = (@list.join(',')) +Output: (@list.sort( { $_ % 2, $_}).join(','))"; +exit; + 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) +} |
