diff options
| author | Packy Anderson <PackyAnderson@gmail.com> | 2024-05-06 23:41:04 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-05-06 23:41:04 -0400 |
| commit | 8da3f7983b54d8c8eb96326f4d7f3d52f1a78d9b (patch) | |
| tree | 4cc9a6443382d2a32208246c481cd566b6ea0142 /challenge-266 | |
| parent | e4a2c66b30346606d347ecc4a08b83b733797d2a (diff) | |
| parent | c1756b0e7aed0ad70fa63feb2565c69215c9d426 (diff) | |
| download | perlweeklychallenge-club-8da3f7983b54d8c8eb96326f4d7f3d52f1a78d9b.tar.gz perlweeklychallenge-club-8da3f7983b54d8c8eb96326f4d7f3d52f1a78d9b.tar.bz2 perlweeklychallenge-club-8da3f7983b54d8c8eb96326f4d7f3d52f1a78d9b.zip | |
Merge branch 'manwar:master' into challenge-268
Diffstat (limited to 'challenge-266')
98 files changed, 4609 insertions, 17 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); + diff --git a/challenge-266/2colours/php/ch-1.php b/challenge-266/2colours/php/ch-1.php new file mode 100644 index 0000000000..3079fd5dff --- /dev/null +++ b/challenge-266/2colours/php/ch-1.php @@ -0,0 +1,14 @@ +<?php + +echo '$line1 = '; +$line1 = preg_split('/\s+/', rtrim(fgets(STDIN))); +echo '$line2 = '; +$line2 = preg_split('/\s+/', rtrim(fgets(STDIN))); + +$word_frequencies = array_count_values($line1); +foreach (array_count_values($line2) as $key => $value) { + @$word_frequencies[$key] += $value; +} + +$solution = array_keys(array_filter($word_frequencies, fn($freq) => $freq === 1)) ?: ['']; +var_dump($solution); diff --git a/challenge-266/2colours/php/ch-2.php b/challenge-266/2colours/php/ch-2.php new file mode 100644 index 0000000000..c8f7064f37 --- /dev/null +++ b/challenge-266/2colours/php/ch-2.php @@ -0,0 +1,22 @@ +<?php + +$matrix = json_decode(file_get_contents('php://stdin')); +$size = count($matrix[0]); # let's hope it's actually a square matrix... + +// all 0's except for those 2 - which may be one if it's the middle +function is_row_compliant($row, $diagonal_position, $size) +{ + $is_middle = $diagonal_position === $size - 1 - $diagonal_position; + return @array_count_values($row)[0] === $size - ($is_middle ? 1 : 2) && $row[$diagonal_position] !== 0 && $row[$size - 1 - $diagonal_position] !== 0; +} + +//[$diagonal_position, $solution] = array_reduce($matrix, fn ($state, $row) => [$state[0] + 1, $state[1] && is_row_compliant($row, $state[0], $size)], [0, true]); # is this really worth it? :D +$solution = true; +foreach ($matrix as $row_index => $row) { + if (!is_row_compliant($row, $row_index, $size)) { + $solution = false; + break; + } +} + +var_dump($solution);
\ No newline at end of file diff --git a/challenge-266/arne-sommer/blog.txt b/challenge-266/arne-sommer/blog.txt new file mode 100644 index 0000000000..28aabc6510 --- /dev/null +++ b/challenge-266/arne-sommer/blog.txt @@ -0,0 +1 @@ +https://raku-musings.com/uncommon-x.html diff --git a/challenge-266/arne-sommer/raku/ch-1.raku b/challenge-266/arne-sommer/raku/ch-1.raku new file mode 100755 index 0000000000..a9b8fc4986 --- /dev/null +++ b/challenge-266/arne-sommer/raku/ch-1.raku @@ -0,0 +1,16 @@ +#! /usr/bin/env raku + +unit sub MAIN (*@ |
