aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPip <Pip@CPAN.Org>2023-03-05 06:45:53 -0600
committerPip <Pip@CPAN.Org>2023-03-05 06:45:53 -0600
commit2edc8565e58f0505cb952e1447c1fcb192e98399 (patch)
treed80c3d877908fcb8ea47ec0126a516d128188c69
parent67c3eb984b684e9bc48cd7a2de9dc4487226e70c (diff)
downloadperlweeklychallenge-club-2edc8565e58f0505cb952e1447c1fcb192e98399.tar.gz
perlweeklychallenge-club-2edc8565e58f0505cb952e1447c1fcb192e98399.tar.bz2
perlweeklychallenge-club-2edc8565e58f0505cb952e1447c1fcb192e98399.zip
Pip Stuart's submission for challenge-206.
-rw-r--r--challenge-206/pip/perl/ch-1.pl45
-rw-r--r--challenge-206/pip/perl/ch-2.pl48
-rw-r--r--challenge-206/pip/raku/ch-1.raku45
-rw-r--r--challenge-206/pip/raku/ch-2.raku48
4 files changed, 186 insertions, 0 deletions
diff --git a/challenge-206/pip/perl/ch-1.pl b/challenge-206/pip/perl/ch-1.pl
new file mode 100644
index 0000000000..deaa94b61d
--- /dev/null
+++ b/challenge-206/pip/perl/ch-1.pl
@@ -0,0 +1,45 @@
+#!/usr/bin/perl
+# HTTPS://TheWeeklyChallenge.Org - Perl/Raku Weekly Challenge #206 - Pip Stuart
+# Task1: Shortest Time: Submitted by: Mohammad S Anwar; You are given a list of time points, at least 2, in the 24-hour clock format HH:MM.
+# Write a script to find out the shortest time in minutes between any two time points.
+# Example1:
+# In-put: @time = ("00:00", "23:55", "20:00" )
+# Output: 5 Since the difference between "00:00" and "23:55" is the shortest (5 minutes).
+# Example2:
+# In-put: @time = ("01:01", "00:50", "00:57" )
+# Output: 4
+# Example3:
+# In-put: @time = ("10:10", "09:30", "09:00", "09:55")
+# Output: 15
+use strict;use warnings;use utf8;use v5.12;my $d8VS='N34LAnds';
+sub ShTm {my @time = @_;my $shtm = 0;my @tmnz = ();my %tdfz = ();my $sinc = '';
+ if ( @time) {
+ for ( 0 ..($#time-1)) {
+ if ( $#time) {
+ for my $scnd (($_+1).. $#time ) {
+ my @hhmm = split(/:/, $time[$_ ]);
+ $tmnz[$_ ] = $hhmm[0] * 60 + $hhmm[1];
+ @hhmm = split(/:/, $time[$scnd]);
+ $tmnz[$scnd] = $hhmm[0] * 60 + $hhmm[1];
+ if ($tmnz[$_ ] < $tmnz[$scnd]) {
+ $tdfz{ $tmnz[$scnd] - $tmnz[$_ ] } = "\"$time[$_ ]\" and \"$time[$scnd]\"";
+ $tdfz{ $tmnz[$_ ] + (24 * 60) - $tmnz[$scnd] } = "\"$time[$scnd]\" and \"$time[$_ ]\"";
+ } else {
+ $tdfz{ $tmnz[$_ ] - $tmnz[$scnd] } = "\"$time[$scnd]\" and \"$time[$_ ]\"";
+ $tdfz{ $tmnz[$scnd] + (24 * 60) - $tmnz[$_ ] } = "\"$time[$_ ]\" and \"$time[$scnd]\"";
+ } } } }
+ if ( %tdfz ) {
+ my @stdf = sort { $a <=> $b } keys(%tdfz);
+ $shtm = $stdf[0]; # max shortest time should be is 720 minutes for just 2 times exactly 12 hours off from each other
+ $sinc = "Since the difference between $tdfz{$shtm} is the shortest ($shtm minutes).";
+ } }
+ printf("(%-34s) => %3d; %s\n", sprintf('"%s"', join('", "', @time)), $shtm, $sinc);
+ return($shtm);
+}
+if (@ARGV) {
+ ShTm(@ARGV);
+} else {
+ ShTm("00:00", "23:55", "20:00" ); # => 5;
+ ShTm("01:01", "00:50", "00:57" ); # => 4;
+ ShTm("10:10", "09:30", "09:00", "09:55"); # => 15;
+}
diff --git a/challenge-206/pip/perl/ch-2.pl b/challenge-206/pip/perl/ch-2.pl
new file mode 100644
index 0000000000..ffe7084a62
--- /dev/null
+++ b/challenge-206/pip/perl/ch-2.pl
@@ -0,0 +1,48 @@
+#!/usr/bin/perl
+# HTTPS://TheWeeklyChallenge.Org - Perl/Raku Weekly Challenge #206 - Pip Stuart
+# Task2: Array Pairings: Submitted by: Mohammad S Anwar; You are given an array of integers having even number of elements.
+# Write a script to find the maximum sum of the minimum of each pairs.
+# Example1:
+# In-put: @array = (1,2,3,4)
+# Output: 4 Possible Pairings are as below:
+# a) (1,2) and (3,4). So min(1,2) + min(3,4) => 1 + 3 => 4
+# b) (1,3) and (2,4). So min(1,3) + min(2,4) => 1 + 2 => 3
+# c) (1,4) and (2,3). So min(1,4) + min(2,3) => 2 + 1 => 3
+# So the maximum sum is 4.
+# Example2:
+# In-put: @array = (0,2,1,3)
+# Output: 2 Possible Pairings are as below:
+# a) (0,2) and (1,3). So min(0,2) + min(1,3) => 0 + 1 => 1
+# b) (0,1) and (2,3). So min(0,1) + min(2,3) => 0 + 2 => 2
+# c) (0,3) and (2,1). So min(0,3) + min(2,1) => 0 + 1 => 1
+# So the maximum sum is 2.
+# Last date to submit the solution 23:59 (UK Time) Sunday 5th March 2023.
+use strict;use warnings;use utf8;use v5.12;my $d8VS='N34LHtJk';my @para=();my %sumz=();
+sub APrz {my @aray = @_; # recursively loop through all possible pairs
+ if (!(@aray % 2)) {my $parz; # don't really need this pairz since TempARrAy below tracks removed pairs
+ for my $andx ( 1 .. $#aray ) {$parz = '';my @tara = @aray;
+ my $frst = shift (@tara); # pair up first value with each of the remaining values
+ my $scnd = splice(@tara, $andx - 1, 1);
+ push(@para,"$frst,$scnd"); # track pair progress
+ $parz .= $para[-1];
+ if (@tara) { $parz .= APrz(@tara); } # recurse...
+ else {my $psum = 0; # no pairs left so compute max sum of min of each pair
+ for (@para) {($frst,$scnd) = split(/,/,$_);
+ $psum += ($frst < $scnd) ? $frst : $scnd; }
+ $sumz{$psum} = join(' ', @para);
+# say "@para => $psum;"; # this will print every set of pairs with their sum
+ } pop (@para);
+ } return($parz); } }
+sub MSMP {my @aray = @_;my $msmp = 0; # Max Sum of Min of Pairs
+ if (!(@aray % 2)) {@para=();%sumz=(); # make sure there's an even number of elements to pair
+ APrz(@aray);
+ my @srts = sort { $a <=> $b } keys(%sumz);
+ $msmp = $srts[-1]; } # get Max Sum from last element of numerically sorted sumz
+ printf("(%-7s) => %d;\n", join(',', @aray), $msmp);
+ return($msmp); }
+if (@ARGV) {
+ MSMP(@ARGV);
+} else {
+ MSMP(1,2,3,4); # => 4;
+ MSMP(0,2,1,3); # => 2;
+}
diff --git a/challenge-206/pip/raku/ch-1.raku b/challenge-206/pip/raku/ch-1.raku
new file mode 100644
index 0000000000..5b753a54c5
--- /dev/null
+++ b/challenge-206/pip/raku/ch-1.raku
@@ -0,0 +1,45 @@
+#!/usr/bin/env raku
+# HTTPS://TheWeeklyChallenge.Org - Perl/Raku Weekly Challenge #206 - Pip Stuart
+# Task1: Shortest Time: Submitted by: Mohammad S Anwar; You are given a list of time points, at least 2, in the 24-hour clock format HH:MM.
+# Write a script to find out the shortest time in minutes between any two time points.
+# Example1:
+# In-put: @time = ("00:00", "23:55", "20:00" )
+# Output: 5 Since the difference between "00:00" and "23:55" is the shortest (5 minutes).
+# Example2:
+# In-put: @time = ("01:01", "00:50", "00:57" )
+# Output: 4
+# Example3:
+# In-put: @time = ("10:10", "09:30", "09:00", "09:55")
+# Output: 15
+use v6;my $d8VS='N35L6TKO';
+sub ShTm {my @time = @_;my $shtm = 0;my @tmnz = ();my %tdfz = ();my $sinc = '';
+ if ( @time.elems) {
+ for ( 0 .. (@time.elems - 2)) {
+ if ( @time.elems - 1) {
+ for (($_+1).. (@time.elems - 1)) -> $scnd {
+ my @hhmm = split(':', @time[$_ ], :skip-empty);
+ @tmnz[$_ ] = @hhmm[0] * 60 + @hhmm[1];
+ @hhmm = split(':', @time[$scnd], :skip-empty);
+ @tmnz[$scnd] = @hhmm[0] * 60 + @hhmm[1];
+ if (@tmnz[$_ ] < @tmnz[$scnd]) {
+ %tdfz{ @tmnz[$scnd] - @tmnz[$_ ] } = "\"@time[$_ ]\" and \"@time[$scnd]\"";
+ %tdfz{ @tmnz[$_ ] + (24 * 60) - @tmnz[$scnd] } = "\"@time[$scnd]\" and \"@time[$_ ]\"";
+ } else {
+ %tdfz{ @tmnz[$_ ] - @tmnz[$scnd] } = "\"@time[$scnd]\" and \"@time[$_ ]\"";
+ %tdfz{ @tmnz[$scnd] + (24 * 60) - @tmnz[$_ ] } = "\"@time[$_ ]\" and \"@time[$scnd]\"";
+ } } } }
+ if ( %tdfz ) {
+ my @stdf = sort +*, keys(%tdfz);
+ $shtm = @stdf[0]; # max shortest time should be is 720 minutes for just 2 times exactly 12 hours off from each other
+ $sinc = "Since the difference between %tdfz{$shtm} is the shortest ($shtm minutes).";
+ } }
+ printf("(%-34s) => %3d; %s\n", sprintf('"%s"', join('", "', @time)), $shtm, $sinc);
+ return($shtm);
+}
+if (@*ARGS) {
+ ShTm(@*ARGS);
+} else {
+ ShTm("00:00", "23:55", "20:00" ); # => 5;
+ ShTm("01:01", "00:50", "00:57" ); # => 4;
+ ShTm("10:10", "09:30", "09:00", "09:55"); # => 15;
+}
diff --git a/challenge-206/pip/raku/ch-2.raku b/challenge-206/pip/raku/ch-2.raku
new file mode 100644
index 0000000000..0df8769050
--- /dev/null
+++ b/challenge-206/pip/raku/ch-2.raku
@@ -0,0 +1,48 @@
+#!/usr/bin/env raku
+# HTTPS://TheWeeklyChallenge.Org - Perl/Raku Weekly Challenge #206 - Pip Stuart
+# Task2: Array Pairings: Submitted by: Mohammad S Anwar; You are given an array of integers having even number of elements.
+# Write a script to find the maximum sum of the minimum of each pairs.
+# Example1:
+# In-put: @array = (1,2,3,4)
+# Output: 4 Possible Pairings are as below:
+# a) (1,2) and (3,4). So min(1,2) + min(3,4) => 1 + 3 => 4
+# b) (1,3) and (2,4). So min(1,3) + min(2,4) => 1 + 2 => 3
+# c) (1,4) and (2,3). So min(1,4) + min(2,3) => 2 + 1 => 3
+# So the maximum sum is 4.
+# Example2:
+# In-put: @array = (0,2,1,3)
+# Output: 2 Possible Pairings are as below:
+# a) (0,2) and (1,3). So min(0,2) + min(1,3) => 0 + 1 => 1
+# b) (0,1) and (2,3). So min(0,1) + min(2,3) => 0 + 2 => 2
+# c) (0,3) and (2,1). So min(0,3) + min(2,1) => 0 + 1 => 1
+# So the maximum sum is 2.
+# Last date to submit the solution 23:59 (UK Time) Sunday 5th March 2023.
+use v6;my $d8VS='N35L6Who';my @para=();my %sumz=();
+sub APrz {my @aray = @_; # recursively loop through all possible pairs
+ if (!(@aray % 2)) {my $parz; # don't really need this pairz since TempARrAy below tracks removed pairs
+ for 1 .. (@aray.elems-1) -> $andx {$parz = '';my @tara = @aray;
+ my $frst = shift (@tara); # pair up first value with each of the remaining values
+ my $scnd = splice(@tara, $andx - 1, 1);
+ push(@para,"$frst,$scnd"); # track pair progress
+ $parz ~= @para[*-1];
+ if (@tara) { $parz ~= APrz(@tara); } # recurse...
+ else {my $psum = 0; # no pairs left so compute max sum of min of each pair
+ for @para {($frst,$scnd) = split(',', $_, :skip-empty);
+ $psum += ($frst < $scnd) ?? $frst !! $scnd; }
+ %sumz{$psum} = join(' ', @para);
+# say "@para[] => $psum;"; # this will print every set of pairs with their sum
+ }; pop (@para);
+ }; return($parz); } }
+sub MSMP {my @aray = @_;my $msmp = 0; # Max Sum of Min of Pairs
+ if (!(@aray % 2)) {@para=();%sumz=(); # make sure there's an even number of elements to pair
+ APrz(@aray);
+ my @srts = sort +*, keys(%sumz);
+ $msmp = @srts[*-1]; } # get Max Sum from last element of numerically sorted sumz
+ printf("(%-7s) => %d;\n", join(',', @aray), $msmp);
+ return($msmp); }
+if (@*ARGS) {
+ MSMP(@*ARGS);
+} else {
+ MSMP(1,2,3,4); # => 4;
+ MSMP(0,2,1,3); # => 2;
+}