aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-219/athanasius/perl/ch-1.pl149
-rw-r--r--challenge-219/athanasius/raku/ch-1.raku159
2 files changed, 308 insertions, 0 deletions
diff --git a/challenge-219/athanasius/perl/ch-1.pl b/challenge-219/athanasius/perl/ch-1.pl
new file mode 100644
index 0000000000..7b1445503f
--- /dev/null
+++ b/challenge-219/athanasius/perl/ch-1.pl
@@ -0,0 +1,149 @@
+#!perl
+
+################################################################################
+=comment
+
+Perl Weekly Challenge 219
+=========================
+
+TASK #1
+-------
+*Sorted Squares*
+
+Submitted by: Mohammad S Anwar
+
+You are given a list of numbers.
+
+Write a script to square each number in the list and return the sorted list,
+increasing order.
+
+Example 1
+
+ Input: @list = (-2, -1, 0, 3, 4)
+ Output: (0, 1, 4, 9, 16)
+
+Example 2
+
+ Input: @list = (5, -4, -1, 3, 6)
+ Output: (1, 9, 16, 25, 36)
+
+=cut
+################################################################################
+
+#--------------------------------------#
+# Copyright © 2023 PerlMonk Athanasius #
+#--------------------------------------#
+
+#===============================================================================
+=comment
+
+Assumption
+----------
+The input numbers are integers.
+
+Interface
+---------
+If no command-line arguments are given, the test suite is run.
+
+=cut
+#===============================================================================
+
+use strict;
+use warnings;
+use Const::Fast;
+use Regexp::Common qw( number );
+use Test::More;
+
+const my $USAGE =>
+"Usage:
+ perl $0 [<list> ...]
+ perl $0
+
+ [<list> ...] A list of 1 or more integers\n";
+
+#-------------------------------------------------------------------------------
+BEGIN
+#-------------------------------------------------------------------------------
+{
+ $| = 1;
+ print "\nChallenge 219, Task #1: Sorted Squares (Perl)\n\n";
+}
+
+#===============================================================================
+MAIN:
+#===============================================================================
+{
+ my $args = scalar @ARGV;
+
+ if ($args == 0)
+ {
+ run_tests();
+ }
+ else
+ {
+ / ^ $RE{num}{int} $ /x or error( qq[Invalid integer "$_"] ) for @ARGV;
+
+ printf "Input: \@list = (%s)\n", join ', ', @ARGV;
+
+ my $output = square_and_sort( \@ARGV );
+
+ printf "Output: (%s)\n", join ', ', @$output;
+ }
+}
+
+#-------------------------------------------------------------------------------
+sub square_and_sort
+#-------------------------------------------------------------------------------
+{
+ my ($list) = @_;
+ my @solution;
+
+ push @solution, $_ * $_ for @$list;
+
+ @solution = sort { $a <=> $b } @solution;
+
+ return \@solution;
+}
+
+#-------------------------------------------------------------------------------
+sub run_tests
+#-------------------------------------------------------------------------------
+{
+ print "Running the test suite\n";
+
+ while (my $line = <DATA>)
+ {
+ chomp $line;
+
+ my ($test_name, $list_str, $expd_str) = split / \| /x, $line;
+
+ for ($test_name, $list_str, $expd_str) # Trim whitespace
+ {
+ s/ ^ \s+ //x;
+ s/ \s+ $ //x;
+ }
+
+ my @expected = split / \, \s* /x, $expd_str;
+ my @input_list = split / \, \s* /x, $list_str;
+ my $solution = square_and_sort( \@input_list );
+
+ is_deeply $solution, \@expected, $test_name;
+ }
+
+ done_testing;
+}
+
+#-------------------------------------------------------------------------------
+sub error
+#-------------------------------------------------------------------------------
+{
+ my ($message) = @_;
+
+ die "ERROR: $message\n$USAGE";
+}
+
+################################################################################
+
+__DATA__
+Example 1|-2, -1, 0, 3, 4|0, 1, 4, 9, 16
+Example 2| 5, -4, -1, 3, 6|1, 9, 16, 25, 36
diff --git a/challenge-219/athanasius/raku/ch-1.raku b/challenge-219/athanasius/raku/ch-1.raku
new file mode 100644
index 0000000000..b828c66a3c
--- /dev/null
+++ b/challenge-219/athanasius/raku/ch-1.raku
@@ -0,0 +1,159 @@
+use v6d;
+
+################################################################################
+=begin comment
+
+Perl Weekly Challenge 219
+=========================
+
+TASK #1
+-------
+*Sorted Squares*
+
+Submitted by: Mohammad S Anwar
+
+You are given a list of numbers.
+
+Write a script to square each number in the list and return the sorted list,
+increasing order.
+
+Example 1
+
+ Input: @list = (-2, -1, 0, 3, 4)
+ Output: (0, 1, 4, 9, 16)
+
+Example 2
+
+ Input: @list = (5, -4, -1, 3, 6)
+ Output: (1, 9, 16, 25, 36)
+
+=end comment
+################################################################################
+
+#--------------------------------------#
+# Copyright © 2023 PerlMonk Athanasius #
+#--------------------------------------#
+
+#===============================================================================
+=begin comment
+
+Assumption
+----------
+The input numbers are integers.
+
+Interface
+---------
+1. If no command-line arguments are given, the test suite is run. Otherwise:
+2. If the first integer is negative, the input list must be preceded by '--'.
+
+=end comment
+#===============================================================================
+
+use Test;
+
+#-------------------------------------------------------------------------------
+BEGIN
+#-------------------------------------------------------------------------------
+{
+ "\nChallenge 219, Task #1: Sorted Squares (Raku)\n".put;
+}
+
+#===============================================================================
+multi sub MAIN
+(
+ #| A list of 1 or more integers
+
+ *@list where { .all ~~ Int:D && .elems > 0 }
+)
+#===============================================================================
+{
+ "Input: \@list = (%s)\n".printf: @list.join: ', ';
+
+ my Int @output = square-and-sort( @list );
+
+ "Output: (%s)\n".printf: @output.join: ', ';
+}
+
+#===============================================================================
+multi sub MAIN() # No input: run the test suite
+#===============================================================================
+{
+ run-tests();
+}
+
+#-------------------------------------------------------------------------------
+sub square-and-sort
+(
+ List:D[Int:D] $list where { .elems > 0 }
+--> List:D[Int:D]
+)
+#-------------------------------------------------------------------------------
+{
+ my Int @solution;
+
+ @solution.push: $_² for @$list;
+
+ @solution.= sort;
+
+ return @solution;
+}
+
+#-------------------------------------------------------------------------------
+sub run-tests()
+#-------------------------------------------------------------------------------
+{
+ 'Running the test suite'.put;
+
+ for test-data.lines -> Str $line
+ {
+ my Str ($test-name, $list-str, $expd-str) = $line.split: / \| /;
+
+ for $test-name, $list-str, $expd-str # Trim whitespace
+ {
+ s/ ^ \s+ //;
+ s/ \s+ $ //;
+ }
+
+ my Int @expected = $expd-str.split( / \, \s* / ).map: { .Int };
+ my Int @input-list = $list-str.split( / \, \s* / ).map: { .Int };
+ my Int @solution = square-and-sort( @input-list );
+
+ is-deeply @solution, @expected, $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|-2, -1, 0, 3, 4|0, 1, 4, 9, 16
+ Example 2| 5, -4, -1, 3, 6|1, 9, 16, 25, 36
+ END
+}
+
+################################################################################