From 32de3453dfef9953ef8452d7ed80062ca328f460 Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Mon, 27 Feb 2023 09:15:41 +0000 Subject: Initial 206 (Raku) --- challenge-206/mark-anderson/raku/ch-1.raku | 18 ++++++++++++++++++ challenge-206/mark-anderson/raku/ch-2.raku | 17 +++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 challenge-206/mark-anderson/raku/ch-1.raku create mode 100644 challenge-206/mark-anderson/raku/ch-2.raku diff --git a/challenge-206/mark-anderson/raku/ch-1.raku b/challenge-206/mark-anderson/raku/ch-1.raku new file mode 100644 index 0000000000..48746c0830 --- /dev/null +++ b/challenge-206/mark-anderson/raku/ch-1.raku @@ -0,0 +1,18 @@ +#!/usr/bin/env raku +use Test; + +is shortest-time(< 00:00 23:55 20:00 >), 5; +is shortest-time(< 01:01 00:50 00:57 >), 4; +is shortest-time(< 10:10 09:30 09:00 09:55 >), 15; + +sub shortest-time(@a) +{ + min map + { + my @t = .map({ .split(':').List }) + .map({ .[0] * 60 + .[1] }); + + ((.min, .min + 1440) >>->> .max)>>.abs.min given @t + }, + @a.combinations(2) +} diff --git a/challenge-206/mark-anderson/raku/ch-2.raku b/challenge-206/mark-anderson/raku/ch-2.raku new file mode 100644 index 0000000000..7c88b4c177 --- /dev/null +++ b/challenge-206/mark-anderson/raku/ch-2.raku @@ -0,0 +1,17 @@ +#!/usr/bin/env raku +use Test; + +is array-pairings(< 1 2 3 4 >), 4; +is array-pairings(< 0 2 1 3 >), 2; +is array-pairings(< 1 2 3 4 5 6 >), 5; + +sub array-pairings(@a where * %% 2) +{ + my @c = @a.combinations(@a.elems div 2); + + max map + { + sum .map({ .min }) + }, + (@c[^(@c.elems div 2)] Z @c[@c.elems div 2..@c.end].reverse) +} -- cgit From fb41788f04ef4c0fcd837faf8e43e3bf7bcca26c Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Mon, 27 Feb 2023 11:13:17 +0000 Subject: Initial 206 (Raku) --- challenge-206/mark-anderson/raku/ch-2.raku | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/challenge-206/mark-anderson/raku/ch-2.raku b/challenge-206/mark-anderson/raku/ch-2.raku index 7c88b4c177..3358092efc 100644 --- a/challenge-206/mark-anderson/raku/ch-2.raku +++ b/challenge-206/mark-anderson/raku/ch-2.raku @@ -13,5 +13,5 @@ sub array-pairings(@a where * %% 2) { sum .map({ .min }) }, - (@c[^(@c.elems div 2)] Z @c[@c.elems div 2..@c.end].reverse) + (.[^(.elems div 2)] Z .[.elems div 2 .. .end].reverse) given @c } -- cgit 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 From a36faafe79c1f7bedf4e7787df128c10c81f5ea3 Mon Sep 17 00:00:00 2001 From: James Smith Date: Mon, 27 Feb 2023 12:54:32 +0000 Subject: Update README.md --- challenge-206/james-smith/README.md | 96 ++++++++++++++++--------------------- 1 file changed, 42 insertions(+), 54 deletions(-) diff --git a/challenge-206/james-smith/README.md b/challenge-206/james-smith/README.md index 9f095be3e9..4c0ad820a2 100644 --- a/challenge-206/james-smith/README.md +++ b/challenge-206/james-smith/README.md @@ -1,7 +1,7 @@ -[< Previous 204](https://github.com/drbaggy/perlweeklychallenge-club/tree/master/challenge-204/james-smith) | -[Next 206 >](https://github.com/drbaggy/perlweeklychallenge-club/tree/master/challenge-206/james-smith) +[< Previous 205](https://github.com/drbaggy/perlweeklychallenge-club/tree/master/challenge-205/james-smith) | +[Next 207 >](https://github.com/drbaggy/perlweeklychallenge-club/tree/master/challenge-207/james-smith) -# The Weekly Challenge 205 +# The Weekly Challenge 206 You can find more information about this weeks, and previous weeks challenges at: @@ -13,81 +13,69 @@ submit solutions in whichever language you feel comfortable with. You can find the solutions here on github at: -https://github.com/drbaggy/perlweeklychallenge-club/tree/master/challenge-205/james-smith +https://github.com/drbaggy/perlweeklychallenge-club/tree/master/challenge-2065/james-smith -# Task 1: Third Highest +# Task 1: Shortest Time -***You are given an array of integers. Write a script to find out the Third Highest if found otherwise return the maximum.*** - -***Note the examples suggest we are looking for third highest unique value - so we will solve both solutions*** +***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.*** ## Solution -A quick (code wise) solution would be to sort the list `@_` and take the 3rd value or first if the list has length less than 3. But for large lists this would be inefficient. There is a debate here about what the cut off value is and so a simple sort will be quicker for small arrays. - -This was pass 1 - which sorts irrespective of uniqueness. But fails test 3. - -We start by sorting the first three values, then we proceed to check the next values against the three current values, and insert the new value into the correct place in the list (or do nothing); +We will do a pairwise comparison of each pair. The shortest time for any pair is either going from the absolute differences in times directly - OR going through midnight. These are `abs( t1 - t2 )` or `abs( t1 + t2 - 1440 )`. The code becomes: ```perl -sub third { - my ($i,$j,$k) = sort { $b <=> $a } splice @_,0,3; - return $i unless defined $k; - $_ > $i ? (($i,$j,$k)=($_,$i,$j)) - : $_ > $j ? (($j,$k)=($_,$j)) - : ( $_ > $k ) && ($k=$_) for @_; - $k; +sub shortest_time { + my $min = 1_440, @_ = map { @Q = split /:/; $Q[0]*60 + $Q[1] } @_; + while( defined (my $t = shift) ) { + abs( $t-$_ ) < $min && ( $min = abs $t-$_ ), + abs( $t+$_-1_440 ) < $min && ( $min = abs $t+$_-1_440 ) for @_; + } + $min } ``` -Now if we are looking for uniqueness - then the code becomes slighly more complex. If we have the value matching another value we do nothing. Here we can't splice off the first 3 values, instead we have to check for this equality each time. So the code becomes. -Note we could have re-ordered the checks to avoid the two *skips* when checking for equality - but then the code becomes less readable. +Now how efficient is this - though - is there a better way to use built-in perk functions? + +If we sort the times in order, we only have to compare the `n` gaps, from the last to the first through midnight and each of the subsequent neighbours. + +This gives us two alternative code blocks: ```perl -sub third_unique { - my ($i,$j,$k) = shift; - $_ > $i ? (($i,$j,$k)=($_,$i,$j)) - : $_ == $i ? () - : !defined $j || $_ > $j ? (($j,$k)=($_,$j)) - : defined $j && $_ == $j ? () - : ( !defined $k || $_ > $k ) && ($k=$_) for @_; - $k//$i; +sub shortest_time { + @_ = map { my @Q = split /:/; $Q[0]*60 + $Q[1] } sort @_; + my $min = 1440 + (my $t = shift) - $_[-1]; + ($_-$t<$min) && ($min=$_-$t), $t=$_ for @_; + $min } ``` -### A third solution - -Having the extra `defined` queries in the code had seems a little inefficient. We can get round these by using the special variable `'-inf'`. - -Perl does not have a true concept of "infinity". But does have the string `'-inf'` - if you do `$i > '-inf'` it will always be true for all `$i`. Unlike `$i > undef` which is treated as `$i > 0`. +or: ```perl -sub third_unique_inf { - my ($i,$j,$k) = (shift,'-inf','-inf'); - $_ > $i ? ( ($i,$j,$k) = ($_,$i,$j) ) - : $_ == $i ? ( ) - : $_ > $j ? ( ($j,$k) = ($_,$j) ) - : $_ == $j ? ( ) - : $_ > $k && ( $k = $_ ) for @_; - $k eq '-inf' ? $i : $k +sub shortest_time { + @_ = sort { $a<=>$b } map { my @Q = split /:/; $Q[0]*60 + $Q[1] } @_; + my $min = 1440 + (my $t = shift) - $_[-1]; + ($_-$t<$min) && ($min=$_-$t), $t=$_ for @_; + $min } ``` -# Task 2: Maximum XOR -***You are given an array of integers. Write a script to find the highest value obtained by XORing any two distinct members of the array.*** +Which of these is fastest? The `sort` method is much more efficient than the pairwise approach (It's `O(n.log n)` where the pairwise solution is `O(n^2)`. Of the two the second numeric `sort` after the `map` if slightly faster than the `map` after the string `sort`. + +# Task 2: Array Pairings + +***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.*** ## Solution -There is nothing other than brute force to find the solution as we have to check every combination. Just how we do this - using indexes or `shift` and `for{each}`. We go for the latter and the code becomes simple. +There is a trick here - the optimal solution is achieved by sorting the array into order and then chunking into to pairs... Then take the minimum of each... ```perl -sub max_xor { - my $m = 0; - while( @_ ){ - my $x=shift; - ( $x^$_ ) > $m && ( $m = $x^$_ ) for @_ - } - $m +sub max_sum_pair_min { + my $t = 0, @_ = sort {$a<=>$b} @_; + $t += shift, shift while @_; + $t } ``` -To avoid a set of brackets we use the simple logic of `$a && $b` is the same as `$b if $a`. + +When we `shift`, `shift` the first value is added to the total, the second value is discarded. -- cgit From cd793c06e9edd25ce03b56b8a0a00e1f22bb009e Mon Sep 17 00:00:00 2001 From: James Smith Date: Mon, 27 Feb 2023 12:54:55 +0000 Subject: Create blog.txt --- challenge-206/james-smith/blog.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 challenge-206/james-smith/blog.txt diff --git a/challenge-206/james-smith/blog.txt b/challenge-206/james-smith/blog.txt new file mode 100644 index 0000000000..4167b6ec36 --- /dev/null +++ b/challenge-206/james-smith/blog.txt @@ -0,0 +1 @@ +https://github.com/manwar/perlweeklychallenge-club/blob/master/challenge-206/james-smith/blog.txt -- cgit From 59ec5a557f0b6a2a8b369d165cd0b30d9fa017d0 Mon Sep 17 00:00:00 2001 From: James Smith Date: Mon, 27 Feb 2023 13:00:51 +0000 Subject: Create ch-1.pl --- challenge-206/james-smith/perl/ch-1.pl | 112 +++++++++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 challenge-206/james-smith/perl/ch-1.pl diff --git a/challenge-206/james-smith/perl/ch-1.pl b/challenge-206/james-smith/perl/ch-1.pl new file mode 100644 index 0000000000..bb1a6bd6ea --- /dev/null +++ b/challenge-206/james-smith/perl/ch-1.pl @@ -0,0 +1,112 @@ +#!/usr/local/bin/perl + +use strict; +use warnings; +use feature qw(say); +use Test::More; +use Benchmark qw(cmpthese timethis); + +my @TESTS = ( + [ [qw(00:00 23:55 20:00 )], 5 ], + [ [qw(01:01 00:50 00:57 )], 4 ], + [ [qw(10:10 09:30 09:00 09:55)], 15 ], + [ [qw(07:52 03:22 02:59 21:40 22:58 06:22 19:05 21:23 11:50 01:02 + 00:52 20:40 13:39 11:00 03:29 11:57 18:46 13:54 18:57 12:17 + 13:12 14:56 14:58 08:53 14:15 10:46 14:33 09:50 07:41 15:58 + 21:14 19:35 10:42 09:11 17:07 17:34 03:52 19:30 22:06 16:27 + 00:39 03:12 04:28 08:51 17:23 10:17 19:46 15:57 09:01 16:07 + 21:16 09:59 03:18 07:57 10:28 12:55 09:22 14:51 23:11 17:06 + 18:29 06:55 19:01 21:38 17:45 15:07 21:11 06:27 18:10 08:47 + 11:20 19:36 02:12 04:39 08:36 05:37 14:35 04:05 20:15 03:34 + 13:23 11:08 19:00 15:56 08:43 12:34 21:09 05:45 18:59 22:30 + 07:11 19:18 06:19 13:22 04:22 15:54 17:41 23:55 19:27 07:17 + )], 1 ], + [ [qw(14:54 21:04 16:52 20:19 03:12 15:15 00:19 00:22 11:37 09:48 + 11:44 08:42 16:14 10:49 11:43 00:35 13:09 04:03 05:42 15:52 + 02:18 06:32 13:00 00:31 20:21 09:23 07:37 09:59 13:42 14:46 + 19:36 04:19 03:17 15:35 09:52 06:58 10:44 18:49 09:40 02:02 + 01:18 02:16 07:19 19:41 14:33 13:41 17:19 20:08 07:17 18:30 + 17:18 21:49 14:35 15:53 11:34 12:48 07:55 06:40 06:04 14:10 + 02:57 08:35 17:41 11:10 22:44 07:40 02:22 17:10 09:18 16:26 + 23:40 02:35 03:37 18:52 07:35 22:24 14:22 02:01 05:19 02:31 + 12:26 06:44 00:18 01:58 05:00 01:12 06:16 11:23 04:05 18:34 + 00:53 10:20 03:05 14:20 02:48 20:03 18:11 11:35 23:23 19:04 + 12:14 20:09 20:06 20:30 19:35 06:22 06:10 14:18 12:01 04:26 + 21:47 16:10 02:21 12:08 17:47 08:55 13:48 04:34 19:10 15:07 + 13:53 15:58 15:09 05:43 10:21 13:08 00:10 04:14 11:02 03:04 + 04:32 21:36 06:50 17:33 14:52 19:06 10:07 00:04 04:38 02:10 + 09:24 18:42 16:40 04:52 16:05 03:40 09:17 23:49 09:29 19:07 + 14:24 10:01 12:31 04:27 05:59 13:05 05:33 19:15 15:24 01:45 + 12:55 21:41 08:20 06:21 12:13 15:36 02:50 18:01 15:18 17:09 + 19:22 23:26 10:34 06:17 17:05 23:07 00:05 19:55 01:10 02:00 + 00:47 02:30 00:24 20:24 22:11 04:04 15:48 15:03 00:09 12:51 + 16:18 08:24 15:30 06:12 01:31 08:18 19:03 07:39 20:05 14:36 + 19:58 17:29 09:28 20:51 03:24 02:46 00:11 08:31 04:31 02:32 + 02:44 18:33 16:47 19:59 17:06 08:22 06:34 23:10 00:17 06:41 + 01:26 03:06 23:50 06:37 01:54 23:51 12:52 13:49 13:01 16:41 + 20:39 07:21 22:52 11:39 11:17 08:26 22:13 02:34 05:11 15:14 + 18:58 12:35 20:46 06:00 05:04 13:22 04:41 05:54 18:36 01:19 + 01:21 22:02 14:59 04:30 06:49 05:16 03:32 02:58 08:52 16:49 + 11:07 17:00 18:39 14:21 12:43 00:51 10:35 06:06 22:19 04:02 + 01:44 23:35 11:20 15:40 07:29 05:28 17:49 01:03 04:21 02:43 + 02:36 13:50 07:18 20:16 23:38 09:51 06:13 21:55 02:45 14:23 + 17:50 16:01 03:56 21:27 21:17 15:19 22:39 04:24 04:47 05:27 + 07:44 15:28 15:41 23:00 13:20 20:41 12:05 21:00 21:56 05:39 + 20:40 21:38 04:51 09:22 17:01 16:16 07:24 16:02 10:26 23:17 + 04:35 16:21 01:49 12:04 02:39 12:18 06:25 23:47 12:12 15:43 + 13:16 02:08 05:13 15:01 11:27 23:15 13:56 18:15 20:45 18:53 + 07:16 12:53 14:08 13:43 22:42 22:55 01:53 14:37 01:34 08:59 + 09:09 11:41 10:32 09:06 03:27 10:59 13:28 14:40 09:26 05:35 + 00:01 08:45 22:59 18:55 02:11 07:59 16:44 17:57 16:15 18:10 + 22:38 16:56 02:25 14:03 21:25 12:49 07:56 03:02 10:05 08:36 + 02:54 14:45 04:54 14:50 23:02 23:05 23:27 22:12 20:47 07:22 + 08:03 14:48 19:56 12:17 15:49 22:25 16:12 04:23 18:18 01:39 + 21:39 22:15 11:14 00:28 21:34 20:17 22:20 10:12 19:29 05:21 + 07:57 10:17 20:48 17:17 23:12 09:49 23:42 03:46 01:59 19:19 + 18:20 17:36 16:23 03:34 11:31 06:20 16:48 11:12 05:49 14:58 + 11:48 22:57 01:11 06:19 07:51 09:41 18:17 23:21 21:29 06:48 + 19:11 07:32 17:02 18:50 19:20 04:15 14:53 15:12 06:54 19:54 + 08:08 21:26 15:29 22:40 21:13 04:56 07:14 06:26 05:03 09:21 + 11:26 09:47 08:25 23:34 13:18 06:02 03:29 02:56 09:01 07:50 + 15:50 06:27 20:36 05:02 19:09 13:38 08:32 10:14 00:45 03:26 + 18:25 07:13 09:08 05:47 04:08 16:55 03:03 09:05 19:37 02:26 + 19:39 23:55 23:08 21:48 01:02 05:55 21:57 23:29 01:20 03:48 + 13:34 + )], 1 ], +); + +is( shortest_time( @{$_->[0]} ), $_->[1] ) for @TESTS; +is( shortest_time_sort_str( @{$_->[0]} ), $_->[1] ) for @TESTS; +is( shortest_time_sort_num( @{$_->[0]} ), $_->[1] ) for @TESTS; + +done_testing(); + +cmpthese( -10, { + 'st' => sub { shortest_time( @{$_->[0]} ) for @TESTS }, + 'st_str' => sub { shortest_time_sort_str( @{$_->[0]} ) for @TESTS }, + 'st_num' => sub { shortest_time_sort_num( @{$_->[0]} ) for @TESTS }, +} ); + +sub shortest_time { + my $min = 1_440, @_ = map { @Q = split /:/; $Q[0]*60 + $Q[1] } @_; + while( defined (my $t = shift) ) { + abs( $t-$_ ) < $min && ( $min = abs $t-$_ ), + abs( $t+$_-1_440 ) < $min && ( $min = abs $t+$_-1_440 ) for @_; + } + $min +} + +sub shortest_time_sort_str { + @_ = map { my @Q = split /:/; $Q[0]*60 + $Q[1] } sort @_; + my $min = 1440 + (my $t = shift) - $_[-1]; + ($_-$t<$min) && ($min=$_-$t), $t=$_ for @_; + $min +} +or: + +sub shortest_time_sort_num { + @_ = sort { $a<=>$b } map { my @Q = split /:/; $Q[0]*60 + $Q[1] } @_; + my $min = 1440 + (my $t = shift) - $_[-1]; + ($_-$t<$min) && ($min=$_-$t), $t=$_ for @_; + $min +} -- cgit From 978a6e83d295db79dfbbbb410bfb7024ee97c962 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 27 Feb 2023 14:01:21 +0100 Subject: Task 1 done --- challenge-206/luca-ferrari/raku/ch-1.p6 | 38 +++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 challenge-206/luca-ferrari/raku/ch-1.p6 diff --git a/challenge-206/luca-ferrari/raku/ch-1.p6 b/challenge-206/luca-ferrari/raku/ch-1.p6 new file mode 100644 index 0000000000..e9e31dddec --- /dev/null +++ b/challenge-206/luca-ferrari/raku/ch-1.p6 @@ -0,0 +1,38 @@ +#!raku + +# +# Perl Weekly Challenge 206 +# Task 1 +# +# See +# + +sub diff ( $start, $end ) { + my ( $start-hours, $start-mins ) = $start.chomp.split( ':' ); + my ( $end-hours, $end-mins ) = $end.chomp.split( ':' ); + + if ( $start-hours == 0 ) { + $start-hours = 23; + $start-mins += 60; + } + + if ( $end-hours == 0 ) { + $end-hours = 23; + $end-mins += 60; + } + + my $diff-hours = abs( $end-hours - $start-hours ); + my $diff-mins = abs( $end-mins - $start-mins ) % 60; + + return $diff-hours * 60 + $diff-mins; + +} + +sub MAIN( :$verbose = True, *@times where { @times.grep( * ~~ / ^ \d ** 2 ':' \d ** 2 $ / ).elems == @times.elems } ) { + + my %diffs; + %diffs{ diff( $_[ 1 ], $_[ 0 ] ) } = [ $_[0], $_[1] ] for @times.sort.combinations( 2 ); + + %diffs.keys.map( *.Int ).min.say; + %diffs{ %diffs.keys.map( *.Int ).min }.join( ' - ' ).say if ( $verbose ); +} -- cgit From cff557456764cd477196d8c2abcdfedf4aed55a3 Mon Sep 17 00:00:00 2001 From: James Smith Date: Mon, 27 Feb 2023 13:03:34 +0000 Subject: Create ch-2.pl --- challenge-206/james-smith/perl/ch-2.pl | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 challenge-206/james-smith/perl/ch-2.pl diff --git a/challenge-206/james-smith/perl/ch-2.pl b/challenge-206/james-smith/perl/ch-2.pl new file mode 100644 index 0000000000..cd44540577 --- /dev/null +++ b/challenge-206/james-smith/perl/ch-2.pl @@ -0,0 +1,19 @@ +#!/usr/local/bin/perl + +use strict; +use warnings; +use feature qw(say); +use Test::More; + +my @TESTS = ( + [ [1,2,3,4], 4 ], + [ [0,2,1,3], 2 ], +); + +is( max_sum_pair_min( @{$_->[0]} ), $_->[1] ) for @TESTS; + +sub max_sum_pair_min { + my $t = 0, @_ = sort {$a<=>$b} @_; + $t += shift, shift while @_; + $t +} -- cgit From 1a508068a3beeb171d974d7d68c9f86e5a91ec81 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 27 Feb 2023 14:09:00 +0100 Subject: Task 2 done --- challenge-206/luca-ferrari/raku/ch-2.p6 | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 challenge-206/luca-ferrari/raku/ch-2.p6 diff --git a/challenge-206/luca-ferrari/raku/ch-2.p6 b/challenge-206/luca-ferrari/raku/ch-2.p6 new file mode 100644 index 0000000000..2c4376e5b1 --- /dev/null +++ b/challenge-206/luca-ferrari/raku/ch-2.p6 @@ -0,0 +1,20 @@ +#!raku + +# +# Perl Weekly Challenge 206 +# Task 2 +# +# See +# + +sub MAIN( *@list where { @list.elems %% 2 && @list.grep( * ~~ Int ).elems == @list.elems } ) { + my @sums; + + for @list.permutations { + for $_.rotor( 2 ) -> $a, $b { + @sums.push: sum( min( $a ) + min( $b ) ); + } + } + + @sums.min.say; +} -- cgit From 1219992c1e63e6edea84b3b0c90cbad646545fb3 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 27 Feb 2023 14:27:01 +0100 Subject: Task 1 plperl done --- challenge-206/luca-ferrari/postgresql/ch-1.plperl | 52 +++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 challenge-206/luca-ferrari/postgresql/ch-1.plperl diff --git a/challenge-206/luca-ferrari/postgresql/ch-1.plperl b/challenge-206/luca-ferrari/postgresql/ch-1.plperl new file mode 100644 index 0000000000..aa69f27629 --- /dev/null +++ b/challenge-206/luca-ferrari/postgresql/ch-1.plperl @@ -0,0 +1,52 @@ +-- +-- Perl Weekly Challenge 206 +-- Task 1 +-- See +-- + +CREATE SCHEMA IF NOT EXISTS pwc206; + +CREATE OR REPLACE FUNCTION +pwc206.task1_plperl( text[] ) +RETURNS int +AS $CODE$ + my ( $times ) = @_; + my $computations = {}; + my $min; + + my $diff = sub { + my ( $start, $end ) = @_; + $start =~ /^(\d{2}):(\d{2})$/; + my ( $start_hours, $start_mins ) = ( $1, $2 ); + + $end =~ /^(\d{2}):(\d{2})$/; + my ( $end_hours, $end_mins ) = ( $1, $2 ); + + if ( $start_hours == 0) { + $start_hours = 23; + $start_mins += 60; + } + + if ( $end_hours == 0) { + $end_hours = 23; + $end_mins += 60; + } + + return abs( $end_hours - $start_hours ) * 60 + abs( $end_mins - $start_mins ) % 60; + }; + + + for my $begin ( sort $times->@* ) { + for my $end ( sort $times->@* ) { + next if ( $begin eq $end ); + + my $difference = $diff->( $end, $begin ); + $computations->{ $difference } = [ $begin, $end ]; + $min = $difference if ( ! $min || $difference < $min ); + } + } + + elog(INFO, "Min is $min minutes from " . join( ',', $computations->{ $min }->@* ) ); + return $min; +$CODE$ +LANGUAGE plperl; -- cgit From 0023994b2906e66bf0f170c0fb5b492bb9eea3fb Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 27 Feb 2023 14:32:27 +0100 Subject: Task 2 plperl done --- challenge-206/luca-ferrari/postgresql/ch-2.plperl | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 challenge-206/luca-ferrari/postgresql/ch-2.plperl diff --git a/challenge-206/luca-ferrari/postgresql/ch-2.plperl b/challenge-206/luca-ferrari/postgresql/ch-2.plperl new file mode 100644 index 0000000000..d9cbbe220c --- /dev/null +++ b/challenge-206/luca-ferrari/postgresql/ch-2.plperl @@ -0,0 +1,23 @@ +-- +-- Perl Weekly Challenge 206 +-- Task 2 +-- See +-- + +CREATE SCHEMA IF NOT EXISTS pwc206; + +CREATE OR REPLACE FUNCTION +pwc206.task2_plperl( int[] ) +RETURNS int +AS $CODE$ + my ( $list ) = sort $_[ 0 ]; + my $sum = 0; + + while ( $list->@* ) { + $sum += shift $list->@*; + shift $list->@*; + } + + return $sum; +$CODE$ +LANGUAGE plperl; -- cgit From f9d896bb38edec71ac1e2663636c140bb3f4abf0 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 27 Feb 2023 14:44:37 +0100 Subject: Task 1 plpgsql done --- challenge-206/luca-ferrari/postgresql/ch-1.sql | 37 ++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 challenge-206/luca-ferrari/postgresql/ch-1.sql diff --git a/challenge-206/luca-ferrari/postgresql/ch-1.sql b/challenge-206/luca-ferrari/postgresql/ch-1.sql new file mode 100644 index 0000000000..3165bc3a97 --- /dev/null +++ b/challenge-206/luca-ferrari/postgresql/ch-1.sql @@ -0,0 +1,37 @@ +-- +-- Perl Weekly Challenge 206 +-- Task 1 +-- +-- See +-- + +CREATE SCHEMA IF NOT EXISTS pwc206; + +CREATE OR REPLACE FUNCTION +pwc206.task1_plpgsql( t text[] ) +RETURNS time +AS $CODE$ + +DECLARE + m time; + t1 text; + t2 text; +BEGIN + + FOREACH t1 IN ARRAY t LOOP + FOREACH t2 IN ARRAY t LOOP + IF t1 = t2 THEN + CONTINUE; + END IF; + + IF m IS NULL OR ( t2::time - t1::time ) < m THEN + m := ( t2::time - t1::time ); + END IF; + + END LOOP; + END LOOP; + + RETURN m; +END +$CODE$ --foo +LANGUAGE plpgsql; -- cgit From d7bc63303af1388af838f77db741dede81f499ed Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 27 Feb 2023 14:49:51 +0100 Subject: Task 2 plpgsql done --- challenge-206/luca-ferrari/postgresql/ch-2.sql | 30 ++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 challenge-206/luca-ferrari/postgresql/ch-2.sql diff --git a/challenge-206/luca-ferrari/postgresql/ch-2.sql b/challenge-206/luca-ferrari/postgresql/ch-2.sql new file mode 100644 index 0000000000..3add7366d5 --- /dev/null +++ b/challenge-206/luca-ferrari/postgresql/ch-2.sql @@ -0,0 +1,30 @@ +-- +-- Perl Weekly Challenge 206 +-- Task 1 +-- +-- See +-- + +CREATE SCHEMA IF NOT EXISTS pwc206; + +CREATE OR REPLACE FUNCTION +pwc206.task2_plpgsql( l int[] ) +RETURNS int +AS $CODE$ +DECLARE + res int; +BEGIN + WITH data AS ( + SELECT v, row_number() OVER ( ORDER BY v ) r + FROM unnest( l ) v + ) + SELECT sum( v ) + INTO res + FROM data + WHERE r % 2 <> 0 + ; + + RETURN res; +END +$CODE$ +LANGUAGE plpgsql; -- cgit From 32f6a2a74f6b0a050e251eb60c02851df0f4c49b Mon Sep 17 00:00:00 2001 From: Matthias Muth Date: Mon, 27 Feb 2023 14:53:51 +0100 Subject: Challenge 206 Perl solutions by Matthias Muth --- challenge-206/matthias-muth/README.md | 14 +++++--- challenge-206/matthias-muth/ch-1.pl | 33 +++++++++++++++++ challenge-206/matthias-muth/ch-2.pl | 68 +++++++++++++++++++++++++++++++++++ 3 files changed, 110 insertions(+), 5 deletions(-) create mode 100755 challenge-206/matthias-muth/ch-1.pl create mode 100755 challenge-206/matthias-muth/ch-2.pl diff --git a/challenge-206/matthias-muth/README.md b/challenge-206/matthias-muth/README.md index 758f229f45..8152988cdd 100644 --- a/challenge-206/matthias-muth/README.md +++ b/challenge-206/matthias-muth/README.md @@ -1,10 +1,14 @@ -# Uniq anyone? -**Challenge 205 solutions in Perl by Matthias Muth** +# All the permutations... +**Challenge 206 solutions in Perl by Matthias Muth** -## Task 1: Third Highest +## Task 1: Shortest Time -> You are given an array of integers.
-Write a script to find out the _Third Highest_ if found otherwise return the maximum. +> 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. + +The examples given are important for understanding that the shortest time between two time points might also span over midnight. + +##### CONTINUE HERE Thinking about a solution I start at the end:
I imagine simply getting the result from the ordered list of input values -- just take the third value, and it there isn't any, take the first. So sort and return -- easy! diff --git a/challenge-206/matthias-muth/ch-1.pl b/challenge-206/matthias-muth/ch-1.pl new file mode 100755 index 0000000000..b1ad5dbf2e --- /dev/null +++ b/challenge-206/matthias-muth/ch-1.pl @@ -0,0 +1,33 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'signatures'; +no warnings 'experimental::signatures'; + +use List::Util qw( min ); + +sub time_diffs( @t ) { + map { my $d = abs( $t[0] - $t[$_] ); min( $d, (24*60) - $d ); } 1..$#t; +} + +sub shortest_time( @hhmm_times ) { + my @t = map { /^(\d+):(\d{2})$/; $1 * 60 + $2 } @hhmm_times; + return min( map time_diffs( @t[$_..$#t] ), 0..$#t ); +} + + +use Test::More; + +my @tests = ( + [ [ "00:04", "23:55", "20:00" ], 9 ], + [ [ "00:00", "23:55", "20:00" ], 5 ], + [ [ "01:01", "00:50", "00:57" ], 4 ], + [ [ "10:10", "09:30", "09:00", "09:55" ], 15 ], +); + +is shortest_time( @{$_->[0]} ), $_->[1], + "shortest_time( @{$_->[0]} ) == " . ( $_->[1] // "undef" ) + for @tests; + +done_testing; diff --git a/challenge-206/matthias-muth/ch-2.pl b/challenge-206/matthias-muth/ch-2.pl new file mode 100755 index 0000000000..76758886f5 --- /dev/null +++ b/challenge-206/matthias-muth/ch-2.pl @@ -0,0 +1,68 @@ +#!/usr/bin/env perl + +use strict; +use warnings; + +use feature 'signatures'; +no warnings 'experimental::signatures'; + +use List::Util qw( min max sum ); + +sub permute( $a_ref ) { + return undef unless defined $a_ref && ref $a_ref eq 'ARRAY'; + return () if @$a_ref == 0; + return $a_ref if @$a_ref == 1; + + my @permutations; + for my $i ( 0..$#$a_ref ) { + my @others = @$a_ref; + my $extracted = splice( @others, $i, 1, () ); + push @permutations, [ $extracted, @$_ ] + for permute( [ @others ] ); + } + return @permutations; +} + +sub sum_of_min_of_pairs( @a ) { + return undef + unless @a % 2 == 0; + return + sum( map $_ % 2 == 0 ? min( $a[$_], $a[ $_ + 1 ] ) : 0, 0 .. $#a - 1 ); +} + +sub max_of_sums( @a ) { + return undef + unless @a % 2 == 0; + return + max( map sum_of_min_of_pairs( @$_ ), permute( [ @a ] ) ); +} + + +use Test::More; + +my @tests = ( + [ [], undef ], + [ [ 11 ], undef ], + [ [ 11,12 ], 11 ], + [ [ 1,2,3,4,5,6 ], 9 ], + [ [ 1,2,3,4 ], 4 ], + [ [ 0,2,1,3 ], 2 ], +); + +is max_of_sums( @{$_->[0]} ), $_->[1], + "max_of_sums( @{$_->[0]} ) == " . ( $_->[1] // "undef" ) + for @tests; + +done_testing; + +__END__ + +use v5.10; + +# For testing: +for ( @tests ) { + say "Permutations of @{$_->[0]}:"; + my @permutations = permute( [ @{$_->[0]} ] ); + say " @{$_}" + for @permutations; +} -- cgit From 234e36c18af3f3009930b02740b0dc3e302b6f16 Mon Sep 17 00:00:00 2001 From: Peter Campbell Smith Date: Mon, 27 Feb 2023 16:15:23 +0000 Subject: Week 206 --- challenge-206/peter-campbell-smith/blog.txt | 1 + challenge-206/peter-campbell-smith/perl/ch-01.pl | 54 ++++++++++++++++++++++++ challenge-206/peter-campbell-smith/perl/ch-02.pl | 32 ++++++++++++++ 3 files changed, 87 insertions(+) create mode 100644 challenge-206/peter-campbell-smith/blog.txt create mode 100755 challenge-206/peter-campbell-smith/perl/ch-01.pl create mode 100755 challenge-206/peter-campbell-smith/perl/ch-02.pl diff --git a/challenge-206/peter-campbell-smith/blog.txt b/challenge-206/peter-campbell-smith/blog.txt new file mode 100644 index 0000000000..909c6150e8 --- /dev/null +++ b/challenge-206/peter-campbell-smith/blog.txt @@ -0,0 +1 @@ +http://ccgi.campbellsmiths.force9.co.uk/challenge/206/1 diff --git a/challenge-206/peter-campbell-smith/perl/ch-01.pl b/challenge-206/peter-campbell-smith/perl/ch-01.pl new file mode 100755 index 0000000000..578dce976b --- /dev/null +++ b/challenge-206/peter-campbell-smith/perl/ch-01.pl @@ -0,0 +1,54 @@ +#!/usr/bin/perl + +# Peter Campbell Smith - 2023-02-20 + +use v5.28; +use utf8; +use warnings; + +# Task: 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. + +# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge/206/1 + +my (@time, $j); + +shortest_interval("00:01", "01:05", "01:12", "23:59"); +shortest_interval("01:01", "00:50", "00:57"); +shortest_interval("10:10", "09:30", "09:00", "09:55"); +shortest_interval("08:59", "21:00"); +shortest_interval("09:01", "21:00"); + +for $j (0 .. 20) { + @time[$j] = sprintf('%02d:%02d', rand(24), rand(60)); +} +shortest_interval(@time); + +sub shortest_interval { + + my (@times, $minutes, $shortest, $j, $gap, $this_minutes); + + # sort the times so that the shortest gap is bewteen 2 consecutive entries + @times = sort @_; + + # add one to the end that is 24hr after the first, in case the shortest bridges midnight + $times[0] =~ m|^(\d\d).(\d\d)|; + $times[scalar(@times)] = sprintf('%02d:%02d', $1 + 24, $2); + $minutes = $1 * 60 + $2; + + # pass along the list comparing each element with the previous one + $shortest = 9999; + for $j (1 .. scalar(@times) - 1) { + $times[$j] =~ m|^(\d\d).(\d\d)|; + $this_minutes = $1 * 60 + $2; + $gap = $this_minutes - $minutes; + $shortest = $gap if $gap < $shortest; + last if $shortest == 0; + $minutes = $this_minutes; + } + + say qq[\nInput: \@time = ("] . join('", "', @_) . qq[")\nOutput: $shortest]; +} + + + diff --git a/challenge-206/peter-campbell-smith/perl/ch-02.pl b/challenge-206/peter-campbell-smith/perl/ch-02.pl new file mode 100755 index 0000000000..b30a8f2d53 --- /dev/null +++ b/challenge-206/peter-campbell-smith/perl/ch-02.pl @@ -0,0 +1,32 @@ +#!/usr/bin/perl + +# Peter Campbell Smith - 2023-02-27 + +use v5.28; +use utf8; +use warnings; + +# Task: You are given an array of integers having even number of elements. Divide the array into +# all possible pairs of elements. Write a script to find the two pairs whose minima sum to the +# maximum value. + +# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge/206/1 + + +max_min(1, 2, 3, 4); +max_min(0, 2, 1, 3); +max_min(3, 7, 4, 12, 5, 1, 0, 10, 9, 2, 11, 13, -1, 6, 2); + +sub max_min { + + # reverse sort the array numerically + my @array = reverse sort { $a <=> $b} @_; + + # show the results (see blog for explanation) + say qq[\nInput: \@array = (] . join(', ', @_) . qq[)]; + say qq[Output: ] . ($array[1] + $array[3]) . + qq[ = min($array[0], $array[1]) + min($array[2], $array[3])]; +} + + + -- cgit From 49aa2ae8f267c571a01bfc4e708edb1a5733cdff Mon Sep 17 00:00:00 2001 From: David Ferrone Date: Mon, 27 Feb 2023 11:48:05 -0500 Subject: Week 206 --- challenge-206/zapwai/perl/ch-1.pl | 24 ++++++++++++++++++++++++ challenge-206/zapwai/perl/ch-2.pl | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 challenge-206/zapwai/perl/ch-1.pl create mode 100644 challenge-206/zapwai/perl/ch-2.pl diff --git a/challenge-206/zapwai/perl/ch-1.pl b/challenge-206/zapwai/perl/ch-1.pl new file mode 100644 index 0000000000..98aa650673 --- /dev/null +++ b/challenge-206/zapwai/perl/ch-1.pl @@ -0,0 +1,24 @@ +use v5.30.0; +my @time = ("00:00", "23:55", "20:00"); +#my @time= ("01:01", "00:50", "00:57"); +#my @time=("10:10", "09:30", "09:00", "09:55"); +print "Input: \@time = (".join(", ",@time).")\nOutput: "; +my @MIN; +foreach (@time) { + my ($hr, $mn) = split(":",$_); + push @MIN, $hr*60 + $mn; +} +my $min = 60*24; +my ($ind1,$ind2); +for my $i ( 0 .. $#MIN - 1) { + for my $j ( $i + 1 .. $#MIN) { + my $diff = abs $MIN[$i] - $MIN[$j]; + $diff = 60*24 - $diff if ($diff > 30*24); + if ($min > $diff) { + $min = $diff; + $ind1 = $i; + $ind2 = $j; + } + } +} +say "$min\n$min min is the difference between times $time[$ind1] and $time[$ind2]"; diff --git a/challenge-206/zapwai/perl/ch-2.pl b/challenge-206/zapwai/perl/ch-2.pl new file mode 100644 index 0000000000..e5724b4e85 --- /dev/null +++ b/challenge-206/zapwai/perl/ch-2.pl @@ -0,0 +1,32 @@ +use v5.30.0; +my @arr = (1,2,3,4); +sub min { + my ($a, $b) = @_; + ($a < $b) ? {return $a} : {return $b} +} +sub sum { + my ($a,$b,$c,$d) = @_; + min($a,$b) + min($c,$d) +} +my ($max,$out); +for my $i (0 .. $#arr - 3) { + for my $j ($i+1 .. $#arr - 2) { + for my $k ($j+1 .. $#arr - 1) { + for my $l ($k+1 .. $#arr) { + my ($I, $J, $K, $L) = ($arr[$i],$arr[$j],$arr[$k],$arr[$l]); + my $sum1 = sum($I,$J,$K,$L); + my $sum2 = sum($I,$K,$J,$L); + my $sum3 = sum($I,$L,$K,$J); + for ($sum1, $sum2, $sum3) { + $max = $_ if ($max < $_); + } + $out .= " ($arr[$i],$arr[$j]) & ($arr[$k],$arr[$l]) --> " . $sum1 ."\n"; + $out .= " ($arr[$i],$arr[$k]) & ($arr[$j],$arr[$l]) --> " . $sum2."\n"; + $out .= " ($arr[$i],$arr[$l]) & ($arr[$j],$arr[$k]) --> " . $sum3 ."\n"; + } + } + } +} +say "Input: \@array = (".join(",",@arr).")"; +say "Output: $max"; +print $out; -- cgit From 36fcf51fa7395602b2cba0652f68949c92000c85 Mon Sep 17 00:00:00 2001 From: Luis Mochan Date: Mon, 27 Feb 2023 12:38:28 -0600 Subject: Solve PWC206 --- challenge-206/wlmb/blog.txt | 2 ++ challenge-206/wlmb/perl/ch-1.pl | 24 ++++++++++++++++++++++++ challenge-206/wlmb/perl/ch-2.pl | 18 ++++++++++++++++++ 3 files changed, 44 insertions(+) create mode 100644 challenge-206/wlmb/blog.txt create mode 100755 challenge-206/wlmb/perl/ch-1.pl create mode 100755 challenge-206/wlmb/perl/ch-2.pl diff --git a/challenge-206/wlmb/blog.txt b/challenge-206/wlmb/blog.txt new file mode 100644 index 0000000000..6fe72cc5db --- /dev/null +++ b/challenge-206/wlmb/blog.txt @@ -0,0 +1,2 @@ +https://wlmb.github.io/2023/02/27/PWC206/ + diff --git a/challenge-206/wlmb/perl/ch-1.pl b/challenge-206/wlmb/perl/ch-1.pl new file mode 100755 index 0000000000..ae8b3d96d8 --- /dev/null +++ b/challenge-206/wlmb/perl/ch-1.pl @@ -0,0 +1,24 @@ +#!/usr/bin/env perl +# Perl weekly challenge 206 +# Task 1: Shortest Time +# +# See https://wlmb.github.io/2023/02/27/PWC206/#task-1-shortest-time +use v5.36; +use List::Util qw(min); +die <<~"FIN" unless @ARGV; + Usage: $0 T1 [T2...]HH:MM_2... + to find the shortest time between the time points T1, T2..., + each in the 24h format HH:MM + FIN +my ($current, @rest)=sort {$a <=> $b} map {to_minutes($_)} @ARGV; +push @rest, $current + 24*60; # Add first time as last time, next day +my $min = min map {my $diff=$_-$current; $current=$_; $diff} @rest; +say join " ", @ARGV, "->", $min; + +sub to_minutes($s){ + die "Wrong format, expected HH:MM: $s\n" unless $s=~/(\d\d?):(\d\d?)/; + my ($hour, $min)=($1, $2); + die "Hour should obey 0<=hour<24: $s" unless $hour<24; + die "Minute should obey 0<=minute<60: $s" unless $min<60; + return $hour*60+$min; +} diff --git a/challenge-206/wlmb/perl/ch-2.pl b/challenge-206/wlmb/perl/ch-2.pl new file mode 100755 index 0000000000..de0a3c9d3a --- /dev/null +++ b/challenge-206/wlmb/perl/ch-2.pl @@ -0,0 +1,18 @@ +#!/usr/bin/env perl +# Perl weekly challenge 206 +# Task 2: Array Pairings +# +# See https://wlmb.github.io/2023/02/27/PWC206/#task-2-array-pairings +use v5.36; +use List::Util qw(sum); +die <<~"FIN" unless @ARGV && @ARGV%2==0; + Usage: $0 N1 N2 [N3 N4...] + to find the maximum of the sum of the minima of each pair (Ni, Nj) + taken over all possible pairings. The number of arguments should be even. + FIN +my $i = 0; # counter +my $max = sum # sum + map {$i++%2?():$_} # every second element + sort {$a<=>$b} # of the sorted (ascending) + @ARGV; # input +say join " ", @ARGV, "->", $max; -- cgit From 62774e809fdabef49849468c2d894540cd08a583 Mon Sep 17 00:00:00 2001 From: Polgár Márton Date: Mon, 27 Feb 2023 21:08:20 +0100 Subject: Weeklies for 206th week by 2colours --- challenge-206/2colours/raku/ch-1.raku | 26 ++++++++++++++++++++++++++ challenge-206/2colours/raku/ch-2.raku | 13 +++++++++++++ 2 files changed, 39 insertions(+) create mode 100755 challenge-206/2colours/raku/ch-1.raku create mode 100755 challenge-206/2colours/raku/ch-2.raku diff --git a/challenge-206/2colours/raku/ch-1.raku b/challenge-206/2colours/raku/ch-1.raku new file mode 100755 index 0000000000..b0ea729a7a --- /dev/null +++ b/challenge-206/2colours/raku/ch-1.raku @@ -0,0 +1,26 @@ +#!/usr/bin/env raku + + +my token time-hours24 { \d ** 2 } +my token time-minutes { \d ** 2 } +my token time-o-clock { ':' } +my token time-list {^ '(' ['"' '"'] ** 2..* % ',' ')' $} + +sub infix:($lhs is copy, $rhs is copy) { + $_ = .split(':').&{ .[0] * 60 + .[1]} for ($lhs, $rhs); + ($lhs - $rhs) % 1440 +} + +sub MAIN($_) { + die 'Please provide a valid list of HH:MM times' unless S:g/\s// ~~ &time-list; + given my Str() @time = $ { + .=sort; + .push: .head; + } + say @time; + @time + .rotor: 2 => -1 andthen + .map: { .[1] t- .[0] } andthen + .min + .say; +} \ No newline at end of file diff --git a/challenge-206/2colours/raku/ch-2.raku b/challenge-206/2colours/raku/ch-2.raku new file mode 100755 index 0000000000..dd8a4d3895 --- /dev/null +++ b/challenge-206/2colours/raku/ch-2.raku @@ -0,0 +1,13 @@ +#!/usr/bin/env raku + + +my token unsigned-integer { 0 | <[1..9]><[0..9]>* }; +my token integer { '-'? }; +subset IntList of Str where /^ '(' * % ',' ')' $/; + + +sub MAIN(Str $array) { + die 'Please supply a valid list of integers.' unless $array.subst(/\s/, '', :g) ~~ IntList; + my Int() @array = $; + @array.sort.rotor(1 => 1).flat.sum.say; +} -- cgit From c639304fac77af9151623860c24e295091e9b8a7 Mon Sep 17 00:00:00 2001 From: "E. Choroba" Date: Mon, 27 Feb 2023 23:00:32 +0100 Subject: Add solutions to 206: Shortest Time & Array Pairings by E. Choroba --- challenge-206/e-choroba/perl/ch-1.pl | 26 +++++++++++++++++++ challenge-206/e-choroba/perl/ch-2.pl | 49 ++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100755 challenge-206/e-choroba/perl/ch-1.pl create mode 100755 challenge-206/e-choroba/perl/ch-2.pl diff --git a/challenge-206/e-choroba/perl/ch-1.pl b/challenge-206/e-choroba/perl/ch-1.pl new file mode 100755 index 0000000000..e77abd936f --- /dev/null +++ b/challenge-206/e-choroba/perl/ch-1.pl @@ -0,0 +1,26 @@ +#!/usr/bin/perl +use warnings; +use strict; +use experimental qw( signatures ); + +sub shortest_time(@timestamps) { + @timestamps = sort @timestamps; + my $min = 24 * 60 + 59; + for my $i (0 .. $#timestamps) { + my ($h0, $m0) = split /:/, $timestamps[$i]; + my ($h1, $m1) = split /:/, $timestamps[ ($i + 1) % (1 + $#timestamps) ]; + my $diff = 60 * $h1 + $m1 - 60 * $h0 - $m0 + + ($i == $#timestamps ? 24 * 60 : 0); + $min = $diff if $diff < $min; + } + return $min +} + +use Test::More tests => 3 + 2; + +is shortest_time("00:00", "23:55", "20:00"), 5, 'Example 1'; +is shortest_time("01:01", "00:50", "00:57"), 4, 'Example 2'; +is shortest_time("10:10", "09:30", "09:00", "09:55"), 15, 'Example 3'; + +is shortest_time("00:10", "00:10", "22:50"), 0, 'Zero'; +is shortest_time("01:10", "05:20", "17:40"), 250, 'More than 1 hour'; diff --git a/challenge-206/e-choroba/perl/ch-2.pl b/challenge-206/e-choroba/perl/ch-2.pl new file mode 100755 index 0000000000..9bdba6dab2 --- /dev/null +++ b/challenge-206/e-choroba/perl/ch-2.pl @@ -0,0 +1,49 @@ +#!/usr/bin/perl +use warnings; +use strict; +use experimental qw( signatures ); + +use List::Util qw{ sum }; + +sub array_pairings(@arr) { + my $i = 0; + return sum(grep ++$i % 2, sort { $a <=> $b } @arr) +} + +sub array_pairings_slow(@arr) { + my $s = 0; + for my $i (0 .. $#arr) { + for my $j (0 .. $#arr) { + next if $i == $j; + + my $c = $arr[ $arr[$i] < $arr[$j] ? $i : $j ] + + array_pairings(@arr[grep $_ != $i && $_ != $j, 0 .. $#arr]); + $s = $c if $c > $s; + } + } + return $s +} + +use Test::More tests => 2 + 2 + 20; + +is array_pairings(1, 2, 3, 4), 4, 'Example 1'; +is array_pairings(0, 2, 1, 3), 2, 'Example 2'; + +is array_pairings_slow(1, 2, 3, 4), 4, 'Slow example 1'; +is array_pairings_slow(0, 2, 1, 3), 2, 'Slow example 2'; + +for (1 .. 20) { + my @arr = map int rand 20, 1 .. 10; + is array_pairings(@arr), array_pairings_slow(@arr), "Random @arr"; +} + +use Benchmark qw{ cmpthese }; +cmpthese(-3, { + slow => sub { array_pairings_slow(1 .. 8) }, + fast => sub { array_pairings(1 .. 8) }, +}); + +__END__ + Rate slow fast +slow 7197/s -- -99% +fast 649241/s 8921% -- -- cgit From ae4d1ff408793ea702e876a4a854894b04d3e18c Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Tue, 28 Feb 2023 00:34:11 +0000 Subject: ch-1.raku --- challenge-206/mark-anderson/raku/ch-2.raku | 17 ----------------- 1 file changed, 17 deletions(-) delete mode 100644 challenge-206/mark-anderson/raku/ch-2.raku diff --git a/challenge-206/mark-anderson/raku/ch-2.raku b/challenge-206/mark-anderson/raku/ch-2.raku deleted file mode 100644 index 3358092efc..0000000000 --- a/challenge-206/mark-anderson/raku/ch-2.raku +++ /dev/null @@ -1,17 +0,0 @@ -#!/usr/bin/env raku -use Test; - -is array-pairings(< 1 2 3 4 >), 4; -is array-pairings(< 0 2 1 3 >), 2; -is array-pairings(< 1 2 3 4 5 6 >), 5; - -sub array-pairings(@a where * %% 2) -{ - my @c = @a.combinations(@a.elems div 2); - - max map - { - sum .map({ .min }) - }, - (.[^(.elems div 2)] Z .[.elems div 2 .. .end].reverse) given @c -} -- cgit From b0c85458d55af55be49e2f5a3632c730aad8e7cd Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Tue, 28 Feb 2023 10:48:21 +0100 Subject: Blog references --- challenge-206/luca-ferrari/blog-1.txt | 1 + challenge-206/luca-ferrari/blog-2.txt | 1 + challenge-206/luca-ferrari/blog-3.txt | 1 + challenge-206/luca-ferrari/blog-4.txt | 1 + challenge-206/luca-ferrari/blog-5.txt | 1 + challenge-206/luca-ferrari/blog-6.txt | 1 + challenge-206/luca-ferrari/postgresql/ch-1.sql | 2 +- challenge-206/luca-ferrari/postgresql/ch-2.sql | 4 ++-- 8 files changed, 9 insertions(+), 3 deletions(-) create mode 100644 challenge-206/luca-ferrari/blog-1.txt create mode 100644 challenge-206/luca-ferrari/blog-2.txt create mode 100644 challenge-206/luca-ferrari/blog-3.txt create mode 100644 challenge-206/luca-ferrari/blog-4.txt create mode 100644 challenge-206/luca-ferrari/blog-5.txt create mode 100644 challenge-206/luca-ferrari/blog-6.txt diff --git a/challenge-206/luca-ferrari/blog-1.txt b/challenge-206/luca-ferrari/blog-1.txt new file mode 100644 index 0000000000..b4e5f2d251 --- /dev/null +++ b/challenge-206/luca-ferrari/blog-1.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/02/27/PerlWeeklyChallenge206.html#task1 diff --git a/challenge-206/luca-ferrari/blog-2.txt b/challenge-206/luca-ferrari/blog-2.txt new file mode 100644 index 0000000000..bbeb3b8149 --- /dev/null +++ b/challenge-206/luca-ferrari/blog-2.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/02/27/PerlWeeklyChallenge206.html#task2 diff --git a/challenge-206/luca-ferrari/blog-3.txt b/challenge-206/luca-ferrari/blog-3.txt new file mode 100644 index 0000000000..ba8342161d --- /dev/null +++ b/challenge-206/luca-ferrari/blog-3.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/02/27/PerlWeeklyChallenge206.html#task1plperl diff --git a/challenge-206/luca-ferrari/blog-4.txt b/challenge-206/luca-ferrari/blog-4.txt new file mode 100644 index 0000000000..072ce91cbe --- /dev/null +++ b/challenge-206/luca-ferrari/blog-4.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/02/27/PerlWeeklyChallenge206.html#task2plperl diff --git a/challenge-206/luca-ferrari/blog-5.txt b/challenge-206/luca-ferrari/blog-5.txt new file mode 100644 index 0000000000..d444dccdd3 --- /dev/null +++ b/challenge-206/luca-ferrari/blog-5.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/02/27/PerlWeeklyChallenge206.html#task1plpgsql diff --git a/challenge-206/luca-ferrari/blog-6.txt b/challenge-206/luca-ferrari/blog-6.txt new file mode 100644 index 0000000000..ffb4ce2f96 --- /dev/null +++ b/challenge-206/luca-ferrari/blog-6.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/02/27/PerlWeeklyChallenge206.html#task2plpgsql diff --git a/challenge-206/luca-ferrari/postgresql/ch-1.sql b/challenge-206/luca-ferrari/postgresql/ch-1.sql index 3165bc3a97..3f3e36f47b 100644 --- a/challenge-206/luca-ferrari/postgresql/ch-1.sql +++ b/challenge-206/luca-ferrari/postgresql/ch-1.sql @@ -33,5 +33,5 @@ BEGIN RETURN m; END -$CODE$ --foo +$CODE$ LANGUAGE plpgsql; diff --git a/challenge-206/luca-ferrari/postgresql/ch-2.sql b/challenge-206/luca-ferrari/postgresql/ch-2.sql index 3add7366d5..0c73ec4c72 100644 --- a/challenge-206/luca-ferrari/postgresql/ch-2.sql +++ b/challenge-206/luca-ferrari/postgresql/ch-2.sql @@ -15,8 +15,8 @@ DECLARE res int; BEGIN WITH data AS ( - SELECT v, row_number() OVER ( ORDER BY v ) r - FROM unnest( l ) v + SELECT v, row_number() OVER ( ORDER BY v ) r + FROM unnest( l ) v ) SELECT sum( v ) INTO res -- cgit From 83e4763feaf1ad26134fa66151fb3098082e9f19 Mon Sep 17 00:00:00 2001 From: Mariano Spadaccini Date: Tue, 28 Feb 2023 11:26:35 +0100 Subject: PWC 206 - Perl, Python, Go --- challenge-206/spadacciniweb/go/ch-1.go | 86 ++++++++++++++++++++++++++++++ challenge-206/spadacciniweb/perl/ch-1.pl | 74 +++++++++++++++++++++++++ challenge-206/spadacciniweb/perl/ch-2.pl | 64 ++++++++++++++++++++++ challenge-206/spadacciniweb/python/ch-1.py | 58 ++++++++++++++++++++ challenge-206/spadacciniweb/python/ch-2.py | 73 +++++++++++++++++++++++++ 5 files changed, 355 insertions(+) create mode 100644 challenge-206/spadacciniweb/go/ch-1.go create mode 100644 challenge-206/spadacciniweb/perl/ch-1.pl create mode 100644 challenge-206/spadacciniweb/perl/ch-2.pl create mode 100644 challenge-206/spadacciniweb/python/ch-1.py create mode 100644 challenge-206/spadacciniweb/python/ch-2.py 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])) -- cgit From 47a0042586cc6650559cb4768bb2f7b865c8c8f9 Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Tue, 28 Feb 2023 15:01:07 +0000 Subject: Add Perl solution to challenge 206 --- challenge-206/paulo-custodio/perl/ch-1.pl | 46 ++++++++++++++++++ challenge-206/paulo-custodio/perl/ch-2.pl | 75 ++++++++++++++++++++++++++++++ challenge-206/paulo-custodio/t/test-1.yaml | 15 ++++++ challenge-206/paulo-custodio/t/test-2.yaml | 10 ++++ 4 files changed, 146 insertions(+) create mode 100644 challenge-206/paulo-custodio/perl/ch-1.pl create mode 100644 challenge-206/paulo-custodio/perl/ch-2.pl create mode 100644 challenge-206/paulo-custodio/t/test-1.yaml create mode 100644 challenge-206/paulo-custodio/t/test-2.yaml diff --git a/challenge-206/paulo-custodio/perl/ch-1.pl b/challenge-206/paulo-custodio/perl/ch-1.pl new file mode 100644 i