From 75ae7a1e0872ef0ecc5b65db9112b2eea38f79bb Mon Sep 17 00:00:00 2001 From: Simon Green Date: Mon, 27 Feb 2023 23:53:49 +1100 Subject: Simon's solution to challenge 206 --- challenge-206/sgreen/README.md | 4 ++-- challenge-206/sgreen/blog.txt | 1 + challenge-206/sgreen/perl/ch-1.pl | 37 +++++++++++++++++++++++++++++++++++++ challenge-206/sgreen/perl/ch-2.pl | 18 ++++++++++++++++++ challenge-206/sgreen/python/ch-1.py | 30 ++++++++++++++++++++++++++++++ challenge-206/sgreen/python/ch-2.py | 17 +++++++++++++++++ 6 files changed, 105 insertions(+), 2 deletions(-) create mode 100644 challenge-206/sgreen/blog.txt create mode 100755 challenge-206/sgreen/perl/ch-1.pl create mode 100755 challenge-206/sgreen/perl/ch-2.pl create mode 100755 challenge-206/sgreen/python/ch-1.py create mode 100755 challenge-206/sgreen/python/ch-2.py 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) -- cgit