diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-01-14 22:51:23 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-01-14 22:51:23 +0000 |
| commit | 3137a91502cba8caff6b8c5c32781d5c0ea68c00 (patch) | |
| tree | 8252aaa5609cf9bda2f110e8d39cf3a510d3c2fa | |
| parent | 3ac4bb22d7fe527c9e6b6a5476770eddf4136bb1 (diff) | |
| parent | 0b41294e70fc0f3c38745c08ecf4aff0639598db (diff) | |
| download | perlweeklychallenge-club-3137a91502cba8caff6b8c5c32781d5c0ea68c00.tar.gz perlweeklychallenge-club-3137a91502cba8caff6b8c5c32781d5c0ea68c00.tar.bz2 perlweeklychallenge-club-3137a91502cba8caff6b8c5c32781d5c0ea68c00.zip | |
Merge pull request #9399 from 0rir/251
251
| -rw-r--r-- | challenge-251/0rir/raku/ch-1.raku | 79 | ||||
| -rwxr-xr-x | challenge-251/0rir/raku/ch-2.raku | 80 |
2 files changed, 159 insertions, 0 deletions
diff --git a/challenge-251/0rir/raku/ch-1.raku b/challenge-251/0rir/raku/ch-1.raku new file mode 100644 index 0000000000..3b6804c526 --- /dev/null +++ b/challenge-251/0rir/raku/ch-1.raku @@ -0,0 +1,79 @@ +#!/usr/bin/env raku +# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉ ≡ ≢ « » ∴ +use v6d; +use Test; + +=begin comment +251-1: Concatenation Value Submitted by: Mohammad S Anwar +You are given an array of integers, @ints. +Write a script to find the concatenation value of the given array. + +The concatenation of two numbers is the number formed by concatenating their numerals. + +For example, the concatenation of 10, 21 is 1021. +The concatenation value of @ints is initially equal to 0. +Perform this operation until @ints becomes empty: + +If there exists more than one number in @ints, pick the first element +and last element in @ints respectively and add the value of their +concatenation to the concatenation value of @ints, then delete the +first and last element from @ints. + +If one element exists, add its value to the concatenation value of +@ints, then delete it. + +Example 1 +Input: @ints = (6, 12, 25, 1) +Output: 1286 + +1st operation: concatenation of 6 and 1 is 61 +2nd operation: concaternation of 12 and 25 is 1225 + +Concatenation Value => 61 + 1225 => 1286 +Example 2 +Input: @ints = (10, 7, 31, 5, 2, 2) +Output: 489 + +1st operation: concatenation of 10 and 2 is 102 +2nd operation: concatenation of 7 and 2 is 72 +3rd operation: concatenation of 31 and 5 is 315 + +Concatenation Value => 102 + 72 + 315 => 489 +Example 3 +Input: @ints = (1, 2, 10) +Output: 112 + +1st operation: concatenation of 1 and 10 is 110 +2nd operation: only element left is 2 + +Concatenation Value => 110 + 2 => 112 + +=end comment + +my @Test = + (2, 1, 10), 211, + (1, 2, 10), 112, + (10, 7, 31, 5, 2, 2), 489, + (6, 12, 25, 1), 1286, +; +plan @Test ÷ 2; + +sub cat-head-tail( @a is copy --> Int) { + my $sum; + while @a > 1 { $sum += @a.pop R~ @a.shift; } + if @a == 1 { $sum += @a.pop } + $sum +} + +for @Test -> $in, $exp { + is cat-head-tail($in), $exp, "$exp <- $in[]"; +} + +done-testing; + +my @int = 1, 7,4,3; + +say "\nInput: @int = @int.raku()\nOutput: &cat-head-tail( @int)"; + +exit; + diff --git a/challenge-251/0rir/raku/ch-2.raku b/challenge-251/0rir/raku/ch-2.raku new file mode 100755 index 0000000000..329adadc43 --- /dev/null +++ b/challenge-251/0rir/raku/ch-2.raku @@ -0,0 +1,80 @@ +#!/home/rir/Repo/rakudo/install/bin/raku +##!/usr/bin/env raku +# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉ ≡ ≢ « » ∴ +use v6.e.PREVIEW; +use Math::Matrix; +use Test; + +say %*ENV<VERSION>; + +=begin comment +251-2: Lucky Numbers Submitted by: Mohammad S Anwar +You are given a m x n matrix of distinct numbers. +Write a script to return the lucky number, if there is one, or -1 if not. +A lucky number is an element of the matrix such that it is +the minimum element in its row and maximum in its column. + +Example 1 +Input: $matrix = [ [ 3, 7, 8], + [ 9, 11, 13], + [15, 16, 17] ]; +Output: 15 + +15 is the only lucky number since it is the minimum in its row +and the maximum in its column. +Example 2 +Input: $matrix = [ [ 1, 10, 4, 2], + [ 9, 3, 8, 7], + [15, 16, 17, 12] ]; +Output: 12 +Example 3 +Input: $matrix = [ [7 ,8], + [1 ,2] ]; +Output: 7 +=end comment + + +my @Test = + 12, [ [ 1, 10, 4, 2], + [ 9, 3, 8, 7], + [15, 16, 17, 12] ], + 15, [ [ 3, 7, 8], + [ 9, 11, 13], + [15, 16, 17] ], + 7, [ [7 ,8], + [1 ,2] ], +; +plan @Test ÷ 2; + +sub ____ (+@list) { gather @list.deepmap: *.take } # move to Flatland + +the-lucky-number-is(); + +sub the-lucky-number-is( @a = @Test[1] --> Int) { + my @w = @a.&____.sort; +say @w.raku; + die 'Non-distinct data' unless @w.sort eqv @w.unique.sort; + + my @lucky; + + my @row-min = @a.map( *.min( :p)[0]); # ?????? + for @row-min -> $p { +say $p; +say "A col-max ", (@a[ 0..^@a, $p.key]).max; + my $col-max = @a[ 0..^@a, $p.key].max; + @lucky.push( $p.value) if $col-max == $p.value; + } + say @lucky.raku; + return @lucky; +} +=finish + +for @Test -> $exp, $in { + is the-lucky-number-is($in), $exp, "$exp <- $in"; +} + +done-testing; +my @X = X; + +exit; + |
