aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-01-07 02:12:24 +0000
committerGitHub <noreply@github.com>2024-01-07 02:12:24 +0000
commita067852544a1a06839fdaf1edc3f847ba7e4340b (patch)
tree217945209be7e5ed032c41b63b8e120b8b57ab0e
parent683d39b7b0f3256ce396eb91b1fc541cc1f823f8 (diff)
parentd4c152ef4f3f5807e48f1914ff0e3b2e1c0b603d (diff)
downloadperlweeklychallenge-club-a067852544a1a06839fdaf1edc3f847ba7e4340b.tar.gz
perlweeklychallenge-club-a067852544a1a06839fdaf1edc3f847ba7e4340b.tar.bz2
perlweeklychallenge-club-a067852544a1a06839fdaf1edc3f847ba7e4340b.zip
Merge pull request #9352 from PerlMonk-Athanasius/branch-for-challenge-250
Perl & Raku solutions to Tasks 1 & 2 for Week 250
-rw-r--r--challenge-250/athanasius/perl/ch-1.pl177
-rw-r--r--challenge-250/athanasius/perl/ch-2.pl167
-rw-r--r--challenge-250/athanasius/raku/ch-1.raku180
-rw-r--r--challenge-250/athanasius/raku/ch-2.raku171
4 files changed, 695 insertions, 0 deletions
diff --git a/challenge-250/athanasius/perl/ch-1.pl b/challenge-250/athanasius/perl/ch-1.pl
new file mode 100644
index 0000000000..90046accda
--- /dev/null
+++ b/challenge-250/athanasius/perl/ch-1.pl
@@ -0,0 +1,177 @@
+#!perl
+
+################################################################################
+=comment
+
+Perl Weekly Challenge 250
+=========================
+
+TASK #1
+-------
+*Smallest Index*
+
+Submitted by: Mohammad S Anwar
+
+You are given an array of integers, @ints.
+
+Write a script to find the smallest index i such that i mod 10 == $ints[i]
+otherwise return -1.
+
+Example 1
+
+ Input: @ints = (0, 1, 2)
+ Output: 0
+
+ i=0: 0 mod 10 = 0 == $ints[0].
+ i=1: 1 mod 10 = 1 == $ints[1].
+ i=2: 2 mod 10 = 2 == $ints[2].
+ All indices have i mod 10 == $ints[i], so we return the smallest index 0.
+
+Example 2
+
+ Input: @ints = (4, 3, 2, 1)
+ Output: 2
+
+ i=0: 0 mod 10 = 0 != $ints[0].
+ i=1: 1 mod 10 = 1 != $ints[1].
+ i=2: 2 mod 10 = 2 == $ints[2].
+ i=3: 3 mod 10 = 3 != $ints[3].
+ 2 is the only index which has i mod 10 == $ints[i].
+
+Example 3
+
+ Input: @ints = (1, 2, 3, 4, 5, 6, 7, 8, 9, 0)
+ Output: -1
+ Explanation: No index satisfies i mod 10 == $ints[i].
+
+=cut
+################################################################################
+
+#--------------------------------------#
+# Copyright © 2024 PerlMonk Athanasius #
+#--------------------------------------#
+
+#===============================================================================
+=comment
+
+Interface
+---------
+If no command-line arguments are given, the test suite is run.
+
+Note
+----
+Negative numbers are allowed in the input. However, as array indices are never
+negative, the expression i mod 10 is never negative, and so the equality
+i mod 10 = ints[i] can never be satisfied when the array element ints[i] is
+negative.
+
+=cut
+#===============================================================================
+
+use v5.32.1; # Enables strictures
+use warnings;
+use Const::Fast;
+use Regexp::Common qw( number );
+use Test::More;
+
+const my $USAGE => <<END;
+Usage:
+ perl $0 [<ints> ...]
+ perl $0
+
+ [<ints> ...] A non-empty list of integers
+END
+
+#-------------------------------------------------------------------------------
+BEGIN
+#-------------------------------------------------------------------------------
+{
+ $| = 1;
+ print "\nChallenge 250, Task #1: Smallest Index (Perl)\n\n";
+}
+
+#===============================================================================
+MAIN:
+#===============================================================================
+{
+ if (scalar @ARGV == 0)
+ {
+ run_tests();
+ }
+ else
+ {
+ my @ints = @ARGV;
+
+ / ^ $RE{num}{int} $ /x or error( qq["$_" is not a valid integer] )
+ for @ints;
+
+ printf "Input: \@ints = (%s)\n", join ', ', @ints;
+
+ my $smallest_index = find_smallest_index( \@ints );
+
+ print "Output: $smallest_index\n";
+ }
+}
+
+#-------------------------------------------------------------------------------
+sub find_smallest_index
+#-------------------------------------------------------------------------------
+{
+ my ($ints) = @_;
+ my $smallest_index = -1;
+
+ for my $i (0 .. $#$ints)
+ {
+ if ($i % 10 == $ints->[ $i ])
+ {
+ $smallest_index = $i;
+ last;
+ }
+ }
+
+ return $smallest_index;
+}
+
+#-------------------------------------------------------------------------------
+sub run_tests
+#-------------------------------------------------------------------------------
+{
+ print "Running the test suite\n";
+
+ while (my $line = <DATA>)
+ {
+ chomp $line;
+
+ my ($test_name, $ints_str, $expected) = split / \| /x, $line;
+
+ for ($test_name, $ints_str, $expected)
+ {
+ s/ ^ \s+ //x;
+ s/ \s+ $ //x;
+ }
+
+ my @ints = split / \s+ /x, $ints_str;
+ my $smallest_index = find_smallest_index( \@ints );
+
+ is $smallest_index, $expected, $test_name;
+ }
+
+ done_testing;
+}
+
+#-------------------------------------------------------------------------------
+sub error
+#-------------------------------------------------------------------------------
+{
+ my ($message) = @_;
+
+ die "ERROR: $message\n$USAGE";
+}
+
+################################################################################
+
+__DATA__
+Example 1|0 1 2 | 0
+Example 2|4 3 2 1 | 2
+Example 3|1 2 3 4 5 6 7 8 9 0|-1
+Negatives|1 2 3 -17 0 -5 |-1
diff --git a/challenge-250/athanasius/perl/ch-2.pl b/challenge-250/athanasius/perl/ch-2.pl
new file mode 100644
index 0000000000..5debc3e146
--- /dev/null
+++ b/challenge-250/athanasius/perl/ch-2.pl
@@ -0,0 +1,167 @@
+#!perl
+
+################################################################################
+=comment
+
+Perl Weekly Challenge 250
+=========================
+
+TASK #2
+-------
+*Alphanumeric String Value*
+
+Submitted by: Mohammad S Anwar
+
+You are given an array of alphanumeric strings.
+
+Write a script to return the maximum value of alphanumeric string in the given
+array.
+
+The value of alphanumeric string can be defined as
+
+ a) The numeric representation of the string in base 10 if it is made up of
+ digits only.
+ b) otherwise the length of the string
+
+Example 1
+
+ Input: @alphanumstr = ("perl", "2", "000", "python", "r4ku")
+ Output: 6
+
+ "perl" consists of letters only so the value is 4.
+ "2" is digits only so the value is 2.
+ "000" is digits only so the value is 0.
+ "python" consits of letters so the value is 6.
+ "r4ku" consists of letters and digits so the value is 4.
+
+Example 2
+
+ Input: @alphanumstr = ("001", "1", "000", "0001")
+ Output: 1
+
+=cut
+################################################################################
+
+#--------------------------------------#
+# Copyright © 2024 PerlMonk Athanasius #
+#--------------------------------------#
+
+#===============================================================================
+=comment
+
+Assumptions
+-----------
+1. An "alphanumeric string" contains letters and/or digits only. So, "+", "-",
+ "_", etc., are not allowed.
+2. An empty string has no digits, therefore its value is its length (viz. 0).
+
+Interface
+---------
+If no command-line arguments are given, the test suite is run.
+
+=cut
+#===============================================================================
+
+use v5.32.1; # Enables strictures
+use warnings;
+use Const::Fast;
+use Test::More;
+
+const my $USAGE => <<END;
+Usage:
+ perl $0 [<alphanumstr> ...]
+ perl $0
+
+ [<alphanumstr> ...] A non-empty list of alphanumeric strings
+END
+
+#-------------------------------------------------------------------------------
+BEGIN
+#-------------------------------------------------------------------------------
+{
+ $| = 1;
+ print "\nChallenge 250, Task #2: Alphanumeric String Value (Perl)\n\n";
+}
+
+#===============================================================================
+MAIN:
+#===============================================================================
+{
+ if (scalar @ARGV == 0)
+ {
+ run_tests();
+ }
+ else
+ {
+ my @alphanumstr = @ARGV;
+
+ / ^ [A-Za-z0-9]* $ /x or error( qq[Invalid input string "$_"] )
+ for @alphanumstr;
+
+ printf "Input: \@alphanumstr = (%s)\n",
+ join ', ', map { qq["$_"] } @alphanumstr;
+
+ my $max_value = find_max_value( \@alphanumstr );
+
+ print "Output: $max_value\n";
+ }
+}
+
+#-------------------------------------------------------------------------------
+sub find_max_value
+#-------------------------------------------------------------------------------
+{
+ my ($alphanumstr) = @_;
+ my $max_value = 0;
+
+ for (@$alphanumstr)
+ {
+ my $value = (/ ^ [0-9]+ $ /x ? $_ : length) + 0; # Numify the string
+
+ $max_value = $value if $value > $max_value;
+ }
+
+ return $max_value;
+}
+
+#-------------------------------------------------------------------------------
+sub run_tests
+#-------------------------------------------------------------------------------
+{
+ print "Running the test suite\n";
+
+ while (my $line = <DATA>)
+ {
+ chomp $line;
+
+ my ($test_name, $alphanumstrs, $expected) = split / \| /x, $line;
+
+ for ($test_name, $alphanumstrs, $expected)
+ {
+ s/ ^ \s+ //x;
+ s/ \s+ $ //x;
+ }
+
+ my @alphanumstr = split / \s+ /x, $alphanumstrs;
+ my $max_value = find_max_value( \@alphanumstr );
+
+ is $max_value, $expected, $test_name;
+ }
+
+ done_testing;
+}
+
+#-------------------------------------------------------------------------------
+sub error
+#-------------------------------------------------------------------------------
+{
+ my ($message) = @_;
+
+ die "ERROR: $message\n$USAGE";
+}
+
+################################################################################
+
+__DATA__
+Example 1|perl 2 000 python r4ku|6
+Example 2|001 1 000 0001 |1
diff --git a/challenge-250/athanasius/raku/ch-1.raku b/challenge-250/athanasius/raku/ch-1.raku
new file mode 100644
index 0000000000..691c642d12
--- /dev/null
+++ b/challenge-250/athanasius/raku/ch-1.raku
@@ -0,0 +1,180 @@
+use v6d;
+
+################################################################################
+=begin comment
+
+Perl Weekly Challenge 250
+=========================
+
+TASK #1
+-------
+*Smallest Index*
+
+Submitted by: Mohammad S Anwar
+
+You are given an array of integers, @ints.
+
+Write a script to find the smallest index i such that i mod 10 == $ints[i]
+otherwise return -1.
+
+Example 1
+
+ Input: @ints = (0, 1, 2)
+ Output: 0
+
+ i=0: 0 mod 10 = 0 == $ints[0].
+ i=1: 1 mod 10 = 1 == $ints[1].
+ i=2: 2 mod 10 = 2 == $ints[2].
+ All indices have i mod 10 == $ints[i], so we return the smallest index 0.
+
+Example 2
+
+ Input: @ints = (4, 3, 2, 1)
+ Output: 2
+
+ i=0: 0 mod 10 = 0 != $ints[0].
+ i=1: 1 mod 10 = 1 != $ints[1].
+ i=2: 2 mod 10 = 2 == $ints[2].
+ i=3: 3 mod 10 = 3 != $ints[3].
+ 2 is the only index which has i mod 10 == $ints[i].
+
+Example 3
+
+ Input: @ints = (1, 2, 3, 4, 5, 6, 7, 8, 9, 0)
+ Output: -1
+ Explanation: No index satisfies i mod 10 == $ints[i].
+
+=end comment
+################################################################################
+
+#--------------------------------------#
+# Copyright © 2024 PerlMonk Athanasius #
+#--------------------------------------#
+
+#===============================================================================
+=begin comment
+
+Interface
+---------
+1. If no command-line arguments are given, the test suite is run. Otherwise:
+2. If the first command-line argument is negative, it must be preceded by "--"
+ to distinguish it from a command-line switch.
+
+Note
+----
+Negative numbers are allowed in the input. However, as array indices are never
+negative, the expression i mod 10 is never negative, and so the equality
+i mod 10 = ints[i] can never be satisfied when the array element ints[i] is
+negative.
+
+=end comment
+#===============================================================================
+
+use Test;
+
+#-------------------------------------------------------------------------------
+BEGIN
+#-------------------------------------------------------------------------------
+{
+ "\nChallenge 250, Task #1: Smallest Index (Raku)\n".put;
+}
+
+#===============================================================================
+multi sub MAIN
+(
+ *@ints where { .elems > 0 && .all ~~ Int:D } #= A non-empty list of integers
+)
+#===============================================================================
+{
+ "Input: \@ints = (%s)\n".printf: @ints.join: ', ';
+
+ my Int $smallest-index = find-smallest-index( @ints );
+
+ "Output: $smallest-index".put;
+}
+
+#===============================================================================
+multi sub MAIN() # No input: run the test suite
+#===============================================================================
+{
+ run-tests();
+}
+
+#-------------------------------------------------------------------------------
+sub find-smallest-index( List:D[Int:D] $ints --> Int:D )
+#-------------------------------------------------------------------------------
+{
+ my Int $smallest-index = -1;
+
+ for 0 .. $ints.end -> UInt $i
+ {
+ if $i % 10 == $ints[ $i ]
+ {
+ $smallest-index = $i;
+ last;
+ }
+ }
+
+ return $smallest-index;
+}
+
+#-------------------------------------------------------------------------------
+sub run-tests()
+#-------------------------------------------------------------------------------
+{
+ 'Running the test suite'.put;
+
+ for test-data.lines -> Str $line
+ {
+ my Str ($test-name, $ints-str, $expected) = $line.split: / \| /;
+
+ for $test-name, $ints-str, $expected
+ {
+ s/ ^ \s+ //;
+ s/ \s+ $ //;
+ }
+
+ my Int @ints = $ints-str.split( / \s+ / ).map: { .Int };
+ my Int $smallest-index = find-smallest-index( @ints );
+
+ is $smallest-index, $expected.Int, $test-name;
+ }
+
+ done-testing;
+}
+
+#-------------------------------------------------------------------------------
+sub error( Str:D $message )
+#-------------------------------------------------------------------------------
+{
+ "ERROR: $message".put;
+
+ USAGE();
+
+ exit 0;
+}
+
+#-------------------------------------------------------------------------------
+sub USAGE()
+#-------------------------------------------------------------------------------
+{
+ my Str $usage = $*USAGE;
+
+ $usage ~~ s:g/ ($*PROGRAM-NAME) /raku $0/;
+
+ $usage.put;
+}
+
+#-------------------------------------------------------------------------------
+sub test-data( --> Str:D )
+#-------------------------------------------------------------------------------
+{
+ return q:to/END/;
+ Example 1|0 1 2 | 0
+ Example 2|4 3 2 1 | 2
+ Example 3|1 2 3 4 5 6 7 8 9 0|-1
+ Negatives|1 2 3 -17 0 -5 |-1
+ END
+}
+
+################################################################################
diff --git a/challenge-250/athanasius/raku/ch-2.raku b/challenge-250/athanasius/raku/ch-2.raku
new file mode 100644
index 0000000000..39bf655fda
--- /dev/null
+++ b/challenge-250/athanasius/raku/ch-2.raku
@@ -0,0 +1,171 @@
+use v6d;
+
+################################################################################
+=begin comment
+
+Perl Weekly Challenge 250
+=========================
+
+TASK #2
+-------
+*Alphanumeric String Value*
+
+Submitted by: Mohammad S Anwar
+
+You are given an array of alphanumeric strings.
+
+Write a script to return the maximum value of alphanumeric string in the given
+array.
+
+The value of alphanumeric string can be defined as
+
+ a) The numeric representation of the string in base 10 if it is made up of
+ digits only.
+ b) otherwise the length of the string
+
+Example 1
+
+ Input: @alphanumstr = ("perl", "2", "000", "python", "r4ku")
+ Output: 6
+
+ "perl" consists of letters only so the value is 4.
+ "2" is digits only so the value is 2.
+ "000" is digits only so the value is 0.
+ "python" consits of letters so the value is 6.
+ "r4ku" consists of letters and digits so the value is 4.
+
+Example 2
+
+ Input: @alphanumstr = ("001", "1", "000", "0001")
+ Output: 1
+
+=end comment
+################################################################################
+
+#--------------------------------------#
+# Copyright © 2024 PerlMonk Athanasius #
+#--------------------------------------#
+
+#===============================================================================
+=begin comment
+
+Assumptions
+-----------
+1. An "alphanumeric string" contains letters and/or digits only. So, "+", "-",
+ "_", etc., are not allowed.
+2. An empty string has no digits, therefore its value is its length (viz. 0).
+
+Interface
+---------
+If no command-line arguments are given, the test suite is run.
+
+=end comment
+#===============================================================================
+
+use Test;
+
+#-------------------------------------------------------------------------------
+BEGIN
+#-------------------------------------------------------------------------------
+{
+ "\nChallenge 250, Task #2: Alphanumeric String Value (Raku)\n".put;
+}
+
+#===============================================================================
+multi sub MAIN
+(
+ #| A non-empty list of alphanumeric strings
+
+ *@alphanumstr where { .elems > 0 && .all ~~ / ^ <[ A..Z a..z 0..9 ]>* $ / }
+)
+#===============================================================================
+{
+ "Input: \@alphanumstr = (%s)\n".printf:
+ @alphanumstr.map( { qq["$_"] } ).join: ', ';
+
+ my UInt $max-value = find-max-value( @alphanumstr );
+
+ "Output: $max-value".put;
+}
+
+#===============================================================================
+multi sub MAIN() # No input: run the test suite
+#===============================================================================
+{
+ run-tests();
+}
+
+#-------------------------------------------------------------------------------
+sub find-max-value( List:D[Str:D] $alphanumstr --> UInt:D )
+#-------------------------------------------------------------------------------
+{
+ my UInt $max-value = 0;
+
+ for @$alphanumstr
+ {
+ my UInt $value = / ^ <[ 0 .. 9 ]>+ $ / ?? .Int !! .chars;
+
+ $max-value = ($value, $max-value).max;
+ }
+
+ return $max-value;
+}
+
+#-------------------------------------------------------------------------------
+sub run-tests()
+#-------------------------------------------------------------------------------
+{
+ 'Running the test suite'.put;
+
+ for test-data.lines -> Str $line
+ {
+ my Str ($test-name, $alphanumstrs, $expected) = $line.split: / \| /;
+
+ for $test-name, $alphanumstrs, $expected
+ {
+ s/ ^ \s+ //;
+ s/ \s+ $ //;
+ }
+
+ my Str @alphanumstr = $alphanumstrs.split: / \s+ /;
+ my UInt $max-value = find-max-value( @alphanumstr );
+
+ is $max-value, $expected.Int, $test-name;
+ }
+
+ done-testing;
+}
+
+#-------------------------------------------------------------------------------
+sub error( Str:D $message )
+#-------------------------------------------------------------------------------
+{
+ "ERROR: $message".put;
+
+ USAGE();
+
+ exit 0;
+}
+
+#-------------------------------------------------------------------------------
+sub USAGE()
+#-------------------------------------------------------------------------------
+{
+ my Str $usage = $*USAGE;
+
+ $usage ~~ s:g/ ($*PROGRAM-NAME) /raku $0/;
+
+ $usage.put;
+}
+
+#-------------------------------------------------------------------------------
+sub test-data( --> Str:D )
+#-------------------------------------------------------------------------------
+{
+ return q:to/END/;
+ Example 1|perl 2 000 python r4ku|6
+ Example 2|001 1 000 0001 |1
+ END
+}
+
+################################################################################