diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-04-21 23:25:32 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-04-21 23:25:32 +0100 |
| commit | cf4e18f239d3cf6d70843272487efe54cdc2bae3 (patch) | |
| tree | 917c4292cc12d5b9f0e923c8ae280d063912c752 | |
| parent | 1d5258f33f99640d0c7e96cd8ac139ebe5315bfe (diff) | |
| parent | 5e745c277bd9578a6343c5e653f4b052320621ab (diff) | |
| download | perlweeklychallenge-club-cf4e18f239d3cf6d70843272487efe54cdc2bae3.tar.gz perlweeklychallenge-club-cf4e18f239d3cf6d70843272487efe54cdc2bae3.tar.bz2 perlweeklychallenge-club-cf4e18f239d3cf6d70843272487efe54cdc2bae3.zip | |
Merge pull request #9967 from 0rir/work
265
| -rw-r--r-- | challenge-265/0rir/ch-1.raku | 73 | ||||
| -rw-r--r-- | challenge-265/0rir/ch-2.raku | 71 |
2 files changed, 144 insertions, 0 deletions
diff --git a/challenge-265/0rir/ch-1.raku b/challenge-265/0rir/ch-1.raku new file mode 100644 index 0000000000..124959e39b --- /dev/null +++ b/challenge-265/0rir/ch-1.raku @@ -0,0 +1,73 @@ +#!/usr/bin/env raku +# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉⊆ ≡ ≢ « » ∴ +use v6.d; +use Test; + +=begin comment +265-1: 33% Appearance Submitted by: Mohammad Sajid Anwar +You are given an array of integers, @ints. Write a script to find an integer +in the given array that appeared 33% or more. If more than one found, +return the smallest. If none found then return undef. + +Example 1 +Input: @ints = (1,2,3,3,3,3,4,2) +Output: 3 + +1 appeared 1 times. +2 appeared 2 times. +3 appeared 4 times. + +3 appeared 50% (>33%) in the given array. +Example 2 +Input: @ints = (1,1) +Output: 1 + +1 appeared 2 times. + +1 appeared 100% (>33%) in the given array. +Example 3 +Input: @ints = (1,2,3) +Output: 1 + +1 appeared 1 times. +2 appeared 1 times. +3 appeared 1 times. + +Since all three appeared 33.3% (>33%) in the given array. +We pick the smallest of all. + +=end comment + +my @Test = + 3, (1,2,3,3,3,3,4,2), + 1, (1,1), + 1, (1,2,3), + Int, (1,2,3,4), + 1, (4,1,4,1,4,1,4,1), + 1, (4,1,4,1,4,1,4,4), + 1, (4,1,4,1,4,1,4,9), + 1, flat( (1,2,3) xx 333_331, 4 xx 7 ), + 1, flat( (1,2,3) xx 33_330, 2, 2, 4 xx 7), + Int, flat( (1,2,3) xx 30, 4 xx 10 ), + Int, flat( (1,2,3) xx 30, 4 xx 9 ), + 3, flat( (1,2,3) xx 30, 3 xx 10 ), +; +plan @Test ÷ 2; + +sub over( @a, $min = 33 -->Int) { + my $m = MixHash.new( @a); + for $m.values -> $v is rw { $v ×= 100 ÷ @a.elems } # percents + my $ret = $m.sort( { .value, .key } ).first( *.value > $min); + $ret.defined ?? $ret.key !! Int; +} + +for @Test -> $exp, @in { + is over( @in), $exp, ($exp // 'Int') ~ " <- @in.gist()"; +} + +done-testing; +my @int = flat | (1,2,3) xx 32, 4, 2, 3,3 ; +say "\nInput: @int = @int[]\nOutput: ", over(@int); + +exit; + diff --git a/challenge-265/0rir/ch-2.raku b/challenge-265/0rir/ch-2.raku new file mode 100644 index 0000000000..e602437973 --- /dev/null +++ b/challenge-265/0rir/ch-2.raku @@ -0,0 +1,71 @@ +#!/usr/bin/env raku +# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉⊆ ≡ ≢ « » ∴ +use v6.d; +use Test; + +=begin comment +265-2: Completing Word Submitted by: Mohammad Sajid Anwar +You are given a string, $str containing alphnumeric characters and array +of strings (alphabetic characters only), @str. + +Write a script to find the shortest completing word. If none found return empty + string. + +A completing word is a word that contains all the letters in the given string, ignoring space and number. If a letter appeared more than once in the given string then it must appear the same number or more in the word. + +Example 1 +Input: $str = 'aBc 11c' + @str = ('accbbb', 'abc', 'abbc') +Output: 'accbbb' + +The given string contains following, ignoring case and number: +a 1 times +b 1 times +c 2 times + +The only string in the given array that satisfies the condition is 'accbbb'. +Example 2 +Input: $str = 'Da2 abc' + @str = ('abcm', 'baacd', 'abaadc') +Output: 'baacd' +Example 3 +Input: $str = 'JB 007' + @str = ('jj', 'bb', 'bjb') +Output: 'bjb' +=end comment + +my @Test = + # in choices exp + 'JB 007', ['jj', 'bb', 'bjb'], 'bjb', + 'Da2 abc', ['abcm', 'baacd', 'abaadc'], 'baacd', + 'aBc 11c', ['accbbb', 'abc', 'abbc'], 'accbbb', + 'abb cDE', ['abcDE', 'abcde', 'abcdde'], '', + 'abb cDE', ['abcDE', 'abcde', 'ABcbde'], 'ABcbde', + 'abb cDE', ['abBCDEE', 'abcde', 'abcdbe'], 'abcdbe', + 'abb cDE', ['abcde', 'abcdbe','abBCDEE' ], 'abcdbe', + 'ab', ['abcde', 'ABc','abB' ], 'ABc', +; +plan @Test ÷ 3; + +sub func( $str is copy, @str -->Str) { + $str ~~ s:g/<:!L>// ; # scrub to ":Letter"s only + $str = Bag.new( $str.fc.comb); # foldcased + for @str.sort( *.chars) { + return $_ if $str ⊆ Bag.new( $_.fc.comb); + } + return ''; +} + +for @Test -> $str, @str, $exp { + is func($str, @str), $exp, + sprintf "%-8s <- %-8s <- %-20s", $exp, $str, @str.join: ' '; +} + +done-testing; + +my $str = 'abb cDE'; +my @str = 'abcDE', 'abcde', 'ABcbde'; +say "\nInput: \$str = $str\n @str = @str[]\nOutput: ", func( $str, @str); + +exit; + |
