diff options
| author | BarrOff <58253563+BarrOff@users.noreply.github.com> | 2023-04-10 00:54:15 +0200 |
|---|---|---|
| committer | BarrOff <58253563+BarrOff@users.noreply.github.com> | 2023-04-10 00:54:15 +0200 |
| commit | 3eace4beecbfc561bce52e0117c1716cc3d09668 (patch) | |
| tree | 4be18efc8edc3fd4d8cea497a5f2dc1624919f92 | |
| parent | 4890cd1addbde634e231ba6eb4656f7eb59085e9 (diff) | |
| download | perlweeklychallenge-club-3eace4beecbfc561bce52e0117c1716cc3d09668.tar.gz perlweeklychallenge-club-3eace4beecbfc561bce52e0117c1716cc3d09668.tar.bz2 perlweeklychallenge-club-3eace4beecbfc561bce52e0117c1716cc3d09668.zip | |
feat: add solutions for challenge 211 from BarrOff
| -rw-r--r-- | challenge-211/barroff/raku/ch-1.raku | 50 | ||||
| -rw-r--r-- | challenge-211/barroff/raku/ch-2.raku | 36 |
2 files changed, 86 insertions, 0 deletions
diff --git a/challenge-211/barroff/raku/ch-1.raku b/challenge-211/barroff/raku/ch-1.raku new file mode 100644 index 0000000000..a1c61bd33a --- /dev/null +++ b/challenge-211/barroff/raku/ch-1.raku @@ -0,0 +1,50 @@ +#!/usr/bin/env raku + +use v6.d; + +sub get-indices(Int $ystart, Int $ymax, Int $xstart, Int $xmax --> List) { + my Int $count-indices = min($xmax - $xstart, $ymax - $ystart); + return (($xstart, $ystart), { $_[0] + 1, $_[1] + 1 } … *)[^$count-indices]; +} + +sub check-indices(@a, @indices --> Bool) { + my Int $val = @a[@indices[0][1]][@indices[0][0]]; + so $val == map({ @a[$_[1]][$_[0]] }, @indices).all; +} + +sub toeplitz-rows(@matrix, Int $xmax, Int $ymax --> Bool) { + my @y-indices = map({ get-indices($_, $ymax, 0, $xmax) }, 1 ..^ $ymax); + return so map({ check-indices(@matrix, $_) }, @y-indices).all; +} + +sub toeplitz-columns(@matrix, Int $xmax, Int $ymax --> Bool) { + my @x-indices = map({ get-indices(0, $ymax, $_, $xmax) }, 0 ..^ $xmax); + return so map({ check-indices(@matrix, $_) }, @x-indices).all; +} + +sub is-toeplitz(@matrix --> Bool) { + my Int $xmax = @matrix[0].elems; + my Int $ymax = @matrix.elems; + # return toeplitz-columns(@matrix, $xmax, $ymax) and toeplitz-rows(@matrix, $xmax, $ymax); + return so map({ $_(@matrix, $xmax, $ymax) }, (&toeplitz-columns, &toeplitz-rows)).all; +} + +#| Run test cases +multi sub MAIN('test') { + use Test; + plan 2; + + my @a = [ + [4, 3, 2, 1], + [5, 4, 3, 2], + [6, 5, 4, 3] + ]; + + my @b = [ + [1, 2, 3], + [3, 2, 1] + ]; + + is is-toeplitz(@a), True, "works for @a"; + is is-toeplitz(@b), False, "works for @b"; +} diff --git a/challenge-211/barroff/raku/ch-2.raku b/challenge-211/barroff/raku/ch-2.raku new file mode 100644 index 0000000000..f161969b02 --- /dev/null +++ b/challenge-211/barroff/raku/ch-2.raku @@ -0,0 +1,36 @@ +#!/usr/bin/env raku + +use v6.d; + +sub avg(List $l --> Rat) { + sum($l) ÷ $l.elems; +} + +sub same-average(List $l1, List $l2 --> Bool) { + return avg($l1) == avg($l2); +} + +sub check-split(Set $s1, Set $s2 --> Bool) { + my $diff-set = $s1 (-) $s2; + return same-average($s2.keys.List, $diff-set.keys.List); +} + +sub create-n-split(List $l, Int $n --> Seq) { + my @combs = combinations($l, $n); + return map({ ($l.Set, $_.Set) }, @combs); +} + +sub check-averages(List $l --> Bool) { + my @splits = map({ |create-n-split($l, $_) }, 1 .. floor($l.elems ÷ 2)); + say @splits.elems; + return so map({ check-split(|$_) }, @splits.race).any; +} + +#| Run test cases +multi sub MAIN('test') { + use Test; + plan 2; + + is check-averages((1, 2, 3, 4, 5, 6, 7, 8)), True, 'works for (1, 2, 3, 4, 5, 6, 7, 8)'; + is check-averages((1, 3)), False, 'works for (1, 3)'; +} |
