diff options
| -rw-r--r-- | challenge-266/0rir/raku/ch-1.raku | 60 | ||||
| -rw-r--r-- | challenge-266/0rir/raku/ch-2.raku | 85 |
2 files changed, 145 insertions, 0 deletions
diff --git a/challenge-266/0rir/raku/ch-1.raku b/challenge-266/0rir/raku/ch-1.raku new file mode 100644 index 0000000000..896c4e86dd --- /dev/null +++ b/challenge-266/0rir/raku/ch-1.raku @@ -0,0 +1,60 @@ +#!/usr/bin/env raku +# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉⊆ ≡ ≢ « » ∴ +use v6.d; +use Test; + +=begin comment +266-1: Uncommon Words Submitted by: Mohammad Sajid Anwar +You are given two sentences, $line1 and $line2. + +Write a script to find all uncommmon words in any order in the given two sentences. Return ('') if none found. + +A word is uncommon if it appears exactly once in one of the sentences and doesn’t appear in other sentence. + +Example 1 +Input: $line1 = 'Mango is sweet' + $line2 = 'Mango is sour' +Output: ('sweet', 'sour') +Example 2 +Input: $line1 = 'Mango Mango' + $line2 = 'Orange' +Output: ('Orange') +Example 3 +Input: $line1 = 'Mango is Mango' + $line2 = 'Orange is Orange' +Output: ('') +=end comment + +my @Test = + # in-a in-b result result-lc + 'Mango is sweet', 'Mango is sour', ('sweet', 'sour'), ('sweet', 'sour'), + 'Mango Mango', 'Orange', ('Orange',), ('orange',), + 'Mango is Mango', 'Orange is Orange', ('',), ('',), + '', '', ('',), ('',), +; +plan @Test ÷ 2; + +# ignore case by using .lc +multi once-words( Bool :$lc!, *@word-text -->List) { + (@word-text.words».lc).Bag.grep( *.value == 1)».key; +} +# case sensitive +multi once-words( *@word-text -->List) { + @word-text.words.Bag.grep( *.value == 1)».key; +} + +for @Test -> $l1, $l2, @exp, @lc { + is once-words( $l1, $l2).sort, @exp.sort, "@exp[] <- $l1 / $l2"; + is once-words( :lc, $l1, $l2).sort, @lc.sort, "@lc[] <- $l1 / $l2"; +} + +done-testing; + +my $line1 = 'Ripe mango is sweet'; +my $line2 = 'New mango is sour'; +say "\nInput: \$line1 = '$line1'\n" + ~ " \$line2 = '$line2'\nOutput: ", + $ = (once-words( $line1, $line2)».&( "'" ~ * ~ "'")) ~~ Empty + ?? "('')" + !! (once-words( $line1, $line2)».&( "'" ~ * ~ "'")); + diff --git a/challenge-266/0rir/raku/ch-2.raku b/challenge-266/0rir/raku/ch-2.raku new file mode 100644 index 0000000000..0a415cec3b --- /dev/null +++ b/challenge-266/0rir/raku/ch-2.raku @@ -0,0 +1,85 @@ +#!/usr/bin/env raku +# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉⊆ ≡ ≢ « » ∴ +use v6.d; +use Test; + +=begin comment +266-2: X Matrix Submitted by: Mohammad Sajid Anwar +You are given a square matrix, $matrix. + +Write a script to find if the given matrix is X Matrix. + +A square matrix is an X Matrix if all the elements on the main diagonal and antidiagonal are non-zero and everything else are zero. + +Example 1 +Input: $matrix = [ [1, 0, 0, 2], + [0, 3, 4, 0], + [0, 5, 6, 0], + [7, 0, 0, 1], + ] +Output: true + +=end comment + +my @Dead = [], [[,],] ; + +my @Test = + [ [1], ], True, + [ [0], ], False, + [ [1, 1], [2, 2], ], True, + [ [1, 0], [2, 2], ], False, + [ [1, 2, 3], + [4, 5, 6], + [7, 8, 9], ], False, + [ [1, 0, 3], + [0, 5, 0], + [7, 0, 9], ], True, + [ [1, 0, 2], + [0, 3, 0], + [4, 0, 5], ], True, + [ [1, 0, 0, 2], + [0, 3, 4, 0], + [0, 5, 6, 0], + [7, 0, 0, 1], ], True, +; +plan 2 + +@Test ÷ 2; + +multi is-X-matrix( [[],]) { die "what is that" } +multi is-X-matrix( []) { die "unsatisfying" } +multi is-X-matrix( @a -->Bool:D) { + my ( $x1,$x2) = 0, @a.end; # Set up tracking of main diag. loca. + my @X; + for @a -> @r { + @X = ($x1, $x2).squish.sort; + my @non-zed-loc = @r.grep( :k, * !~~ 0); # Get all non-zed loca + return False if +@non-zed-loc ≠ +@X; # Have wrong count of non-zed + return False if @non-zed-loc !~~ @X ; # Check diag values + ++ $x1; + -- $x2; + } + return True; +} + +for @Test -> @in, $exp { + is is-X-matrix(@in), $exp, "$exp <- @in.raku()"; +} +for @Dead -> @in { + dies-ok { is-X-matrix @in }, "@in.raku() No numbers"; +} + + +sub display-matrix( $prefix, @matrix -->Str) { + my $ret = "$prefix [\n"; + my $shift = $prefix.chars; + for @matrix -> @r { + $ret ~= ' ' x ($shift + 3) ~ @r.raku() ~ "\n"; + } + $ret ~= ' ' x $shift ~ "]\n"; + $ret; +} +done-testing; + +my @matrix = [ [1, 0, 0, 2], [0, 3, 4, 0], [0, 5, 6, 0], [7, 0, 0, 1], ]; +say display-matrix( "\nInput: @matrix = ", @matrix); +say "Output: ", is-X-matrix( @matrix); + |
