aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2023-03-04 11:28:28 +0000
committerGitHub <noreply@github.com>2023-03-04 11:28:28 +0000
commit7c56098787b5ffc369c7bd765eaa97c28dce5b91 (patch)
tree1d4989f16b0ed0733831fb31fa61589b9f5b9901
parent662a538ae4244f9da5ce77b69abfe9953716ef83 (diff)
parent75ae7a1e0872ef0ecc5b65db9112b2eea38f79bb (diff)
downloadperlweeklychallenge-club-7c56098787b5ffc369c7bd765eaa97c28dce5b91.tar.gz
perlweeklychallenge-club-7c56098787b5ffc369c7bd765eaa97c28dce5b91.tar.bz2
perlweeklychallenge-club-7c56098787b5ffc369c7bd765eaa97c28dce5b91.zip
Merge pull request #7642 from simongreen-net/master
Simon's solution to challenge 206
-rw-r--r--challenge-206/sgreen/README.md4
-rw-r--r--challenge-206/sgreen/blog.txt1
-rwxr-xr-xchallenge-206/sgreen/perl/ch-1.pl37
-rwxr-xr-xchallenge-206/sgreen/perl/ch-2.pl18
-rwxr-xr-xchallenge-206/sgreen/python/ch-1.py30
-rwxr-xr-xchallenge-206/sgreen/python/ch-2.py17
6 files changed, 105 insertions, 2 deletions
diff --git a/challenge-206/sgreen/README.md b/challenge-206/sgreen/README.md
index b2c76ea65b..36f49d2ad5 100644
--- a/challenge-206/sgreen/README.md
+++ b/challenge-206/sgreen/README.md
@@ -1,3 +1,3 @@
-# The Weekly Challenge 205
+# The Weekly Challenge 206
-Blog: [Weekly Challenge 205](https://dev.to/simongreennet/weekly-challenge-205-3f3)
+Blog: [Weekly Challenge 206](https://dev.to/simongreennet/weekly-challenge-206-2god)
diff --git a/challenge-206/sgreen/blog.txt b/challenge-206/sgreen/blog.txt
new file mode 100644
index 0000000000..4878a97c80
--- /dev/null
+++ b/challenge-206/sgreen/blog.txt
@@ -0,0 +1 @@
+https://dev.to/simongreennet/weekly-challenge-206-2god \ No newline at end of file
diff --git a/challenge-206/sgreen/perl/ch-1.pl b/challenge-206/sgreen/perl/ch-1.pl
new file mode 100755
index 0000000000..2850acf9ef
--- /dev/null
+++ b/challenge-206/sgreen/perl/ch-1.pl
@@ -0,0 +1,37 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use feature 'say';
+use experimental 'signatures';
+
+sub main (@times) {
+ # Let the default shortest time be something larger than everything
+ my $shortest = 24 * 60;
+
+ # Convert the times into minutes after midnight
+ my @minutes = map { substr( $_, 0, 2 ) * 60 + substr( $_, 3, 2 ) } @times;
+
+ # Find all pairs
+ foreach my $t1 ( 0 .. $#minutes - 1 ) {
+ foreach my $t2 ( $t1 + 1 .. $#minutes ) {
+ my $diff = abs( $minutes[$t1] - $minutes[$t2] );
+
+ # If the difference is more than 12 hours, it will be shorter
+ # if we cross midnight
+ if ( $diff > 12 * 60 ) {
+ $diff = 24 * 60 - $diff;
+ }
+
+ if ( $diff < $shortest ) {
+ # We have found a new shortest span
+ $shortest = $diff;
+ }
+ }
+ }
+
+ # Print the shortest span
+ say $shortest;
+}
+
+main(@ARGV); \ No newline at end of file
diff --git a/challenge-206/sgreen/perl/ch-2.pl b/challenge-206/sgreen/perl/ch-2.pl
new file mode 100755
index 0000000000..0dc3133b0f
--- /dev/null
+++ b/challenge-206/sgreen/perl/ch-2.pl
@@ -0,0 +1,18 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use feature 'say';
+use experimental 'signatures';
+
+use List::Util qw(pairkeys sum);
+
+sub main(@n) {
+ # Sort the list numerical
+ @n = sort { $a <=> $b} @n;
+
+ # Return the sum of the the odd positioned items (1st, 3rd, 5th, ...)
+ say sum(pairkeys(@n));
+}
+
+main(@ARGV); \ No newline at end of file
diff --git a/challenge-206/sgreen/python/ch-1.py b/challenge-206/sgreen/python/ch-1.py
new file mode 100755
index 0000000000..eb522292f9
--- /dev/null
+++ b/challenge-206/sgreen/python/ch-1.py
@@ -0,0 +1,30 @@
+#!/usr/bin/env python3
+
+import sys
+
+
+def main(times):
+ # Let the default shortest time be something larger than everything
+ shortest = 24*60
+
+ # Convert the times into minutes after midnight
+ minutes = [int(t[0:2]) * 60 + int(t[3:5]) for t in times]
+
+ # Find all pairs
+ for t1 in range(len(minutes)-1):
+ for t2 in range(t1+1, len(minutes)):
+ diff = abs(minutes[t1] - minutes[t2])
+ # If the difference is more than 12 hours, it will be shorter if we cross midnight
+ if diff > 12 * 60:
+ diff = 24 * 60 - diff
+
+ if diff < shortest:
+ # We have found a new shortest span
+ shortest = diff
+
+ # Print the shortest span
+ print(shortest)
+
+
+if __name__ == '__main__':
+ main(sys.argv[1:])
diff --git a/challenge-206/sgreen/python/ch-2.py b/challenge-206/sgreen/python/ch-2.py
new file mode 100755
index 0000000000..4a1e700194
--- /dev/null
+++ b/challenge-206/sgreen/python/ch-2.py
@@ -0,0 +1,17 @@
+#!/usr/bin/env python3
+
+import sys
+
+
+def main(n):
+ # Sort the list numerical
+ n = sorted(n)
+
+ # Return the sum of the the odd positioned items (1st, 3rd, 5th, ...)
+ print(sum(n[::2]))
+
+
+if __name__ == '__main__':
+ # Turn the strings into integers
+ n = [int(i) for i in sys.argv[1:]]
+ main(n)