diff options
| author | rir <rirans@comcast.net> | 2022-11-30 22:56:49 -0500 |
|---|---|---|
| committer | rir <rirans@comcast.net> | 2022-11-30 22:56:49 -0500 |
| commit | 037cc8405de132e491faff769a36b026048aa8d2 (patch) | |
| tree | ea6be287759e246110da6ddbd170380a15e73020 | |
| parent | 8e96f048499196291d2707501a4f8e770fda5fda (diff) | |
| download | perlweeklychallenge-club-037cc8405de132e491faff769a36b026048aa8d2.tar.gz perlweeklychallenge-club-037cc8405de132e491faff769a36b026048aa8d2.tar.bz2 perlweeklychallenge-club-037cc8405de132e491faff769a36b026048aa8d2.zip | |
193
| -rw-r--r-- | challenge-193/0rir/raku/ch-1.raku | 50 | ||||
| -rw-r--r-- | challenge-193/0rir/raku/ch-2.raku | 70 |
2 files changed, 120 insertions, 0 deletions
diff --git a/challenge-193/0rir/raku/ch-1.raku b/challenge-193/0rir/raku/ch-1.raku new file mode 100644 index 0000000000..d814ea1d0f --- /dev/null +++ b/challenge-193/0rir/raku/ch-1.raku @@ -0,0 +1,50 @@ +#!/usr/bin/env raku +# :vim ft=raku sw=4 expandtab # 🦋 ∅ ≡ ∩ ≢ ∈ «» +use v6.d; +use Test; + +=begin comment +193-1: Binary String Submitted by: Mohammad S Anwar +Given an integer, $n > 0, find all possible binary numbers of size $n. + +Input: $n = 3 +Output: 000, 001, 010, 100, 111, 110, 101, 011 +=end comment + +=begin comment-on-spec + The spec is clear. I am adding strictness which will disallow binary + numbers which have leading zeros. +=end comment-on-spec + +multi bit-ct-to-bi-numbers( Int $n, Bool :$strict --> List) { + my $o = $strict ?? 2**($n-1) !! 0; + return ( for $o..^2**$n { sprintf "%0$n" ~ 's', $_.base(2) }) +} + +multi MAIN ( Int $n where * > 0) { + say "Input: \$n = $n"; + say 'Output: ', bit-ct-to-bi-numbers( $n).join(', '); +} + +multi MAIN ( 'test') { + my @Test = + 1 => qw<0 1>, + 2 => qw<00 01 10 11 >, + 3 => qw<000 001 010 011 100 101 110 111 >, + 4 => (sprintf( "%04s", $_) for (0..^16)».base(2)), + 5 => (sprintf( "%05s", $_) for (0..^32)».base(2)), + 6 => (sprintf( "%06s", $_) for (0..^64)».base(2)), + 12 => (sprintf( "%012s", $_) for (0..^4096)».base(2)), + ; + + plan 2 × @Test; + + for @Test -> $t { + is-deeply bit-ct-to-bi-numbers($t.key), $t.value, "testing $t.key()"; + is-deeply bit-ct-to-bi-numbers($t.key, :strict), + $t.value[1+$t.value.end/2 .. $t.value], + "testing $t.key() strictly"; + } + done-testing; + exit; +} diff --git a/challenge-193/0rir/raku/ch-2.raku b/challenge-193/0rir/raku/ch-2.raku new file mode 100644 index 0000000000..5b25f24fb1 --- /dev/null +++ b/challenge-193/0rir/raku/ch-2.raku @@ -0,0 +1,70 @@ +#!/usr/bin/env raku +# :vim ft=raku sw=4 expandtab # 🦋 ∅ ≡ ∩ ≢ ∈ «» +use v6.d; +use Test; + +=begin comment +193-2: Odd String Submitted by: Mohammad S Anwar +Given a list of strings of same length, @s, find the odd string in the given list. Use positional value of alphabet starting with 0, i.e. a = 0, b = 1, ... z = 25. +Find the difference array for each string as shown, then pick the odd one. + +Example 1: +Input: @s = ("adc", "wzy", "abc") +Output: "abc" + +Difference array for "adc" => [ d - a, c - d ] => [ 3 - 0, 2 - 3 ] => [ 3, -1 ] +Difference array for "wzy" => [ z - w, y - z ] => => [ 3, -1 ] +Difference array for "abc" => [ b - a, c - b ] => => [ 1, 1 ] +The difference array for "abc" is the odd one. + +Example 2: +Input: @s = ("aaa", "bob", "ccc", "ddd") +Output: "bob" + +Difference array for "aaa" => [ a - a, a - a ] => => [ 0, 0 ] +Difference array for "bob" => [ o - b, b - o ] => => [ 13, -13 ] +Difference array for "ccc" => [ c - c, c - c ] => => [ 0, 0 ] +Difference array for "ddd" => [ d - d, d - d ] => => [ 0, 0 ] +The difference array for "bob" is the odd one. +=end comment + +=begin interpretation +Case distinction is ignored by assuming all input is lowercase as in the examples. +'Odd' has no clear definition and the examples are restricted to datasets +in which all but one member are equivalent using the prescribed comparison. +This vagueness is resolved by returning undefined unless there is only one +word found as unique, i.e. uniqueness is not odd when there are a multiple +unique items. +=end interpretation + +sub oddity( @list --> Str ) { + my @input = @list; + @input.=map: {$_.comb.map(*.ord-97).rotor( 2=>-1).map( {$_[1]-$_[0]}).join(' ')}; + my %h = Hash.new.append: @input Z=> @list; + my @runique = %h.values.grep( *.elems == 1); + return @runique[0] if @runique.end == 0; + return Nil; +} +multi MAIN ( 'test' ) { + my @Test = + xyz =>(qw{ xyz },), + bob => qw{ aaa bob ccc ddd }, + abc => qw{ adc wzy abc }, + (Nil) => qw{ adc way abc }, + aa => qw{ aa ab bc de }, + (Nil) => qw{ aaa aab abc ade }, + aaaa => qw{ aaaa aaab bbbc ddde }, + (Nil) => qw{ aaaa aaab bbbc ddde bbbb }, + (Nil) => qw{ aaaa azcd amcd alcd }, + ; + plan +@Test; + for @Test -> %t { + is oddity( %t.value), %t.key, ' ' ~ %t.raku; + } + done-testing; +} +multi MAIN( @s = ("aaa", "bob", "ccc", "ddd") ) { + say "Input: \@s = (", @s.join(', ') ~ ')' ; + say "Output: ", oddity( @s ); +} + |
