diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2022-11-06 23:59:24 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-11-06 23:59:24 +0000 |
| commit | e785ac46783940b8448474bdea886d7815754fb2 (patch) | |
| tree | 596cb74719aedc3c641e90fe0d5af2af8dd8f3e0 | |
| parent | b24bae1dbcee9ca240c7a04945301a5d819eba9e (diff) | |
| parent | 7a1870cf78f7c790fbef97f98f5982a7581cdb1b (diff) | |
| download | perlweeklychallenge-club-e785ac46783940b8448474bdea886d7815754fb2.tar.gz perlweeklychallenge-club-e785ac46783940b8448474bdea886d7815754fb2.tar.bz2 perlweeklychallenge-club-e785ac46783940b8448474bdea886d7815754fb2.zip | |
Merge pull request #7042 from 0rir/189
189
| -rw-r--r-- | challenge-189/0rir/raku/ch-1.raku | 57 | ||||
| -rw-r--r-- | challenge-189/0rir/raku/ch-2.raku | 75 |
2 files changed, 132 insertions, 0 deletions
diff --git a/challenge-189/0rir/raku/ch-1.raku b/challenge-189/0rir/raku/ch-1.raku new file mode 100644 index 0000000000..5e215d0747 --- /dev/null +++ b/challenge-189/0rir/raku/ch-1.raku @@ -0,0 +1,57 @@ +#!/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 +189-1: Greater Character Submitted by: Mohammad S Anwar + +You are given an array of characters (a..z) and a target character. +Find the smallest character in the given array lexicographically +greater than the target character. + +Example 1 +Input: @array = qw/e m u g/, $target = 'b' +Output: e +Example 2 +Input: @array = qw/d c e f/, $target = 'a' +Output: c +Example 3 +Input: @array = qw/j a r/, $target = 'o' +Output: r +Example 4 +Input: @array = qw/d c a f/, $target = 'a' +Output: c +Example 5 +Input: @array = qw/t g a l/, $target = 'v' +Output: v +=end comment + +my @Test; + +sub next-gt( Str $ch, List $char ) { + my $r = (@$char.grep( * gt $ch )).min( :by( &infix:<~~> )); + $r ~~ ∞ ?? $ch !! $r; +} + +sub MAIN ( ) { + +my @Test = + { array => qw/e m u g/, target => 'b', exp => 'e',}, + { array => qw/d c e f/, target => 'a', exp => 'c',}, + { array => qw/j a r/, target => 'o', exp => 'r',}, + { array => qw/d c a f/, target => 'a', exp => 'c',}, + { array => qw/t g a l/, target => 'v', exp => 'v',}, + { array => qw//, target => 'v', exp => 'v',}, + ; + + plan +@Test; + for @Test -> %t { + is next-gt( %t<target>, @(%t<array>)), %t<exp>, + "%t<target> @(%t<array>).raku() -> %t<exp>"; + } + done-testing; + exit; +} + diff --git a/challenge-189/0rir/raku/ch-2.raku b/challenge-189/0rir/raku/ch-2.raku new file mode 100644 index 0000000000..364f1db8f4 --- /dev/null +++ b/challenge-189/0rir/raku/ch-2.raku @@ -0,0 +1,75 @@ +#!/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 +189-2: Array Degree Submitted by: Mohammad S Anwar + +Given an array of 2 or more non-negative integers, find the smallest slice, +i.e. contiguous subarray of the original array, having the degree of the given array. + +The degree of an array is the maximum frequency of an element in the array. + +Example 1 +Input: @array = (1, 3, 3, 2) +Output: (3, 3) + +The degree of the given array is 2. +The possible subarrays having the degree 2 are as below: +(3, 3) +(1, 3, 3) +(3, 3, 2) +(1, 3, 3, 2) + +And the smallest of all is (3, 3). +Example 1 +Input: @array = (1, 3, 3, 2) +Output: (3, 3) +Example 2 +Input: @array = (1, 2, 1, 3) +Output: (1, 2, 1) +Example 3 +Input: @array = (1, 3, 2, 1, 2) +Output: (2, 1, 2) +Example 4 +Input: @array = (1, 1, 2, 3, 2) +Output: (1, 1) +Example 5 +Input: @array = (2, 1, 2, 1, 1) +Output: (1, 2, 1, 1) + +=end comment + +sub shortest-degree-slice( @list --> Array ) { + my @by-ct = @list.BagHash.antipairs; + my $most-common-qty = max( map( *.key, @by-ct)); + my @common-value = map( *.value, grep( *.key == $most-common-qty, @by-ct)); + my ($head, $tail) = 0, @list.end; + for @common-value.reverse -> $e { + my $hd = @list.first( * == $e, :k); + my $tl = @list.first( * == $e, :end, :k); + ($head, $tail) = ($hd, $tl) if $tail - $head ≥ $tl - $hd; + } + @list[ $head .. $tail].Array; +} + +sub MAIN() { + my @Test = + { array => (1, 2, 1, 2, 1, 1), :exp(1, 2, 1, 2, 1, 1), }, + { array => (1, 3, 3, 2), :exp(3, 3), }, + { array => (1, 2, 1, 3), :exp(1, 2, 1), }, + { array => (1, 3, 2, 1, 2), :exp(2, 1, 2), }, + { array => (1, 1, 2, 3, 2), :exp(1, 1), }, + { array => (2, 1, 2, 1, 1), :exp(1, 2, 1, 1), }, + { array => (2, 2, 1, 1, 3), :exp(2, 2), }, + ; + + plan +@Test; + for @Test -> %t { + is shortest-degree-slice( %t<array>), %t<exp>, "%t<array> -> %t<exp>"; + } + done-testing; + exit; +} |
