diff options
| author | rir <rirans@comcast.net> | 2024-01-07 23:19:33 -0500 |
|---|---|---|
| committer | rir <rirans@comcast.net> | 2024-01-07 23:19:33 -0500 |
| commit | 6411eefd95c593fbd2f29fa03930520efd4c0fa5 (patch) | |
| tree | 4fe7fbc1eca84311efb72d708fd8763b8d5e0653 | |
| parent | b24ead8163415d30dc65e98fef3599f306f5ef35 (diff) | |
| download | perlweeklychallenge-club-6411eefd95c593fbd2f29fa03930520efd4c0fa5.tar.gz perlweeklychallenge-club-6411eefd95c593fbd2f29fa03930520efd4c0fa5.tar.bz2 perlweeklychallenge-club-6411eefd95c593fbd2f29fa03930520efd4c0fa5.zip | |
250
| -rw-r--r-- | challenge-250/0rir/ch-1.raku | 67 | ||||
| -rw-r--r-- | challenge-250/0rir/ch-2.raku | 79 |
2 files changed, 146 insertions, 0 deletions
diff --git a/challenge-250/0rir/ch-1.raku b/challenge-250/0rir/ch-1.raku new file mode 100644 index 0000000000..b6c021855e --- /dev/null +++ b/challenge-250/0rir/ch-1.raku @@ -0,0 +1,67 @@ +#!/usr/bin/env raku +# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉ ≡ ≢ « » ∴ +use v6; +use lib $?FILE.IO.cleanup.parent(2).add("lib"); +use Test; + +=begin comment +Task 1: Smallest Index +Submitted by: Mohammad S Anwar +You are given an array of integers, @ints. + +Write a script to find the smallest index i such that i mod 10 == $ints[i] otherwise return -1. + +Example 1 +Input: @ints = (0, 1, 2) +Output: 0 + +i=0: 0 mod 10 = 0 == $ints[0]. +i=1: 1 mod 10 = 1 == $ints[1]. +i=2: 2 mod 10 = 2 == $ints[2]. +All indices have i mod 10 == $ints[i], so we return the smallest index 0. +Example 2 +Input: @ints = (4, 3, 2, 1) +Output: 2 + +i=0: 0 mod 10 = 0 != $ints[0]. +i=1: 1 mod 10 = 1 != $ints[1]. +i=2: 2 mod 10 = 2 == $ints[2]. +i=3: 3 mod 10 = 3 != $ints[3]. +2 is the only index which has i mod 10 == $ints[i]. +Example 3 +Input: @ints = (1, 2, 3, 4, 5, 6, 7, 8, 9, 0) +Output: -1 +Explanation: No index satisfies i mod 10 == $ints[i]. +=end comment + +my @Test = + 10, (0, 1, 2), 0, + 10, (4, 3, 2, 1), 2, + 10, (1, 2, 3, 4, 5, 6, 7, 8, 9, 0), -1, + 2, ( 5, 1, 3), 1, + 7, ( 0, 7, 7, 7), 0, + 7, ( 7, 7, 7, 7, 7, 5), 5, + 100, ( 4, 4, 4, 4, 4, 4), 4, + -1, ( -1,-1,-1,), -1, + -10,(), -1, +; +plan @Test ÷ 3; +@Test[1][0].WHAT.say; + +multi func( Int $int where $int < 0, $list ) { -1 }; +multi func( Int $int, $list -->Int:D) { + my $min = $list.pairs.grep( { .key % $int == .value}).min; + $min ~~ Pair ?? $min.value !! -1; +} + +for @Test -> $int, $list, $exp { + is func($int, $list), $exp, + sprintf "%3i <- %3i R%% %s", $exp, $int, $list.raku; +} + +done-testing; +my @int = 4,3,2,1; + +say "\nInput: @int = (@int[])\nOutput: &func(10,@int)"; +exit; + diff --git a/challenge-250/0rir/ch-2.raku b/challenge-250/0rir/ch-2.raku new file mode 100644 index 0000000000..43e7d2b23a --- /dev/null +++ b/challenge-250/0rir/ch-2.raku @@ -0,0 +1,79 @@ +#!/usr/bin/env raku +# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉ ≡ ≢ « » ∴ +use v6; +use lib $?FILE.IO.cleanup.parent(2).add("lib"); +use Test; + +=begin comment +250-2: Alphanumeric String Value Submitted by: Mohammad S Anwar +You are given an array of alphanumeric strings. + +Write a script to return the maximum value of alphanumeric string in the +given array. + +The value of alphanumeric string can be defined as + +a) The numeric representation of the string in base 10 if it is made up of digits only. +b) otherwise the length of the string + +Example 1 +Input: @alphanumstr = ("perl", "2", "000", "python", "r4ku") +Output: 6 + +"perl" consists of letters only so the value is 4. +"2" is digits only so the value is 2. +"000" is digits only so the value is 0. +"python" consits of letters so the value is 6. +"r4ku" consists of letters and digits so the value is 4. +Example 2 +Input: @alphanumstr = ("001", "1", "000", "0001") +Output: 1 + +=end comment + +my @Test = + ("001", "1", "000", "0001"), 1, + ("perl", "2", "000", "python", "r4ku"), 6, + ("perl", "200", "000", "python", "r4ku"), 200, + ("me", "1", "001"), 2, +; +my @Die = + (), + (""), + ("", ""), + ( "" xx 10).List, +; + +plan @Die + @Test ÷ 2; + +multi func( $a where * ~~ Empty ) { die "Empty list." } +multi func( $a where * eqv '' ) { die "Empty string." } +multi func( $a --> Int) { + # my $r = ($a».&{ $_ ~~ / ^ \d+ $ / ?? .Int !! .chars }).max; + # $r == -Inf ?? Int !! $r; + my $r + = ( $a».&{ + when / ^ <:L>+ $ / { .chars } + when / ^ <:Nd>+ $ / { given .Int { + when -Inf { Int } + $_; + } + } + when '' { die "Empty string." } + }).max; +} + +for @Test -> $in, $exp { + is func($in), $exp, ($exp // "(Int)") ~ " <- $in"; +} +for @Die -> $in { + dies-ok { func($in) }, "Dies with: $in.raku()"; +} + +done-testing; +my @alphanumstr = ("perl", "2", "000", "python", "r4ku"); + +say "\nInput: @alphanumstr = @alphanumstr.raku()\nOutput: ", + &func( @alphanumstr); +exit; + |
