diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2022-12-13 12:03:12 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-12-13 12:03:12 +0000 |
| commit | 17388ad8404cbb165b10e0f7dc122fe47e7a7773 (patch) | |
| tree | 6921bc7c71c3cdcf6a843db9876e7205422254b1 | |
| parent | 2f5a8074f53bf779820e6de2ae879b070cefa1e8 (diff) | |
| parent | 24ebe044fcc947417212a49b55aabaeac8d50695 (diff) | |
| download | perlweeklychallenge-club-17388ad8404cbb165b10e0f7dc122fe47e7a7773.tar.gz perlweeklychallenge-club-17388ad8404cbb165b10e0f7dc122fe47e7a7773.tar.bz2 perlweeklychallenge-club-17388ad8404cbb165b10e0f7dc122fe47e7a7773.zip | |
Merge pull request #7250 from 0rir/195
195
| -rw-r--r-- | challenge-195/0rir/raku/ch-1.raku | 52 | ||||
| -rw-r--r-- | challenge-195/0rir/raku/ch-2.raku | 59 |
2 files changed, 111 insertions, 0 deletions
diff --git a/challenge-195/0rir/raku/ch-1.raku b/challenge-195/0rir/raku/ch-1.raku new file mode 100644 index 0000000000..2c7de79d23 --- /dev/null +++ b/challenge-195/0rir/raku/ch-1.raku @@ -0,0 +1,52 @@ +#!/usr/bin/env raku +# :vim ft=raku sw=4 expandtab # 🦋 ∅ ≡ ∩ ≢ ∈ «» +use v6.d; +use Test; + +=begin comment +195-1: Special Integers Submitted by: Mohammad S Anwar + +An integer is special when all of its digits are unique. +Given a positive integer, $n > 0, print the count of all special integers +between 1 and $n. + +Example 1: +Input: $n = 15 +Output: 14 as except 11 all other integers between 1 and 15 are spcial. +Example 2: +Input: $n = 35 +Output: 32 as except 11, 22, 33 all others are special. +=end comment + +constant @special-int-ct = gather { + take Nil; + loop { + state ( $i, $prev) = 0, 0; + ++$i; + take $i.Str.comb.elems == $i.Str.comb.unique.elems ?? ++$prev !! $prev; + } +} + +multi MAIN ( 'test' ) { + my @Test = + { in => 15, exp => 14, }, + { in => 35, exp => 32, }, + { in => 99, exp => 90, }, + { in => 200, exp => 162, }, + { in => 180, exp => 147, }, + { in => 1_000, exp => 738, }, + { in => 10_000, exp => 5_274, }, + { in => 100_000, exp => 32_490, }, + { in => 1_000_000, exp => 168_570, }, + ; + plan +@Test; + for @Test -> %t { + is @special-int-ct[%t<in>], %t<exp>, " %t<exp> <- 1..%t<in>"; + } + done-testing; + exit; +} + +multi MAIN( $n = 180) { + say "Input: \$n = 180\nOutput: @special-int-ct[$n]"; +} diff --git a/challenge-195/0rir/raku/ch-2.raku b/challenge-195/0rir/raku/ch-2.raku new file mode 100644 index 0000000000..2356b48bec --- /dev/null +++ b/challenge-195/0rir/raku/ch-2.raku @@ -0,0 +1,59 @@ +#!/usr/bin/env raku +# :vim ft=raku sw=4 expandtab # 🦋 ∅ ≡ ∩ ≢ ∈ «» +use v6.d; +use Test; + +=begin comment +195-2: Most Frequent Even Submitted by: Mohammad S Anwar + +Given a list of numbers, @list, find the most frequent even numbers in the +list. In case you get more than one even number then return the smallest +even integer. For all other case, return -1. + +Example 1 +Input: @list = (1,1,2,6,2) +Output: 2 as there are only 2 even numbers 2 and 6 and of those 2 appears the most. +Example 2 +Input: @list = (1,3,5,7) +Output: -1 since no even numbers found in the list +Example 3 +Input: @list = (6,4,4,6,1) +Output: 4 since there are only two even numbers 4 and 6. They both appears the equal number of times, so pick the smallest. +=end comment + +sub most-freqy-even( @l where * !~~ () --> Int) { + my %h = Bag.new( @l.grep( * %% 2)); + %h = grep { .key == %h.keys.min}, %h.grep( { .value ~~ %h.values.max}); + return (Int) if %h ~~ {}; + %h.keys[0].Int; +} + +multi MAIN ( 'test' ) { + my @Die = { in => (), exp => (Int), }, ; + + my @Test = + { in => (1,1,2,2,6,6), exp => 2, }, + { in => (1,1,2,6,2), exp => 2, }, + { in => (1,3,5,7), exp => (Int), }, + { in => (6,4,4,6,1), exp => 4, }, + { in => (6,6,6,6,6), exp => 6, }, + { in => (1,2,3,4,5), exp => 2, }, + { in => (2,2,3,3,6,6), exp => 2, }, + ; + plan +@Test + @Die; + for @Die -> %t { + dies-ok { most-freqy-even( @(%t<in>))}, + "most-freqy-even("~ (%t<exp>//"(Int)") ~") dies."; + } + for @Test -> %t { + quietly is most-freqy-even( @(%t<in>) ), %t<exp>, + (%t<exp> // '(Int)') ~" <- %t<in>"; + } + done-testing; +} + +multi MAIN() { + my @list = (1,2,2,2,2,2,3,6,6,6,6,6); + say "Input: \@list = @list.join(', ');\nOutput: ", + &most-freqy-even(@list) // -1; +} |
