diff options
| author | rir <rirans@comcast.net> | 2022-12-09 00:12:24 -0500 |
|---|---|---|
| committer | rir <rirans@comcast.net> | 2022-12-09 00:12:24 -0500 |
| commit | 316181dd66bccd6bfc54a8e46bb24b9e2a4ec947 (patch) | |
| tree | ecf0b2e574713a8a4e8ad2ef3c3156816336f393 /challenge-194 | |
| parent | 05ed7c67138bb2909dc1eb4fcb2a7c46bb087670 (diff) | |
| download | perlweeklychallenge-club-316181dd66bccd6bfc54a8e46bb24b9e2a4ec947.tar.gz perlweeklychallenge-club-316181dd66bccd6bfc54a8e46bb24b9e2a4ec947.tar.bz2 perlweeklychallenge-club-316181dd66bccd6bfc54a8e46bb24b9e2a4ec947.zip | |
194
Diffstat (limited to 'challenge-194')
| -rwxr-xr-x | challenge-194/0rir/raku/ch-1.raku | 95 | ||||
| -rwxr-xr-x | challenge-194/0rir/raku/ch-2.raku | 138 |
2 files changed, 233 insertions, 0 deletions
diff --git a/challenge-194/0rir/raku/ch-1.raku b/challenge-194/0rir/raku/ch-1.raku new file mode 100755 index 0000000000..150abeb124 --- /dev/null +++ b/challenge-194/0rir/raku/ch-1.raku @@ -0,0 +1,95 @@ +#!/usr/bin/env raku +# :vim ft=raku sw=4 expandtab # 🦋 ∅ ≡ ∩ ≢ ∈ «» +use v6.d; +use Test; + +=begin comment +194-1: Digital Clock Submitted by: Mohammad S Anwar +Given time in the format hh:mm with one missing digit. Find the highest +digit between 0-9 that makes it valid time. + +Example 1 +Input: $time = '?5:00' +Output: 1 + +Since 05:00 and 15:00 are valid time and no other digits can fit in +the missing place. +Example 2 +Input: $time = '?3:00' +Output: 2 +Example 3 +Input: $time = '1?:00' +Output: 9 +Example 4 +Input: $time = '2?:00' +Output: 3 +Example 5 +Input: $time = '12:?5' +Output: 5 +Example 6 +Input: $time = '12:5?' +Output: 9 +=end comment + +# Fix the malformed time to the latest time possible. +sub extra( Str:D $t --> Str ) { + my $i = tick-tock( $t) ; + return $t.subst( /'?'/, $i ); +} + +# Return the digit which makes the string the latest time possible. +sub tick-tock( Str:D $t --> Int ) { + my $s; + given $t { + when m/ '?' \d ':' \d \d / { + $s = $t.substr( 1,1 ).Int; + when $s == 4 and $t.substr(3).Int == 0 { return 2 } + when $s > 3 { return 1 } + when $s ≤ 3 { return 2 } + } + when m/ \d '?' ':' \d \d / { + $s = $t.substr( 0,1 ).Int; + when $s ≠ 2 { return 9 } + when $s == 2 and $t.substr(3).Int == 0 { return 4 } + return 3; + } + when m/ \d \d ':' '?' \d / { return 5; } + when m/ \d \d ':' \d '?' / { return 9; } + + default { die 'Input is not broken correctly'; } + } +} + +multi MAIN ( 'test') { + my @Test = + { in => '?0:00', exp => '20:00', }, + { in => '?3:00', exp => '23:00', }, + { in => '?4:00', exp => '24:00', }, + { in => '?4:01', exp => '14:01', }, + { in => '?5:00', exp => '15:00', }, + { in => '0?:00', exp => '09:00', }, + { in => '1?:00', exp => '19:00', }, + { in => '2?:00', exp => '24:00', }, + { in => '2?:09', exp => '23:09', }, + { in => '23:?0', exp => '23:50', }, + { in => '23:0?', exp => '23:09', }, + ; + my @die = '?3:0?',; + + plan 1 + @Test; + for @die -> $s { + dies-ok { tick-tock( $s ) }, 'Dies on malformed input.'; + } + for @Test -> %t { + is extra( %t<in>), %t<exp>, " %t<in> -> %t<exp>"; + } + done-testing; + exit; +} + +multi MAIN( Str $in = "?4:01") { +say "Input: \$time = '?4:01' +Output: ", tick-tock($in); +} + + diff --git a/challenge-194/0rir/raku/ch-2.raku b/challenge-194/0rir/raku/ch-2.raku new file mode 100755 index 0000000000..e7b337ce50 --- /dev/null +++ b/challenge-194/0rir/raku/ch-2.raku @@ -0,0 +1,138 @@ +#!/usr/bin/env raku +# :vim ft=raku sw=4 expandtab # 🦋 ∅ ≡ ∩ ≢ ∈ «» +use v6.d; +use Test; + +=begin comment +194-2: Frequency Equalizer Submitted by: Mohammad S Anwar +Given a string made of alphabetic characters only, a-z, determine whether +removing only one character can make the frequency of the remaining +characters the same. + +Example 1: +Input: $s = 'abbc' +Output: 1 since removing one alphabet 'b' will give us 'abc' where each alphabet frequency is the same. +Example 2: +Input: $s = 'xyzyyxz' +Output: 1 since removing 'y' will give us 'xzyyxz'. +Example 3: +Input: $s = 'xzxz' +Output: 0 since removing any one alphabet would not give us string with same frequency alphabet. +=end comment + +# freq -- Take a str return true if one char can be deleted leaving +# all remaining letter-values occurring equally. +# +# There are two versions; the default is that more than one +# letter-value must remain, i.e. 'ab'. +# +# If the SINGLE flag is raised, a single letter value suffices, i.e. +# 'a'. +proto freq( Str $s, | ) { + + return False if $s.chars ≤ 1; + + my %*h = Bag.new( Bag.new( $s.comb).values); # size => set-ct + + when %*h.elems > 2 { return False } + + if %*h.elems == 1 { + my ( $size, $set-ct ) = %*h.keys[0], %*h.values[0]; + return True if $size == 1 and $set-ct > 2; # abc + return False if $size > 1 and $set-ct > 1; # aabb + } + + if %*h.elems == 2 { + if %*h<1>:exists and %*h<1> == 1 and %*h.keys.max == 2 + and %*h{%*h.keys.max} == 1 + { return True } + } + {*} +} + +multi freq( Str $s --> Bool) { + + if %*h.elems == 2 { + if %*h<1>:exists and %*h<1> == 1 and %*h{%*h.keys.max} == 1 + { return False } + if %*h<1>:exists and %*h<1> == 1 + { return True; } + + my $max-size = %*h.keys.max; + if $max-size - 1 ≠ %*h.keys.min { return False } + if $max-size - 1 == %*h.keys.min and %*h{$max-size} ≠ 1 { return False} + if %*h{$max-size} == 1 and $max-size -1 == %*h.keys.min { return True } + } + + if %*h.elems == 1 { + my ( $size, $set-ct ) = %*h.keys[0], %*h.values[0]; + + return False if $size == 1 and $set-ct == 2; # ab + return False if $set-ct == 1; # a … aaaa + } +} + +multi freq( Str $s, Bool :$SINGLE! --> Bool) { + + if %*h.elems == 2 { + if %*h<1>:exists and %*h<1> == 1 { return True; } + my $max-size = %*h.keys.max; + if $max-size - 1 == %*h.keys.min and %*h{$max-size} ≠ 1 { return False} + if %*h{$max-size} == 1 and $max-size -1 == %*h.keys.min { return True } + if $max-size - 1 ≠ %*h.keys.min { return False } + } + + if %*h.elems == 1 { + my ( $size, $set-ct ) = %*h.keys[0], %*h.values[0]; + + return True if $size == 1 and $set-ct > 1; # reduce ab… + return True if $size > 1 and $set-ct == 1; # reduce aa… + } +} + +sub justify( $s --> Str) { + my $ret = ''; + my $prev = Str; + my @a = $s.comb.sort; + while @a { + my $c = @a.shift; + $ret ~= $c ~~ $prev ?? $c !! " $c"; + $prev = $c; + } + $ret; +} + +multi MAIN( 'test') { + my @Test = + { in => 'a', exp => False, single => False }, + { in => 'aa', exp => False, single => True }, + { in => 'ab', exp => False, single => True }, + { in => 'abc', exp => True, single => True }, + { in => 'aaabbc', exp => False, single => False }, + { in => 'aabb', exp => False, single => False }, + { in => 'abbb', exp => False, single => True }, + { in => 'aabbcd', exp => False, single => False }, + { in => 'aabbbb', exp => False, single => False }, + { in => 'abbcccd', exp => False, single => False }, + { in => 'aabbc', exp => True, single => True }, + { in => 'abbc', exp => True, single => True }, + { in => 'aabbbcc', exp => True, single => True }, + { in => 'abcdeff', exp => True, single => True }, + { in => 'aabbccddeefff', exp => True, single => True }, + { in => 'abb', exp => True, single => True }, + { in => 'aaabbbcccdddeeeff', exp => False, single => False }, + ; + plan 2 × @Test; + for @Test -> %t { + is freq(%t<in>), %t<exp>, "%t<exp>\t<- %t<in>"; + } + for @Test -> %t { + is freq(%t<in>, :SINGLE), %t<single>, " SINGLE %t<single>\t<- %t<in>"; + } + done-testing; +} + +multi MAIN( Str $str = 'aaicocciaoouiacio') { + say "Input: \$s = $str\n" + ~ "Output: ", freq($str).Int(), ' because: ', justify($str); +} |
