Skip to content

Commit 22df984

Browse files
authored
Merge pull request #7660 from robbie-hatley/206
Robbie Hatley's Perl solutions for PWCC 206
2 parents 0f4871c + 34661bc commit 22df984

File tree

3 files changed

+154
-0
lines changed

3 files changed

+154
-0
lines changed

challenge-206/robbie-hatley/blog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
https://hatley-software.blogspot.com/2023/03/robbie-hatleys-perl-solutions-to-weekly.html
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#! /usr/bin/perl
2+
# Robbie Hatley's Perl Solution to PWCC 206-1
3+
4+
=pod
5+
6+
Task 1: Shortest Time
7+
Submitted by: Mohammad S Anwar
8+
Write a program to find the shortest time between any two times in a given
9+
list of times, at least 2, in the 24-hour clock format HH:MM.
10+
Example 1: Input: ("00:00", "23:55", "20:00") Output: 5
11+
Example 2: Input: ("01:01", "00:50", "00:57") Output: 4
12+
Example 3: Input: ("10:10", "09:30", "09:00", "09:55") Output: 15
13+
14+
=cut
15+
16+
# IO NOTES:
17+
# NOTE: Input is by either built-in array-of-arrays, or @ARGV. If using @ARGV,
18+
# the args should be a space-separated sequence of 'single-quoted' 24HR
19+
# time strings of the form '00:23' (for 23min after midnight) or '15:32'
20+
# (for 32min after 3PM). This sequence will be considered to be a single
21+
# array of times.
22+
# NOTE: Output is to STDOUT and will be the shortest time duration.
23+
24+
# PRELIMINARIES:
25+
use v5.36;
26+
$"=", ";
27+
28+
# DEFAULT INPUTS:
29+
my @arrays =
30+
(
31+
["00:00", "23:55", "20:00"],
32+
["01:01", "00:50", "00:57"],
33+
["10:10", "09:30", "09:00", "09:55"]
34+
);
35+
36+
# NON-DEFAULT INPUTS:
37+
if (@ARGV) {@arrays = ([@ARGV]);}
38+
39+
# MAIN BODY OF SCRIPT:
40+
for (@arrays){
41+
my @array = @{$_};
42+
my @times = map {60*substr($_,0,2)+substr($_,3,2)} @array;
43+
my @diffs; # elapsed times
44+
for ( my $i = 0 ; $i <= $#times - 1 ; ++$i ){
45+
for ( my $j = $i + 1 ; $j <= $#times - 0 ; ++$j ){
46+
my ($t1, $t2) = sort {$a<=>$b} ($times[$i], $times[$j]);
47+
my $te = $t2 - $t1;
48+
if ($te > 720) {$te = 1440 - $te}
49+
push @diffs, $te}}
50+
my $mindiff = 720;
51+
for (@diffs) {if ($_<$mindiff) {$mindiff=$_}}
52+
say '';
53+
say "Times: @array";
54+
say "Shortest time difference = $mindiff minutes";}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
#! /usr/bin/perl
2+
# Robbie Hatley's Perl Solution to PWCC 206-2
3+
4+
=pod
5+
6+
Task 2: Array Pairings
7+
Submitted by: Mohammad S Anwar
8+
Given an array of integers having even number of elements, find the
9+
maximum sum of the minimum of each pair.
10+
11+
Example 1: Input: (1,2,3,4) Output: 4
12+
Pairings:
13+
(1,2) and (3,4). So min(1,2) + min(3,4) => 1 + 3 => 4
14+
(1,3) and (2,4). So min(1,3) + min(2,4) => 1 + 2 => 3
15+
(1,4) and (2,3). So min(1,4) + min(2,3) => 2 + 1 => 3
16+
So the maximum sum is 4.
17+
18+
Example 2: Input: (0,2,1,3) Output: 2
19+
Pairings:
20+
(0,2) and (1,3). So min(0,2) + min(1,3) => 0 + 1 => 1
21+
(0,1) and (2,3). So min(0,1) + min(2,3) => 0 + 2 => 2
22+
(0,3) and (2,1). So min(0,3) + min(2,1) => 0 + 1 => 1
23+
So the maximum sum is 2.
24+
25+
=cut
26+
27+
# IO NOTES:
28+
# NOTE: Input is by either built-in array-of-arrays, or @ARGV. If using @ARGV,
29+
# the args should be a space-separated sequence of an even number of real numbers,
30+
# which will be interpreted as being a single array.
31+
# NOTE: Output is to STDOUT and will be the maximum sum of pair minimums.
32+
33+
# PRELIMINARIES:
34+
use v5.36;
35+
use List::Util 'sum0', 'max';
36+
$"=", ";
37+
38+
# DEFAULT INPUTS:
39+
my @arrays = ([1,2,3,4], [0,2,1,3]);
40+
41+
# NON-DEFAULT INPUTS:
42+
if (@ARGV) {@arrays = ([@ARGV]);}
43+
44+
# SUBROUTINES:
45+
46+
sub MaxSumMinEasy($array=[]){
47+
return 0 if 0 == scalar @{$array};
48+
my @sorted = sort {$a<=>$b} @{$array};
49+
my $asize = scalar(@sorted);
50+
die "Error in MaxSumMin(): array size not even.\n$!\n" if 0 != $asize % 2;
51+
my @even_indices; push @even_indices, 2*$_ for 0..($asize/2-1);
52+
return sum0(@sorted[@even_indices])}
53+
54+
sub Pairings ($array=[], $pairs=[]){
55+
state $recurse = 0;
56+
die "Error in Pairings(): Over 50 levels of recursion!\n$!\n" if $recurse > 50;
57+
state @pairings;
58+
# Clear @pairings on first entry, else @pairings accumulates garbage:
59+
@pairings = () if 0 == $recurse;
60+
my $asize = scalar(@{$array});
61+
die "Error in Pairings(): array size not even.\n$!\n" if 0 != $asize % 2;
62+
if (0 == $asize){
63+
push @pairings, $pairs;}
64+
else{
65+
for ( my $i = 0 ; $i <= $asize - 2 ; ++$i ){
66+
for ( my $j = $i + 1 ; $j <= $asize - 1 ; ++$j ){
67+
my @recurse_array = @{$array};
68+
my @recurse_pairs = @{$pairs};
69+
my $p2 = splice @recurse_array, $j, 1; # $j MUST come first!!!
70+
my $p1 = splice @recurse_array, $i, 1; # Can you see why?
71+
push @recurse_pairs, [$p1, $p2];
72+
++$recurse;
73+
Pairings(\@recurse_array, \@recurse_pairs);
74+
--$recurse}}}
75+
return \@pairings}
76+
77+
sub SumMin($pairing=[]){
78+
return sum0 map {(sort {$a<=>$b} @{$_})[0]} @{$pairing}}
79+
80+
sub MaxSumMinHard($pairings=[]){
81+
return max map {SumMin $_} @{$pairings};}
82+
83+
# MAIN BODY OF SCRIPT:
84+
for (@arrays){
85+
my $array = $_;
86+
my $msme = MaxSumMinEasy($array);
87+
my $pairings = Pairings($array);
88+
my $numpai = scalar @{$pairings};
89+
my $msmh = MaxSumMinHard($pairings);
90+
say '';
91+
say "array: (@{$array})";
92+
say 'Pairings:';
93+
for (@{$pairings}){ # For each pairing
94+
for (@{$_}){ # For each pair
95+
print " [$_->[0],$_->[1]]";}
96+
print " ", SumMin($_), "\n";}
97+
say "$numpai pairings";
98+
say "max-sum-min-easy = $msme";
99+
say "max-sum-min-hard = $msmh"}

0 commit comments

Comments
 (0)