diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2023-02-17 19:55:26 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-02-17 19:55:26 +0000 |
| commit | 93f949b307ab0055fb1e60f391c424d0760c5372 (patch) | |
| tree | 90d5f8c415e6f878bc710459297dff597ccfb8f6 | |
| parent | 0c8c2b6beb3619715b058981acdde4bd06922aab (diff) | |
| parent | 9d4724c13ae67bdc3669bf36ee9e75e88f2e1e84 (diff) | |
| download | perlweeklychallenge-club-93f949b307ab0055fb1e60f391c424d0760c5372.tar.gz perlweeklychallenge-club-93f949b307ab0055fb1e60f391c424d0760c5372.tar.bz2 perlweeklychallenge-club-93f949b307ab0055fb1e60f391c424d0760c5372.zip | |
Merge pull request #7584 from 0rir/new
204
| -rw-r--r-- | challenge-202/0rir/raku/ch-2.raku | 99 | ||||
| -rw-r--r-- | challenge-204/0rir/raku/ch-1.raku | 55 | ||||
| -rw-r--r-- | challenge-204/0rir/raku/ch-2.raku | 89 |
3 files changed, 243 insertions, 0 deletions
diff --git a/challenge-202/0rir/raku/ch-2.raku b/challenge-202/0rir/raku/ch-2.raku new file mode 100644 index 0000000000..05dc2c8940 --- /dev/null +++ b/challenge-202/0rir/raku/ch-2.raku @@ -0,0 +1,99 @@ +#!/usr/bin/env raku +# :vim ft=raku sw=4 expandtab # 🦋 ∅ ≡ ∩ ≢ ∈ «  » ∴ +use v6.d; +use lib $?FILE.IO.parent(2).add("lib"); +use Test; + +=begin comment + +202-2: Widest Valley Submitted by: E. Choroba +Given a profile as a list of altitudes, return the leftmost widest valley. +A valley is defined as a subarray of the profile consisting of two parts: +the first part is non-increasing and the second part is non-decreasing. +Either part can be empty. +=end comment + +my @Test = + { in => Empty, exp => Empty, }, + { in => [1,], exp => [1,], }, + { in => [1,5], exp => [1,5], }, + + { in => [1,2,3], exp => [1,2,3], }, + { in => [3,2,1], exp => [3,2,1], }, + { in => [1,2,3,3,2,1], exp => [1,2,3,3], }, + { in => [2,3,3,2,1], exp => [3,3,2,1], }, + + { in => [3,2,1,1,2,3], exp => [3,2,1,1,2,3], }, + { in => [3,2,1,2,3], exp => [3,2,1,2,3], }, + + { in => [1,1,1], exp => [1,1,1], }, + + { in => [5,5,2,5,5], exp => [5,5,2,5,5], }, + { in => [1,1,1,1,5,5,2,5,5], exp => [1,1,1,1,5,5], }, + { in => [5,5,2,5,5,1,1,1,1], exp => [5,5,1,1,1,1], }, + { in => [1,5,5,2,5,5,1,1,1,1], exp => [5,5,1,1,1,1], }, + { in => [1,5,5,2,5,5,1,1,1], exp => [5,5,2,5,5], }, + + { in => [9,8,13,13,2,2,15,17], exp => [13,13,2,2,15,17], }, + { in => [2,1,2,1,3], exp => [2,1,2], }, + { in => [1,3,3,2,1,2,3,3,2], exp => [3,3,2,1,2,3,3], }, + { in => [2,2,2,1,1,1,1,2,1,1,1,3], exp => [ 2,2,2,1,1,1,1,2], }, + { in => [1,2,1,1,2,2,2,2,2,1,2,1], exp => [2,1,1,2,2,2,2,2], }, + { in => [1,2,1,1,2,2,2,2,2,1,1,2,2,1], exp => [2,2,2,2,2,1,1,2,2], }, +; + +multi find-big-valley( @a where Empty ~~ * ) { [,] } +multi find-big-valley( @a where @a.end ≤ 1) { @a } + +multi find-big-valley( @a --> Array) { + # COULDDO bail when best > remainder + my ($h, $t, $hi-score, @best) = 0 xx 5; + loop { + ++$t while $t < @a.end and @a[$t] == @a[$t+1]; # Cross 1st flat area. + if $t == @a.end { + score( $h,$t); + last; + } + if @a[$t] < @a[$t+1] { ### Started mid valley. + ascend-slope; + score-done-or-reset-for-more; + } else { ### Start looping valleys. + loop { + descend-slope; + ascend-slope; + score-done-or-reset-for-more; + } } } + + return [@a[ @best.head..@best.tail]] ; + + die 'not reached, only scope contained subs follow'; + + sub score-done-or-reset-for-more () { + score( $h, $t); + $h = $t; + last if $t == @a.end; + --$h while $h > 0 and @a[$h] == @a[$h-1]; # Re-use plateau. + } + sub score( $h, $t) { + if $t-$h > $hi-score { + @best = $h,$t; $hi-score = $t-$h + } + } + sub ascend-slope() { + ++$t while $t < @a.end and @a[$t] ≤ @a[$t+1]; + } + sub descend-slope() { + ++$t while $t < @a.end and @a[$t] ≥ @a[$t+1]; + } +} + +plan +@Test; +for @Test -> %h { + is find-big-valley( %h<in>), %h<exp>; +} +done-testing; + +my @input = 3,3,2,1,1,1,2,3,3,3,3,2,1,1,1,1,1,2,2,1; +say "\n Input: ", @input.join: ', '; +say "Output: ", find-big-valley( @input).join: ', '; + diff --git a/challenge-204/0rir/raku/ch-1.raku b/challenge-204/0rir/raku/ch-1.raku new file mode 100644 index 0000000000..5a118e69f7 --- /dev/null +++ b/challenge-204/0rir/raku/ch-1.raku @@ -0,0 +1,55 @@ +#!/usr/bin/env raku +# :vim ft=raku sw=4 expandtab # 🦋 ∅ ≡ ∩ ≢ ∈ «  » ∴ +use v6.d; +use lib $?FILE.IO.parent(2).add("lib"); +use Test; + +=begin comment +204-1: Monotonic Array Submitted by: Mohammad S Anwar +Given an array of integers, find if the given array is Monotonic. +Print 1 if it is otherwise 0. + +An array is Monotonic if it is either monotone increasing or decreasing. + +Monotone increasing: for i <= j , nums[i] <= nums[j] +Monotone decreasing: for i <= j , nums[i] >= nums[j] + +Example 1 +Input: @nums = (1,2,2,3) +Output: 1 +Example 2 +Input: @nums (1,3,2) +Output: 0 +Example 3 +Input: @nums = (6,5,5,4) +Output: 1 +=end comment + +my @Test = + (Empty) => Bool, + (1,2,2,3) => True, + (1,3,2) => False, + (6,5,5,4) => True, +; + +multi monotonic( Empty ) { Bool } +multi monotonic( $list --> Bool) { + my $l = $list.sort.List; + $list ~~ $l or $list ~~ $l.reverse; +} + +plan +@Test; +for @Test -> ( :key($in), :value($exp)) { + is monotonic($in), $exp, "$in.raku() -> monotonic $exp.gist()"; +} +done-testing; + + +print "\n"; +my @num = (1,2,3,4,5,6,7,8,9,10,11,11,11,1,12,13,14,15,15,15,15,16,17,19), + (1,2,3,4,5,6,7,8,9,10,11,11,11,12,13,14,15,15,15,15,16,17,19); + +for 0..^@num { +say "Input: @num = @num[$_] +Output: &monotonic(@num[$_]).Int()"; +} diff --git a/challenge-204/0rir/raku/ch-2.raku b/challenge-204/0rir/raku/ch-2.raku new file mode 100644 index 0000000000..e9d49fd8d1 --- /dev/null +++ b/challenge-204/0rir/raku/ch-2.raku @@ -0,0 +1,89 @@ +#!/usr/bin/env raku +# :vim ft=raku sw=4 expandtab # 🦋 ∅ ≡ ∩ ≢ ∈ «  » ∴ +use v6.d; +use lib $?FILE.IO.parent(2).add("lib"); +use Test; + +=begin comment +204-2: Reshape Matrix Submitted by: Mohammad S Anwar + +Given a matrix (m x n) and two integers (r) and (c), reshape that matrix in form +(r x c) with the original value in the given matrix. If you can’t reshape print 0. + +Example 1 +Input: [ 1 2 ] + [ 3 4 ] + + $matrix = [ [ 1, 2 ], [ 3, 4 ] ] + $r = 1 + $c = 4 + +Output: [ 1 2 3 4 ] +Example 2 +Input: [ 1 2 3 ] + [ 4 5 6 ] + + $matrix = [ [ 1, 2, 3 ] , [ 4, 5, 6 ] ] + $r = 3 + $c = 2 + +Output: [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ] + + [ 1 2 ] + [ 3 4 ] + [ 5 6 ] +Example 3 +Input: [ 1 2 ] + + $matrix = [ [ 1, 2 ] ] + $r = 3 + $c = 2 + +Output: 0 +=end comment + +my @Test = + [ [ [1,2],[3,4]], 1, 4, [1,2,3,4] ], + [ [ [1,2,3],[4,5,6]], 3, 2, [ [1,2],[3,4],[5,6]] ], + + [ [ 1,2 ], 3, 2, Array ], + [ [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18], 3, 5, Array ], + [ [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18], 5, 3, Array ], + + [ [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18], 1, 18, + [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18] + ], + [ [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18], 2, 9, + [[1,2,3,4,5,6,7,8,9],[10,11,12,13,14,15,16,17,18]] + ], + [ [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18], 3, 6, + [[1,2,3,4,5,6],[7,8,9,10,11,12],[13,14,15,16,17,18]] + ], + [ [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18], 6, 3, + [[1,2,3],[4,5,6],[7,8,9],[10,11,12],[13,14,15],[16,17,18]] + ], + [ [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18], 9, 2, + [[1,2],[3,4],[5,6],[7,8],[9,10],[11,12],[13,14],[15,16],[17,18]] + ], + [ [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18], 18, 1, + [[1,],[2,],[3,],[4,],[5,],[6,],[7,],[8,],[9,],[10,],[11,],[12,],[13,],[14,],[15,],[16,],[17,],[18,]] + ], +; + +enum Fields < Matrix Rows Cols Exp >; + +multi reshape-matrix( @matrix, $rows, $cols --> Array ) { + my @m = @matrix; + @m = @m».List.flat; + return Array if @m.elems ≠ $rows × $cols; + (@matrix».List.flat.rotor( $rows)».Array).Array; +} + +plan +@Test; +for @Test -> @t { + is reshape-matrix(@t[Matrix], @t[Rows], @t[Cols]), @t[Exp]; +} +done-testing; + + + |
