aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-01-05 20:58:12 +0000
committerGitHub <noreply@github.com>2024-01-05 20:58:12 +0000
commitaafbd16eecf5f9ffa115acfb7890c0ba3af39ee3 (patch)
treea16a20a86f2ab6b17027d0fefac8e655428ca5d7
parent985b747d40117104992cf0b9dcc1605ec8cc0e5d (diff)
parent9c36cc13455bf2bef049af6d14207183df6ddaaf (diff)
downloadperlweeklychallenge-club-aafbd16eecf5f9ffa115acfb7890c0ba3af39ee3.tar.gz
perlweeklychallenge-club-aafbd16eecf5f9ffa115acfb7890c0ba3af39ee3.tar.bz2
perlweeklychallenge-club-aafbd16eecf5f9ffa115acfb7890c0ba3af39ee3.zip
Merge pull request #9350 from jo-37/contrib
Solutions to challenge 250
-rw-r--r--challenge-250/jo-37/blog.txt1
-rwxr-xr-xchallenge-250/jo-37/perl/ch-1.pl63
-rwxr-xr-xchallenge-250/jo-37/perl/ch-2.pl54
3 files changed, 118 insertions, 0 deletions
diff --git a/challenge-250/jo-37/blog.txt b/challenge-250/jo-37/blog.txt
new file mode 100644
index 0000000000..63892b4690
--- /dev/null
+++ b/challenge-250/jo-37/blog.txt
@@ -0,0 +1 @@
+https://github.sommrey.de/blog/pwc/challenge-250
diff --git a/challenge-250/jo-37/perl/ch-1.pl b/challenge-250/jo-37/perl/ch-1.pl
new file mode 100755
index 0000000000..e734e957f3
--- /dev/null
+++ b/challenge-250/jo-37/perl/ch-1.pl
@@ -0,0 +1,63 @@
+#!/usr/bin/perl -s
+
+use v5.24;
+use Test2::V0;
+
+our ($tests, $examples, $base);
+
+run_tests() if $tests || $examples; # does not return
+
+die <<EOS unless @ARGV;
+usage: $0 [-examples] [-tests] [-base=B] [N...]
+
+-examples
+ run the examples from the challenge
+
+-tests
+ run some tests
+
+-base=B
+ use B as base. Default: 10
+
+N...
+ list of numbers
+
+EOS
+
+
+### Input and Output
+
+say smallest_index($base // 10, @ARGV);
+
+
+### Implementation
+
+sub smallest_index {
+ my $base = shift;
+ while (my ($i, $n) = each @_) {
+ return $i if $n == $i % $base;
+ }
+ -1;
+}
+
+
+### Examples and tests
+
+sub run_tests {
+ SKIP: {
+ skip "examples" unless $examples;
+
+ is smallest_index(10,=> 0, 1, 2), 0, 'example 1';
+ is smallest_index(10,=> 4, 3, 2, 1), 2, 'example 2';
+ is smallest_index(10,=> 1, 2, 3, 4, 5, 6, 7, 8, 9, 0), -1, 'example 3';
+ }
+
+ SKIP: {
+ skip "tests" unless $tests;
+
+ is smallest_index(2,=> 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1), 9, 'base 2';
+ }
+
+ done_testing;
+ exit;
+}
diff --git a/challenge-250/jo-37/perl/ch-2.pl b/challenge-250/jo-37/perl/ch-2.pl
new file mode 100755
index 0000000000..e94254968c
--- /dev/null
+++ b/challenge-250/jo-37/perl/ch-2.pl
@@ -0,0 +1,54 @@
+#!/usr/bin/perl -s
+
+use v5.24;
+use Test2::V0;
+use List::Util 'max';
+
+our ($tests, $examples);
+
+run_tests() if $tests || $examples; # does not return
+
+die <<EOS unless @ARGV;
+usage: $0 [-examples] [-tests] [...]
+
+-examples
+ run the examples from the challenge
+
+-tests
+ run some tests
+
+S...
+ list of strings
+
+EOS
+
+
+### Input and Output
+
+say max_val(@ARGV);
+
+
+### Implementation
+
+sub max_val {
+ max map /^\d+$/ ? 0 + $_ : length, @_;
+}
+
+
+### Examples and tests
+
+sub run_tests {
+ SKIP: {
+ skip "examples" unless $examples;
+
+ is max_val("perl", "2", "000", "python", "r4ku"), 6, 'example 1';
+ is max_val("001", "1", "000", "0001"), 1, 'example 2';
+ }
+
+ SKIP: {
+ skip "tests" unless $tests;
+ }
+
+ done_testing;
+ exit;
+}