aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpme <hauptadler@gmail.com>2024-09-03 23:10:46 +0200
committerpme <hauptadler@gmail.com>2024-09-03 23:10:46 +0200
commit55f6c7231998ec853b5068b56d804ac4bd3cfcec (patch)
tree85b4e40ce12fe7f33d8338498a4a9f886d35c2ae
parentfab34b71024a7d75f302ad1b0a4e4df8c15eab46 (diff)
downloadperlweeklychallenge-club-55f6c7231998ec853b5068b56d804ac4bd3cfcec.tar.gz
perlweeklychallenge-club-55f6c7231998ec853b5068b56d804ac4bd3cfcec.tar.bz2
perlweeklychallenge-club-55f6c7231998ec853b5068b56d804ac4bd3cfcec.zip
challenge-206
-rwxr-xr-xchallenge-206/peter-meszaros/perl/ch-1.pl67
-rwxr-xr-xchallenge-206/peter-meszaros/perl/ch-2.pl71
2 files changed, 138 insertions, 0 deletions
diff --git a/challenge-206/peter-meszaros/perl/ch-1.pl b/challenge-206/peter-meszaros/perl/ch-1.pl
new file mode 100755
index 0000000000..6aaf7a93c3
--- /dev/null
+++ b/challenge-206/peter-meszaros/perl/ch-1.pl
@@ -0,0 +1,67 @@
+#!/usr/bin/env perl
+#
+=head1 Task 1: 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.
+
+=head2 Example 1
+
+ Input: @time = ("00:00", "23:55", "20:00")
+ Output: 5
+
+ Since the difference between "00:00" and "23:55" is the shortest (5 minutes).
+
+=head2 Example 2
+
+ Input: @array = ("01:01", "00:50", "00:57")
+ Output: 4
+
+=head2 Example 3
+
+ Input: @array = ("10:10", "09:30", "09:00", "09:55")
+ Output: 15
+
+=cut
+
+use strict;
+use warnings;
+use Test2::V0 -no_srand => 1;
+use Data::Dumper;
+
+my $cases = [
+ [["00:00", "23:55", "20:00"], 5, 'Example 1'],
+ [["01:01", "00:50", "00:57"], 4, 'Example 2'],
+ [["10:10", "09:30", "09:00", "09:55"], 15, 'Example 3'],
+];
+
+sub shortest_time
+{
+ my $l = shift;
+
+ my @l;
+ for my $t (@$l) {
+ my ($h, $m) = split ':', $t;
+ push @l, $h * 60 + $m || 1440;
+ }
+ my $min = 24 * 60;
+ for my $i (0 .. $#l-1) {
+ for my $j ($i+1 .. $#l) {
+ my $diff = abs($l[$i] - $l[$j]);
+ $min = $diff if $diff < $min;
+ }
+ }
+ return $min;
+}
+
+for (@$cases) {
+ is(shortest_time($_->[0]), $_->[1], $_->[2]);
+}
+done_testing();
+
+exit 0;
diff --git a/challenge-206/peter-meszaros/perl/ch-2.pl b/challenge-206/peter-meszaros/perl/ch-2.pl
new file mode 100755
index 0000000000..c745f85c4e
--- /dev/null
+++ b/challenge-206/peter-meszaros/perl/ch-2.pl
@@ -0,0 +1,71 @@
+#!/usr/bin/env perl
+#
+=head1 Task 2: 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.
+
+=head2 Example 1
+
+ Input: @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 maxium sum is 4.
+
+=head2 Example 2
+
+ Input: @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.
+
+=cut
+
+use strict;
+use warnings;
+use Test2::V0 -no_srand => 1;
+use Data::Dumper;
+use Algorithm::Combinatorics qw/partitions/;
+use List::Util qw/min/;
+
+my $cases = [
+ [[1, 2, 3, 4], 4, 'Example 1'],
+ [[0, 2, 1, 3], 2, 'Example 2'],
+];
+
+sub array_pairings
+{
+ my $l = shift;
+
+ my $max = 0;
+ my $iter = partitions($l, @$l/2);
+ PARTITION: while (my $c = $iter->next) {
+ my $v = 0;
+ for my $e (@$c) {
+ next PARTITION if @$e != 2;
+ $v += min(@$e);
+ }
+ $max = $v if $v > $max;
+ }
+ return $max;
+}
+
+for (@$cases) {
+ is(array_pairings($_->[0]), $_->[1], $_->[2]);
+}
+done_testing();
+
+exit 0;