aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2023-10-28 21:18:11 +0100
committerGitHub <noreply@github.com>2023-10-28 21:18:11 +0100
commitbcbf919bcb2e637210660bc55895bf2131848efe (patch)
tree4d21d217d5de3b8a160172e55f4b50c874a0dceb
parent561626aa7caed490946699bddc6f1d541840d292 (diff)
parent6cb4ef79a483c476ead69a342e7f8f730a42187a (diff)
downloadperlweeklychallenge-club-bcbf919bcb2e637210660bc55895bf2131848efe.tar.gz
perlweeklychallenge-club-bcbf919bcb2e637210660bc55895bf2131848efe.tar.bz2
perlweeklychallenge-club-bcbf919bcb2e637210660bc55895bf2131848efe.zip
Merge pull request #8953 from jo-37/contrib
Solutions to challenge 240
-rwxr-xr-xchallenge-240/jo-37/perl/ch-1.pl63
-rwxr-xr-xchallenge-240/jo-37/perl/ch-2.pl54
2 files changed, 117 insertions, 0 deletions
diff --git a/challenge-240/jo-37/perl/ch-1.pl b/challenge-240/jo-37/perl/ch-1.pl
new file mode 100755
index 0000000000..dcbfc1f5e5
--- /dev/null
+++ b/challenge-240/jo-37/perl/ch-1.pl
@@ -0,0 +1,63 @@
+#!/usr/bin/perl -s
+
+use v5.24;
+use Test2::V0;
+
+our ($tests, $examples, $acronym);
+
+run_tests() if $tests || $examples; # does not return
+
+die <<EOS unless $acronym && @ARGV;
+usage: $0 [-examples] [-tests] [-acronym=A WORD...]
+
+-examples
+ run the examples from the challenge
+
+-tests
+ run some tests
+
+-acronym=A
+ check words against acronym A
+
+WORD...
+ list of words
+
+EOS
+
+
+### Input and Output
+
+say is_acronym($acronym, @ARGV) ? 'true' : 'false';
+
+
+### Implementation
+
+# Pick the first character of each word, convert these to lower case and
+# compare the joined result to the lower case given acronym.
+sub is_acronym {
+ # Need two "map"s here because "lc" forces scalar context and the
+ # regex match needs to be in list context.
+ lc(shift) eq join '', map lc, map /(.)/, @_;
+}
+
+
+### Examples and tests
+
+sub run_tests {
+ SKIP: {
+ skip "examples" unless $examples;
+
+ ok is_acronym("ppp" => "Perl", "Python", "Pascal"), 'example 1';
+ ok !is_acronym("rp" => "Perl", "Raku"), 'example 2';
+ ok is_acronym("oac" => "Oracle", "Awk", "C"), 'example 3';
+ }
+
+ SKIP: {
+ skip "tests" unless $tests;
+
+ ok is_acronym("äöü" => "Ärger", "Öde", "Übel"), 'umlaute';
+ }
+
+ done_testing;
+ exit;
+}
diff --git a/challenge-240/jo-37/perl/ch-2.pl b/challenge-240/jo-37/perl/ch-2.pl
new file mode 100755
index 0000000000..6df72e95c8
--- /dev/null
+++ b/challenge-240/jo-37/perl/ch-2.pl
@@ -0,0 +1,54 @@
+#!/usr/bin/perl -s
+
+use v5.24;
+use Test2::V0;
+
+our ($tests, $examples);
+
+run_tests() if $tests || $examples; # does not return
+
+die <<EOS unless @ARGV;
+usage: $0 [-examples] [-tests] [P...]
+
+-examples
+ run the examples from the challenge
+
+-tests
+ run some tests
+
+P...
+ permutation of (0..N-1)
+
+EOS
+
+
+### Input and Output
+
+say "(@{build_array(@ARGV)})";
+
+
+### Implementation
+
+# Slice the array with itself.
+
+sub build_array {
+ [@_[@_]];
+}
+
+
+### Examples and tests
+
+sub run_tests {
+ SKIP: {
+ skip "examples" unless $examples;
+ is build_array(0, 2, 1, 5, 3, 4), [0, 1, 2, 4, 5, 3], 'example 1';
+ is build_array(5, 0, 1, 2, 3, 4), [4, 5, 0, 1, 2, 3], 'example 2';
+ }
+
+ SKIP: {
+ skip "tests" unless $tests;
+ }
+
+ done_testing;
+ exit;
+}