aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-09-14 10:43:16 +0100
committerGitHub <noreply@github.com>2024-09-14 10:43:16 +0100
commit5060425e517b41203da2edf06d1f664c8c124d36 (patch)
treebef48dda02085f6ce0515cbe7a748011dd2538da
parentacccded74bedaf3888e5dc60479959a0e7394f8a (diff)
parentdc99fdd2d3678e27f22c89cd98f49e04d7dad69c (diff)
downloadperlweeklychallenge-club-5060425e517b41203da2edf06d1f664c8c124d36.tar.gz
perlweeklychallenge-club-5060425e517b41203da2edf06d1f664c8c124d36.tar.bz2
perlweeklychallenge-club-5060425e517b41203da2edf06d1f664c8c124d36.zip
Merge pull request #10831 from PerlMonk-Athanasius/branch-for-challenge-286
Perl & Raku solutions to Tasks 1 & 2 for Week 286
-rw-r--r--challenge-286/athanasius/perl/ch-1.pl20
-rw-r--r--challenge-286/athanasius/perl/ch-2.pl246
-rw-r--r--challenge-286/athanasius/raku/ch-1.raku11
-rw-r--r--challenge-286/athanasius/raku/ch-2.raku229
4 files changed, 506 insertions, 0 deletions
diff --git a/challenge-286/athanasius/perl/ch-1.pl b/challenge-286/athanasius/perl/ch-1.pl
new file mode 100644
index 0000000000..1023b41e30
--- /dev/null
+++ b/challenge-286/athanasius/perl/ch-1.pl
@@ -0,0 +1,20 @@
+#!perl
+
+# Copyright © 2024 PerlMonk Athanasius
+
+use v5.32;
+use autodie;
+use List::Util 'shuffle';
+
+$| = 1;
+my %dict;
+
+open my $fh, '<', $0;
+
+while (my $line = <$fh>)
+{
+ ++$dict{$_} for grep { length } split /\s+/, $line;
+}
+
+close $fh;
+say +(shuffle keys %dict)[0];
diff --git a/challenge-286/athanasius/perl/ch-2.pl b/challenge-286/athanasius/perl/ch-2.pl
new file mode 100644
index 0000000000..03a1d8b4fb
--- /dev/null
+++ b/challenge-286/athanasius/perl/ch-2.pl
@@ -0,0 +1,246 @@
+#!perl
+
+################################################################################
+=comment
+
+Perl Weekly Challenge 286
+=========================
+
+TASK #2
+-------
+*Order Game*
+
+Submitted by: Mohammad Sajid Anwar
+
+You are given an array of integers, @ints, whose length is a power of 2.
+
+Write a script to play the order game (min and max) and return the last element.
+
+Example 1
+
+ Input: @ints = (2, 1, 4, 5, 6, 3, 0, 2)
+ Output: 1
+
+ Operation 1:
+
+ min(2, 1) = 1
+ max(4, 5) = 5
+ min(6, 3) = 3
+ max(0, 2) = 2
+
+ Operation 2:
+
+ min(1, 5) = 1
+ max(3, 2) = 3
+
+ Operation 3:
+
+ min(1, 3) = 1
+
+Example 2
+
+ Input: @ints = (0, 5, 3, 2)
+ Output: 0
+
+ Operation 1:
+
+ min(0, 5) = 0
+ max(3, 2) = 3
+
+ Operation 2:
+
+ min(0, 3) = 0
+
+Example 3
+
+ Input: @ints = (9, 2, 1, 4, 5, 6, 0, 7, 3, 1, 3, 5, 7, 9, 0, 8)
+ Output: 2
+
+ Operation 1:
+
+ min(9, 2) = 2
+ max(1, 4) = 4
+ min(5, 6) = 5
+ max(0, 7) = 7
+ min(3, 1) = 1
+ max(3, 5) = 5
+ min(7, 9) = 7
+ max(0, 8) = 8
+
+ Operation 2:
+
+ min(2, 4) = 2
+ max(5, 7) = 7
+ min(1, 5) = 1
+ max(7, 8) = 8
+
+ Operation 3:
+
+ min(2, 7) = 2
+ max(1, 8) = 8
+
+ Operation 4:
+
+ min(2, 8) = 2
+
+=cut
+################################################################################
+
+#--------------------------------------#
+# Copyright © 2024 PerlMonk Athanasius #
+#--------------------------------------#
+
+#===============================================================================
+=comment
+
+Interface
+---------
+1. If no command-line arguments are given, the test suite is run. Otherwise:
+2. A non-empty list of integers is entered on the command-line. The length of
+ the list must be a power of two.
+
+=cut
+#===============================================================================
+
+use v5.32; # Enables strictures and warnings
+use Const::Fast;
+use List::Util qw( max min );
+use Regexp::Common qw( number );
+use Test::More;
+
+const my $LOG2 => log 2;
+const my $USAGE => <<END;
+Usage:
+ perl $0 [<ints> ...]
+ perl $0
+
+ [<ints> ...] A non-empty list of integers whose length is a power of 2
+END
+
+#-------------------------------------------------------------------------------
+BEGIN
+#-------------------------------------------------------------------------------
+{
+ $| = 1;
+ print "\nChallenge 286, Task #2: Order Game (Perl)\n\n";
+}
+
+#===============================================================================
+MAIN:
+#===============================================================================
+{
+ my $argc = scalar @ARGV;
+
+ if ($argc == 0)
+ {
+ run_tests();
+ }
+ else
+ {
+ my $ints = parse_command_line();
+
+ printf "Input: \@ints = (%s)\n", join ', ', @$ints;
+
+ my $last_element = play_order_game( $ints );
+
+ print "Output: $last_element\n";
+ }
+}
+
+#-------------------------------------------------------------------------------
+sub play_order_game
+#-------------------------------------------------------------------------------
+{
+ my ($ints) = @_;
+
+ while (scalar @$ints > 1)
+ {
+ my ($lhs, $rhs, @temp);
+
+ while (@$ints)
+ {
+ $lhs = shift @$ints;
+ $rhs = shift @$ints;
+
+ push @temp, min( $lhs, $rhs );
+
+ if (@$ints)
+ {
+ $lhs = shift @$ints;
+ $rhs = shift @$ints;
+
+ push @temp, max( $lhs, $rhs );
+ }
+ }
+
+ @$ints = @temp;
+ }
+
+ return $ints->[ 0 ];
+}
+
+#-------------------------------------------------------------------------------
+sub parse_command_line
+#-------------------------------------------------------------------------------
+{
+ my @ints = @ARGV;
+
+ for my $int (@ints)
+ {
+ $int =~ / ^ $RE{num}{int} $ /x
+ or error( qq["$int" is not a valid integer] );
+ }
+
+ my $length = scalar @ints;
+ my $log = int( log( $length ) / $LOG2 + 0.5 );
+
+ 2 ** $log == $length
+ or error( 'The length of the input list is not a power of 2' );
+
+ return \@ints;
+}
+
+#-------------------------------------------------------------------------------
+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 $last = play_order_game( \@ints );
+
+ is $last, $expected, $test_name;
+ }
+
+ done_testing;
+}
+
+#-------------------------------------------------------------------------------
+sub error
+#-------------------------------------------------------------------------------
+{
+ my ($message) = @_;
+
+ die "ERROR: $message\n$USAGE";
+}
+
+################################################################################
+
+__DATA__
+Example 1| 2 1 4 5 6 3 0 2 | 1
+Example 2| 0 5 3 2 | 0
+Example 3| 9 2 1 4 5 6 0 7 3 1 3 5 7 9 0 8| 2
+Singleton|42 |42
+Negatives|-9 0 -1 5 |-9
diff --git a/challenge-286/athanasius/raku/ch-1.raku b/challenge-286/athanasius/raku/ch-1.raku
new file mode 100644
index 0000000000..12eaa715fe
--- /dev/null
+++ b/challenge-286/athanasius/raku/ch-1.raku
@@ -0,0 +1,11 @@
+use v6d;
+
+# Copyright © 2024 PerlMonk Athanasius
+
+sub MAIN()
+{
+ my %dict;
+ ++%dict{$_} for $*PROGRAM-NAME.IO.words;
+
+ %dict.keys.pick.say;
+}
diff --git a/challenge-286/athanasius/raku/ch-2.raku b/challenge-286/athanasius/raku/ch-2.raku
new file mode 100644
index 0000000000..1fd3bc08fe
--- /dev/null
+++ b/challenge-286/athanasius/raku/ch-2.raku
@@ -0,0 +1,229 @@
+use v6d;
+
+################################################################################
+=begin comment
+
+Perl Weekly Challenge 286
+=========================
+
+TASK #2
+-------
+*Order Game*
+
+Submitted by: Mohammad Sajid Anwar
+
+You are given an array of integers, @ints, whose length is a power of 2.
+
+Write a script to play the order game (min and max) and return the last element.
+
+Example 1
+
+ Input: @ints = (2, 1, 4, 5, 6, 3, 0, 2)
+ Output: 1
+
+ Operation 1:
+
+ min(2, 1) = 1
+ max(4, 5) = 5
+ min(6, 3) = 3
+ max(0, 2) = 2
+
+ Operation 2:
+
+ min(1, 5) = 1
+ max(3, 2) = 3
+
+ Operation 3:
+
+ min(1, 3) = 1
+
+Example 2
+
+ Input: @ints = (0, 5, 3, 2)
+ Output: 0
+
+ Operation 1:
+
+ min(0, 5) = 0
+ max(3, 2) = 3
+
+ Operation 2:
+
+ min(0, 3) = 0
+
+Example 3
+
+ Input: @ints = (9, 2, 1, 4, 5, 6, 0, 7, 3, 1, 3, 5, 7, 9, 0, 8)
+ Output: 2
+
+ Operation 1:
+
+ min(9, 2) = 2
+ max(1, 4) = 4
+ min(5, 6) = 5
+ max(0, 7) = 7
+ min(3, 1) = 1
+ max(3, 5) = 5
+ min(7, 9) = 7
+ max(0, 8) = 8
+
+ Operation 2:
+
+ min(2, 4) = 2
+ max(5, 7) = 7
+ min(1, 5) = 1
+ max(7, 8) = 8
+
+ Operation 3:
+
+ min(2, 7) = 2
+ max(1, 8) = 8
+
+ Operation 4:
+
+ min(2, 8) = 2
+
+=end comment
+################################################################################
+
+#--------------------------------------#
+# Copyright © 2024 PerlMonk Athanasius #
+#--------------------------------------#
+
+#===============================================================================
+=begin comment
+
+Interface
+---------
+1. If no command-line arguments are given, the test suite is run. Otherwise:
+2. A non-empty list of integers is entered on the command-line. The length of
+ the list must be a power of two.
+3. If the first integer is negative, it must be preceded by "--" to indicate
+ that it is not a command-line flag.
+
+=end comment
+#===============================================================================
+
+use Test;
+
+#-------------------------------------------------------------------------------
+BEGIN
+#-------------------------------------------------------------------------------
+{
+ "\nChallenge 286, Task #2: Order Game (Raku)\n".put;
+}
+
+#===============================================================================
+multi sub MAIN
+(
+ #| A non-empty list of integers whose length is a power of 2
+
+ *@ints where { .all ~~ Int:D && .elems > 0 && is-power-of-two( .elems ) }
+)
+#===============================================================================
+{
+ "Input: \@ints = (%s)\n".printf: @ints.join: ', ';
+
+ my Int $last-element = play-order-game( @ints );
+
+ "Output: $last-element".put;
+}
+
+#===============================================================================
+multi sub MAIN() # No input: run the test suite
+#===============================================================================
+{
+ run-tests();
+}
+
+#-------------------------------------------------------------------------------
+sub play-order-game( List:D[Int:D] $ints --> Int:D )
+#-------------------------------------------------------------------------------
+{
+ my Int @ints = @$ints;
+
+ while @ints.elems > 1
+ {
+ my Int ($lhs, $rhs, @temp);
+
+ while @ints
+ {
+ $lhs = @ints.shift;
+ $rhs = @ints.shift;
+
+ @temp.push: ($lhs, $rhs).min;
+
+ if @ints
+ {
+ $lhs = @ints.shift;
+ $rhs = @ints.shift;
+
+ @temp.push: ($lhs, $rhs).max;
+ }
+ }
+
+ @ints = @temp;
+ }
+
+ return @ints[ 0 ];
+}
+
+#-------------------------------------------------------------------------------
+sub is-power-of-two( UInt:D $n --> Bool:D )
+#-------------------------------------------------------------------------------
+{
+ my Int $log = ($n.log2 + 0.5).floor;
+
+ return 2 ** $log == $n;
+}
+
+#-------------------------------------------------------------------------------
+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+ /, :skip-empty ).map: { .Int };
+ my Int $last = play-order-game( @ints );
+
+ is $last, $expected.Int, $test-name;
+ }
+
+ done-testing;
+}
+
+#-------------------------------------------------------------------------------
+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 4 5 6 3 0 2 | 1
+ Example 2| 0 5 3 2 | 0
+ Example 3| 9 2 1 4 5 6 0 7 3 1 3 5 7 9 0 8| 2
+ Singleton|42 |42
+ Negatives|-9 0 -1 5 |-9
+ END
+}
+
+################################################################################