diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2023-02-19 21:58:52 +0000 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2023-02-19 21:58:52 +0000 |
| commit | 9f696d3c12edb40fa1dfc7ebdde564abd614007b (patch) | |
| tree | a9826072505630ad9e4bd7d149a565ed483bf89a | |
| parent | 58ee0c22a03f13598959b0c516b45e809e83f897 (diff) | |
| parent | ebe946b2c71b7e1417c6b4529a278dd25361e12e (diff) | |
| download | perlweeklychallenge-club-9f696d3c12edb40fa1dfc7ebdde564abd614007b.tar.gz perlweeklychallenge-club-9f696d3c12edb40fa1dfc7ebdde564abd614007b.tar.bz2 perlweeklychallenge-club-9f696d3c12edb40fa1dfc7ebdde564abd614007b.zip | |
Merge branch 'master' of https://github.com/manwar/perlweeklychallenge-club
| -rw-r--r-- | challenge-204/polettix/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-204/polettix/blog1.txt | 1 | ||||
| -rw-r--r-- | challenge-204/polettix/perl/ch-1.pl | 17 | ||||
| -rw-r--r-- | challenge-204/polettix/perl/ch-2.pl | 35 | ||||
| -rw-r--r-- | challenge-204/polettix/raku/ch-1.raku | 7 | ||||
| -rw-r--r-- | challenge-204/polettix/raku/ch-2.raku | 16 |
6 files changed, 77 insertions, 0 deletions
diff --git a/challenge-204/polettix/blog.txt b/challenge-204/polettix/blog.txt new file mode 100644 index 0000000000..e6c41f3a5f --- /dev/null +++ b/challenge-204/polettix/blog.txt @@ -0,0 +1 @@ +https://etoobusy.polettix.it/2023/02/16/pwc204-monotonic-array/ diff --git a/challenge-204/polettix/blog1.txt b/challenge-204/polettix/blog1.txt new file mode 100644 index 0000000000..5424d9acf2 --- /dev/null +++ b/challenge-204/polettix/blog1.txt @@ -0,0 +1 @@ +https://etoobusy.polettix.it/2023/02/17/pwc204-reshape-matrix/ diff --git a/challenge-204/polettix/perl/ch-1.pl b/challenge-204/polettix/perl/ch-1.pl new file mode 100644 index 0000000000..d3743a7881 --- /dev/null +++ b/challenge-204/polettix/perl/ch-1.pl @@ -0,0 +1,17 @@ +#!/usr/bin/env perl +use v5.24; +use warnings; +use experimental 'signatures'; +no warnings 'experimental::signatures'; + +say monotonic_array(@ARGV) ? 1 : 0; + +sub monotonic_array (@array) { + my $direction = 0; + for my $i (1 .. $#array) { + my $delta = $array[$i] - $array[$i - 1]; + return 0 if $direction * $delta < 0; + $direction ||= $delta; + } + return 1; +} diff --git a/challenge-204/polettix/perl/ch-2.pl b/challenge-204/polettix/perl/ch-2.pl new file mode 100644 index 0000000000..98cd009c3f --- /dev/null +++ b/challenge-204/polettix/perl/ch-2.pl @@ -0,0 +1,35 @@ +#!/usr/bin/env perl +use v5.24; +use warnings; +use experimental 'signatures'; +no warnings 'experimental::signatures'; +use Data::Dumper; + +my $reshaped = reshape_matrix([ [ 1 .. 3], [ 4 .. 6] ], 3, 2); +say $reshaped ? Dumper($reshaped) : 0; + +sub reshape_matrix ($matrix, $r, $c) { + my $needed = $r * $c; + my $available = $matrix->@*; + $available *= $matrix->[0]->@* if $available; + return 0 if $needed != $available; + + my $it = elements_it($matrix); + return [ map { [ map { $it->() } 1 .. $c ] } 1 .. $r ]; +} + +sub elements_it ($aoa) { + my ($r, $c) = (0, 0); + return sub { + while ('necessary') { + return if $r > $aoa->$#*; + my $row = $aoa->[$r]; + if ($c > $row->$#*) { + ++$r; + $c = 0; + next; + } + return $row->[$c++]; + } + }; +} diff --git a/challenge-204/polettix/raku/ch-1.raku b/challenge-204/polettix/raku/ch-1.raku new file mode 100644 index 0000000000..d6af334496 --- /dev/null +++ b/challenge-204/polettix/raku/ch-1.raku @@ -0,0 +1,7 @@ +#!/usr/bin/env raku +use v6; +sub MAIN (*@args) { put monotonic-array(@args) ?? 1 !! 0 } + +sub monotonic-array (@array) { + ([*] @array.rotor(2 => -1).map({[-] $_}).minmax[0, *-1]) >= 0 +} diff --git a/challenge-204/polettix/raku/ch-2.raku b/challenge-204/polettix/raku/ch-2.raku new file mode 100644 index 0000000000..ee60eb29da --- /dev/null +++ b/challenge-204/polettix/raku/ch-2.raku @@ -0,0 +1,16 @@ +#!/usr/bin/env raku +use v6; +sub MAIN { + my $reshaped = reshape-matrix([ [ 1 .. 3], [ 4 .. 6] ], 3, 2); + put $reshaped ?? $reshaped.gist !! 0; +} + +sub reshape-matrix ($matrix, $r, $c) { + my $needed = $r * $c; + my $available = $matrix.elems; + $available *= $matrix[0].elems if $available; + return Nil if $needed != $available; + + # https://stackoverflow.com/questions/41648119/how-can-i-completely-flatten-a-list-of-lists-of-lists/41649110#41649110 + return [$matrix[*;*].rotor($c)]; +} |
