aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2023-05-07 23:00:34 +0100
committerGitHub <noreply@github.com>2023-05-07 23:00:34 +0100
commit8cb49c2c4bcd29a6a3e99a64cf4d06a15cd1e1fc (patch)
tree4d010c690b63432ca930e0db815ae010950dba3e
parent5324aca96ee076f98fb70d1d5fdef7bb89e3e668 (diff)
parentf25bab826dc48b84d2e1830b275781422aed6c2b (diff)
downloadperlweeklychallenge-club-8cb49c2c4bcd29a6a3e99a64cf4d06a15cd1e1fc.tar.gz
perlweeklychallenge-club-8cb49c2c4bcd29a6a3e99a64cf4d06a15cd1e1fc.tar.bz2
perlweeklychallenge-club-8cb49c2c4bcd29a6a3e99a64cf4d06a15cd1e1fc.zip
Merge pull request #8023 from jo-37/contrib
Solutions to challenge 215
-rwxr-xr-xchallenge-215/jo-37/perl/ch-1.pl61
-rwxr-xr-xchallenge-215/jo-37/perl/ch-2.pl64
2 files changed, 125 insertions, 0 deletions
diff --git a/challenge-215/jo-37/perl/ch-1.pl b/challenge-215/jo-37/perl/ch-1.pl
new file mode 100755
index 0000000000..2f8236a666
--- /dev/null
+++ b/challenge-215/jo-37/perl/ch-1.pl
@@ -0,0 +1,61 @@
+#!/usr/bin/perl -s
+
+use v5.24;
+use Test2::V0 '!float';
+use PDL;
+use PDL::Char;
+
+our ($tests, $examples);
+
+run_tests() if $tests || $examples; # does not return
+
+die <<EOS unless @ARGV;
+usage: $0 [-examples] [-tests] [WORD...]
+
+-examples
+ run the examples from the challenge
+
+-tests
+ run some tests
+
+WORD...
+ list of words
+
+EOS
+
+
+### Input and Output
+
+say count_unsorted(@ARGV);
+
+
+### Implementation
+
+# Compare each word with its sorted bytes, find any divergences and
+# count the concerned words.
+
+sub count_unsorted {
+ my $w = PDL::Char->new(@_);
+
+ which(($w <=> $w->qsort)->orover)->dim(0);
+}
+
+
+### Examples and tests
+
+sub run_tests {
+ SKIP: {
+ skip "examples" unless $examples;
+
+ is count_unsorted('abc', 'xyz', 'tsu'), 1, 'example 1';
+ is count_unsorted('rat', 'cab', 'dad'), 3, 'example 2';
+ is count_unsorted('x', 'y', 'z'), 0, 'example 3';
+ }
+
+ SKIP: {
+ skip "tests" unless $tests;
+ }
+
+ done_testing;
+ exit;
+}
diff --git a/challenge-215/jo-37/perl/ch-2.pl b/challenge-215/jo-37/perl/ch-2.pl
new file mode 100755
index 0000000000..ef6acca9b4
--- /dev/null
+++ b/challenge-215/jo-37/perl/ch-2.pl
@@ -0,0 +1,64 @@
+#!/usr/bin/perl -s
+
+use v5.24;
+use Test2::V0 '!float';
+use PDL v2.017;
+
+our ($tests, $examples, $count);
+
+run_tests() if $tests || $examples; # does not return
+
+die <<EOS unless @ARGV && $count;
+usage: $0 [-examples] [-tests] [-count=C] [N...]
+
+-examples
+ run the examples from the challenge
+
+-tests
+ run some tests
+
+-count=C
+ check if C zeroes may be flipped
+
+N...
+ list of zeroes and ones
+
+EOS
+
+
+### Input and Output
+
+say 0 + ($count <= replace_count(@ARGV));
+
+
+### Implementation
+
+# For every two zeroes following a single leading zero, one zero may be
+# flipped. Count sequential zeros and sum over the number of zeroes
+# that may be flipped.
+sub replace_count {
+ my ($count, $val) = rle long @_;
+
+ sum +($count->where($val == 0) - 1) / 2;
+}
+
+
+### Examples and tests
+
+sub run_tests {
+ SKIP: {
+ skip "examples" unless $examples;
+
+ ok replace_count([1,0,0,0,1], 1), 'example 1';
+ ok replace_count([1,0,0,0,0,0,0,0,1], 3), 'example 3';
+ }
+
+ SKIP: {
+ skip "tests" unless $tests;
+
+ ok replace_count([0,0,0,0,0,1,0,0,0,0,0], 4), 'two blocks of 5 zeroes';
+ }
+
+ done_testing;
+ exit;
+}