aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2023-03-04 11:43:32 +0000
committerGitHub <noreply@github.com>2023-03-04 11:43:32 +0000
commitc5aff4d02c55f6579e1ec573ff2cb08d924d29b8 (patch)
tree47a9532b32795a10c62cb63bec9f662bace8f3a2
parent320fe6658f101a0e2b650a5c00b4fe0cc2eeb75c (diff)
parent83e4763feaf1ad26134fa66151fb3098082e9f19 (diff)
downloadperlweeklychallenge-club-c5aff4d02c55f6579e1ec573ff2cb08d924d29b8.tar.gz
perlweeklychallenge-club-c5aff4d02c55f6579e1ec573ff2cb08d924d29b8.tar.bz2
perlweeklychallenge-club-c5aff4d02c55f6579e1ec573ff2cb08d924d29b8.zip
Merge pull request #7650 from spadacciniweb/PWC-206
PWC 206 - Perl, Python, Go
-rw-r--r--challenge-206/spadacciniweb/go/ch-1.go86
-rw-r--r--challenge-206/spadacciniweb/perl/ch-1.pl74
-rw-r--r--challenge-206/spadacciniweb/perl/ch-2.pl64
-rw-r--r--challenge-206/spadacciniweb/python/ch-1.py58
-rw-r--r--challenge-206/spadacciniweb/python/ch-2.py73
5 files changed, 355 insertions, 0 deletions
diff --git a/challenge-206/spadacciniweb/go/ch-1.go b/challenge-206/spadacciniweb/go/ch-1.go
new file mode 100644
index 0000000000..3a97ddde59
--- /dev/null
+++ b/challenge-206/spadacciniweb/go/ch-1.go
@@ -0,0 +1,86 @@
+/*
+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.
+
+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).
+
+Example 2
+Input: @array = ("01:01", "00:50", "00:57")
+Output: 4
+
+Example 3
+Input: @array = ("10:10", "09:30", "09:00", "09:55")
+Output: 15
+*/
+
+package main
+
+import (
+ "fmt"
+ "log"
+ "os"
+ "strconv"
+ "gonum.org/v1/gonum/stat/combin"
+)
+
+func abs(x int) int {
+ if x < 0 {
+ return -x
+ }
+ return x
+}
+
+func getDistanceMinutes(m1 int, m2 int) int {
+ var dist = abs(m1 - m2)
+ if dist >= 720 { // 12 hh * 60 = minutes
+ return 1440 - dist // 24 hh * 60 = minutes
+ }
+ return dist
+}
+
+func getMinutes(hhmm string) int {
+ hh, mm := hhmm[0:2], hhmm[3:5]
+ hhInt, err := strconv.Atoi(hh)
+ if (err != nil) {
+ log.Fatal(err)
+ }
+ mmInt, err := strconv.Atoi(mm)
+ if (err != nil) {
+ log.Fatal(err)
+ }
+ return (hhInt % 24) * 60 + mmInt
+}
+
+func main() {
+ arrStr := os.Args[1:]
+ if (len(arrStr) < 2) {
+ log.Fatal("input error")
+ }
+
+ minutes := make([]int, len(arrStr))
+ for i := 0; i <= len(arrStr)-1; i++ {
+ minutes[i] = getMinutes(arrStr[i])
+ }
+
+ var minPointer *int
+ var pair [2]string
+ cs := combin.Combinations(len(minutes), 2)
+ for _, c := range cs {
+ var dist = getDistanceMinutes(minutes[c[0]], minutes[c[1]])
+ if minPointer == nil || *minPointer > dist {
+ minPointer = &dist
+ pair[0] = arrStr[c[0]]
+ pair[1] = arrStr[c[1]]
+ }
+ }
+
+ fmt.Printf("Output: %d", *minPointer)
+ fmt.Printf("\nSince the difference between \"%s\" and \"%s\" is the shortest (%d minutes).\n", pair[0], pair[1], *minPointer)
+}
diff --git a/challenge-206/spadacciniweb/perl/ch-1.pl b/challenge-206/spadacciniweb/perl/ch-1.pl
new file mode 100644
index 0000000000..481de5d4a1
--- /dev/null
+++ b/challenge-206/spadacciniweb/perl/ch-1.pl
@@ -0,0 +1,74 @@
+#!/usr/bin/env perl
+
+# 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.
+#
+# 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).
+#
+# Example 2
+# Input: @array = ("01:01", "00:50", "00:57")
+# Output: 4
+#
+# Example 3
+# Input: @array = ("10:10", "09:30", "09:00", "09:55")
+# Output: 15
+
+use strict;
+use warnings;
+use Math::Combinatorics;
+
+my @input = @ARGV;
+die "Input error\n"
+ if scalar @input < 2
+ or
+ (scalar map { $_ =~ /^\d{2}:\d{2}$/ ? () : 1 }
+ @input) != 0
+ or
+ (scalar map { $_ le '24:00' ? () : 1 }
+ @input) != 0;
+
+my @minutes = map { get_minutes($_) } @input;
+
+my $combinat = Math::Combinatorics->new(count => 2,
+ data => [0..$#minutes],
+ );
+my %min;
+while (my @pair = $combinat->next_combination) {
+ my $diff_minutes = diff_minutes($minutes[$pair[0]], $minutes[$pair[1]]);
+ if (!defined($min{val})
+ or
+ $min{val} > $diff_minutes
+ ) {
+ $min{val} = $diff_minutes;
+ $min{pair} = \@pair;
+ }
+}
+
+print $min{val};
+printf "\nSince the difference between \"%s\" and \"%s\" is the shortest (%d minutes).\n", $input[ $min{pair}[0] ], $input[ $min{pair}[1] ], $min{val};
+
+exit 0;
+
+sub get_minutes {
+ my $time = shift;
+
+ return 60 * (substr $time, 0, 2) +
+ (substr $time, 3, 2);
+}
+
+sub diff_minutes {
+ my ($m1, $m2) = @_;
+
+ my $diff_minutes = abs($m1-$m2);
+
+ return ($diff_minutes >= 720) # 12h * 60 = minutes
+ ? (1440 - $diff_minutes) # 24h * 60 = minutes
+ : $diff_minutes;
+}
diff --git a/challenge-206/spadacciniweb/perl/ch-2.pl b/challenge-206/spadacciniweb/perl/ch-2.pl
new file mode 100644
index 0000000000..94c3d005c1
--- /dev/null
+++ b/challenge-206/spadacciniweb/perl/ch-2.pl
@@ -0,0 +1,64 @@
+#!/usr/bin/env perl
+
+# 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.
+#
+# 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.
+#
+# 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.
+
+use strict;
+use warnings;
+use Algorithm::Combinatorics qw(partitions);
+use List::Util qw(min max sum);
+
+my @input = @ARGV;
+die "Input error\n"
+ if scalar @input < 1
+ or
+ (scalar @input) % 2 != 0
+ or
+ (scalar map { $_ =~ /\D/ ? 1 : () }
+ @input) != 0;
+
+my @pairing;
+my $iter = partitions([map { int($_) } @input], (scalar @input/2));
+while (my $p = $iter->next) {
+ my $bool = 1;
+ foreach my $set (@$p) {
+ if (scalar @$set != 2) {
+ $bool = 0;
+ last;
+ }
+ }
+ push @pairing, $p
+ if $bool;
+}
+
+my @min;
+foreach my $pairs (@pairing) {
+ push @min, sum map { min @{$_} }
+ values @$pairs;
+}
+printf "%s\n", max @min;
diff --git a/challenge-206/spadacciniweb/python/ch-1.py b/challenge-206/spadacciniweb/python/ch-1.py
new file mode 100644
index 0000000000..14d7e34598
--- /dev/null
+++ b/challenge-206/spadacciniweb/python/ch-1.py
@@ -0,0 +1,58 @@
+# 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.
+#
+# 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).
+#
+# Example 2
+# Input: @array = ("01:01", "00:50", "00:57")
+# Output: 4
+#
+# Example 3
+# Input: @array = ("10:10", "09:30", "09:00", "09:55")
+# Output: 15
+
+import re
+import sys
+from itertools import combinations
+
+def get_minutes(t):
+ return 60 * int(t[0:2]) + int(t[3:5])
+
+def get_diff_minutes(m1, m2):
+ res = abs(m1-m2)
+
+ if (res >= 720): # 12h * 60 = minutes
+ return 1440 - res # 24h * 60 = minutes
+ return res
+
+if __name__ == "__main__":
+ input = sys.argv[1:]
+ if (len(input) < 2
+ or
+ len(list(filter(lambda x: re.search(r'\d\d:\d\d', x), input))) != len(input)
+ or
+ len(list(filter(lambda x: x > '24:00', input))) != 0 ):
+ sys.exit("Input error\n")
+
+ minutes = [get_minutes(x) for x in input]
+ comb = combinations(range(len(input)), 2)
+ h_minutes = {
+ 'val': None,
+ 'pair': None
+ }
+
+ for pair in list(comb):
+ diff_minutes = get_diff_minutes(minutes[pair[0]], minutes[pair[1]])
+ if h_minutes['val'] == None or h_minutes['val'] > diff_minutes:
+ h_minutes['val'] = diff_minutes
+ h_minutes['pair'] = pair
+
+ print(h_minutes['val'])
+ print("Since the difference between \"{:s}\" and \"{:s}\" is the shortest ({:d} minutes).".format(input[ h_minutes['pair'][0] ], input[ h_minutes['pair'][1] ], h_minutes['val'] ))
diff --git a/challenge-206/spadacciniweb/python/ch-2.py b/challenge-206/spadacciniweb/python/ch-2.py
new file mode 100644
index 0000000000..832d866b18
--- /dev/null
+++ b/challenge-206/spadacciniweb/python/ch-2.py
@@ -0,0 +1,73 @@
+# 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.
+#
+# 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.
+#
+# 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.
+import re
+import sys
+from functools import reduce
+
+# https://stackoverflow.com/questions/19368375/set-partitions-in-python
+def partition(collection):
+ if len(collection) == 1:
+ yield [ collection ]
+ return
+
+ first = collection[0]
+ for smaller in partition(collection[1:]):
+ # insert `first` in each of the subpartition's subsets
+ for n, subset in enumerate(smaller):
+ yield smaller[:n] + [[ first ] + subset] + smaller[n+1:]
+ # put `first` in its own subset
+ yield [ [ first ] ] + smaller
+
+
+
+if __name__ == "__main__":
+ input = sys.argv[1:]
+ if (len(input) < 2
+ or
+ len(input) % 2 != 0
+ or
+ len(list(filter(lambda x: re.search(r'\D', x), input))) != 0):
+ sys.exit("Input error\n")
+
+ input = list(map(int, input))
+ pairing = []
+ for n, p in enumerate(partition(input), 1):
+ sets = sorted(p)
+ b_pair = True
+ for s in sets:
+ if len(s) != 2:
+ b_pair = False
+ break
+ if b_pair == True:
+ pairing.append(sets)
+
+ l_min = []
+ for pairs in list(pairing):
+ l_min.append([min(s) for s in pairs])
+
+ print(max([sum(s) for s in l_min]))