From 24ebe044fcc947417212a49b55aabaeac8d50695 Mon Sep 17 00:00:00 2001 From: rir Date: Mon, 12 Dec 2022 17:24:48 -0500 Subject: 195 --- challenge-195/0rir/raku/ch-1.raku | 52 ++++++++++++++++++++++++++++++++++ challenge-195/0rir/raku/ch-2.raku | 59 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 111 insertions(+) create mode 100644 challenge-195/0rir/raku/ch-1.raku create mode 100644 challenge-195/0rir/raku/ch-2.raku 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], %t, " %t <- 1..%t"; + } + 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))}, + "most-freqy-even("~ (%t//"(Int)") ~") dies."; + } + for @Test -> %t { + quietly is most-freqy-even( @(%t) ), %t, + (%t // '(Int)') ~" <- %t"; + } + 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; +} -- cgit