diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2023-03-04 19:09:05 +0000 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2023-03-04 19:09:05 +0000 |
| commit | d6adedc738b5d22b6c6e315197b5e0716ef5a7c3 (patch) | |
| tree | 81e3afae5cda90885c17abe5090bbaab96cf3bfe | |
| parent | 22df9848e4301632bdc10eae5bd2064b726288a3 (diff) | |
| download | perlweeklychallenge-club-d6adedc738b5d22b6c6e315197b5e0716ef5a7c3.tar.gz perlweeklychallenge-club-d6adedc738b5d22b6c6e315197b5e0716ef5a7c3.tar.bz2 perlweeklychallenge-club-d6adedc738b5d22b6c6e315197b5e0716ef5a7c3.zip | |
- Added solutions by Mark Anderson.
- Added solutions by Simon Green.
- Added solutions by James Smith.
- Added solutions by Peter Campbell Smith.
- Added solutions by David Ferrone.
- Added solutions by W. Luis Mochan.
- Added solutions by Marton Polgar.
- Added solutions by E. Choroba.
- Added solutions by Luca Ferrari.
- Added solutions by Mariano Spadaccini.
- Added solutions by Paulo Custodio.
- Added solutions by Roger Bell_West.
- Added solutions by Lubos Kolouch.
- Added solutions by Dave Jacoby.
- Added solutions by Niels van Dijke.
- Added solutions by Thomas Kohler.
- Added solutions by Flavio Poletti.
- Added solutions by Robert Ransbottom.
- Added solutions by Jorg Sommrey.
- Added solutions by Robbie Hatley.
- Added solutions by Ulrich Rieke.
- Added solutions by Laurent Rosenfeld.
- Added solutions by Robert DiCicco.
49 files changed, 4103 insertions, 2539 deletions
diff --git a/challenge-206/eric-cheung/python/ch-1.py b/challenge-206/eric-cheung/python/ch-1.py new file mode 100755 index 0000000000..4968d9ef1a --- /dev/null +++ b/challenge-206/eric-cheung/python/ch-1.py @@ -0,0 +1,24 @@ +
+from datetime import datetime
+
+## arrTimeInput = ["00:00", "23:55", "20:00"] ## Example 1
+## arrTimeInput = ["01:01", "00:50", "00:57"] ## Example 2
+arrTimeInput = ["10:10", "09:30", "09:00", "09:55"] ## Example 3
+
+arrTimeMinDiff = []
+
+for nIndxLoop_01 in range(0, len(arrTimeInput) - 1):
+ for nIndxLoop_02 in range(nIndxLoop_01 + 1, len(arrTimeInput)):
+
+ tLoop_01 = datetime.strptime(arrTimeInput[nIndxLoop_01], "%H:%M")
+ tLoop_02 = datetime.strptime(arrTimeInput[nIndxLoop_02], "%H:%M")
+
+ if tLoop_01 <= tLoop_02:
+ tDiff = (tLoop_02 - tLoop_01).total_seconds()
+ else:
+ tDiff = (tLoop_01 - tLoop_02).total_seconds()
+
+ arrTimeMinDiff.append(int(tDiff / 60))
+ arrTimeMinDiff.append(24 * 60 - int(tDiff / 60))
+
+print (min(arrTimeMinDiff))
diff --git a/challenge-206/eric-cheung/python/ch-2.py b/challenge-206/eric-cheung/python/ch-2.py new file mode 100755 index 0000000000..3b9ae04009 --- /dev/null +++ b/challenge-206/eric-cheung/python/ch-2.py @@ -0,0 +1,41 @@ +
+from itertools import combinations
+
+def GetSetFromArrInput(arrFuncInput):
+
+ arrOutputArr = []
+
+ arrCombList = combinations(arrFuncInput, 2)
+
+ for loopComb in list(arrCombList):
+ arrOutputArr.append([loopComb[0], loopComb[1]])
+
+ return arrOutputArr
+
+
+## arrInput = [1, 2, 3, 4] ## Example 1
+arrInput = [0, 2, 1, 3] ## Example 2
+
+arrCombSubList = combinations(range(0, len(arrInput)), 2)
+
+arrSumOutput = []
+
+for loopSubComb in list(arrCombSubList):
+
+ arrSubInput = arrInput[:]
+
+ arrSubList_01 = [arrSubInput[loopSubComb[0]], arrSubInput[loopSubComb[1]]]
+
+ arrSubInput.pop(loopSubComb[1])
+ arrSubInput.pop(loopSubComb[0])
+
+ arrSubList_02 = GetSetFromArrInput(arrSubInput)
+
+ for nLoop in range(0, len(arrSubList_02)):
+
+ if arrSubList_01[0] > arrSubList_02[nLoop][0]:
+ continue
+
+ arrSumOutput.append(min(arrSubList_01) + min(arrSubList_02[nLoop]))
+
+print (max(arrSumOutput))
diff --git a/challenge-206/laurent-rosenfeld/blog.txt b/challenge-206/laurent-rosenfeld/blog.txt new file mode 100644 index 0000000000..65b1a61974 --- /dev/null +++ b/challenge-206/laurent-rosenfeld/blog.txt @@ -0,0 +1 @@ +https://blogs.perl.org/users/laurent_r/2023/02/perl-weekly-challenge-206-shortest-time-and-array-pairings.html diff --git a/challenge-206/laurent-rosenfeld/perl/ch-1.pl b/challenge-206/laurent-rosenfeld/perl/ch-1.pl new file mode 100644 index 0000000000..fae1a893a8 --- /dev/null +++ b/challenge-206/laurent-rosenfeld/perl/ch-1.pl @@ -0,0 +1,27 @@ +use strict; +use warnings; +use feature "say"; + +sub shortest { + my $mod = 60 * 12; + my ($h1, $m1) = split /:/, $_[0]; + my ($h2, $m2) = split /:/, $_[1]; + return abs ($m1 - $m2) if $h1 == $h2; + my $delta = abs(($h1 * 60 + $m1) - ($h2 * 60 + $m2)); + $delta = $mod * 2 - $delta if $delta > $mod; + return $delta +} + +for my $test (["00:00", "23:55", "20:00"], + ["01:01", "00:50", "00:57"], + ["10:10", "09:30", "09:00", "09:55"]) { + my $min = 10000; # larger than any HH:MM time diff + my @t = @$test; + for my $i (0..$#t) { + for my $j ($i+1..$#t) { + my $diff = shortest $t[$i], $t[$j]; + $min = $diff if $diff < $min; + } + } + printf "%-25s => %d\n", "@t", $min; +} diff --git a/challenge-206/laurent-rosenfeld/perl/ch-2.pl b/challenge-206/laurent-rosenfeld/perl/ch-2.pl new file mode 100644 index 0000000000..21e055f201 --- /dev/null +++ b/challenge-206/laurent-rosenfeld/perl/ch-2.pl @@ -0,0 +1,13 @@ +use strict; +use warnings; +use feature "say"; + +sub pairings { + my @sorted = sort { $b <=> $a } @_; + return $sorted[1] + $sorted[3]; +} + +for my $test ([<1 2 3 4>], [<6 5 4 3 2 1>], + [<0 2 1 3>], [<34 12 1 11>]) { + printf "%-15s => %d\n", "@$test", pairings @$test; +} diff --git a/challenge-206/laurent-rosenfeld/raku/ch-1.raku b/challenge-206/laurent-rosenfeld/raku/ch-1.raku new file mode 100644 index 0000000000..428aec28d5 --- /dev/null +++ b/challenge-206/laurent-rosenfeld/raku/ch-1.raku @@ -0,0 +1,19 @@ +sub shortest ($t1, $t2) { + my $mod = 60 * 12; # half a day + my ($h1, $m1) = split /\:/, $t1; + my ($h2, $m2) = split /\:/, $t2; + return abs ($m1 - $m2) if $h1 == $h2; + my $delta = abs(($h1 * 60 + $m1) - ($h2 * 60 + $m2)); + $delta = $mod * 2 - $delta if $delta > $mod; +} + +for ("00:00", "23:55", "20:00"), + ("01:01", "00:50", "00:57"), + ("10:10", "09:30", "09:00", "09:55") -> @test { + my $min = Inf; + for @test.combinations(2) -> @comb { + my $diff = shortest @comb[0], @comb[1]; + $min = $diff if $diff < $min; + } + say "@test[]".fmt("%-25s => "), $min; +} diff --git a/challenge-206/laurent-rosenfeld/raku/ch-2.raku b/challenge-206/laurent-rosenfeld/raku/ch-2.raku new file mode 100644 index 0000000000..d6333f9d97 --- /dev/null +++ b/challenge-206/laurent-rosenfeld/raku/ch-2.raku @@ -0,0 +1,14 @@ +sub pairings (@in) { + my $max = - Inf; + my @perms = @in.permutations; + for @perms -> $perm { + for $perm.rotor(2).combinations(2) -> $comb { + my $sum = $comb[0].min + $comb[1].min; + $max = $sum if $sum > $max + } + } + return $max; +} +for <1 2 3 4>, <6 5 4 3 2 1>, <0 2 1 3> -> @test { + say "@test[]".fmt("%-15s => "), pairings @test; +} diff --git a/challenge-206/laurent-rosenfeld/raku/ch-2a.raku b/challenge-206/laurent-rosenfeld/raku/ch-2a.raku new file mode 100644 index 0000000000..d61d5acb45 --- /dev/null +++ b/challenge-206/laurent-rosenfeld/raku/ch-2a.raku @@ -0,0 +1,7 @@ +sub pairings (@in) { + my @sorted = @in.sort.reverse; + return @sorted[1] + @sorted[3]; +} +for <1 2 3 4>, <6 5 4 3 2 1>, <0 2 1 3> -> @test { + say "@test[]".fmt("%-15s => "), pairings @test; +} diff --git a/challenge-206/perlboy1967/perl/ch1.pl b/challenge-206/perlboy1967/perl/ch-1.pl index 6fb94c1d27..6fb94c1d27 100755 --- a/challenge-206/perlboy1967/perl/ch1.pl +++ b/challenge-206/perlboy1967/perl/ch-1.pl diff --git a/challenge-206/perlboy1967/perl/ch2.pl b/challenge-206/perlboy1967/perl/ch-2.pl index 6d77e90d1a..6d77e90d1a 100755 --- a/challenge-206/perlboy1967/perl/ch2.pl +++ b/challenge-206/perlboy1967/perl/ch-2.pl diff --git a/challenge-206/peter-campbell-smith/perl/ch-01.pl b/challenge-206/peter-campbell-smith/perl/ch-1.pl index 578dce976b..578dce976b 100755 --- a/challenge-206/peter-campbell-smith/perl/ch-01.pl +++ b/challenge-206/peter-campbell-smith/perl/ch-1.pl diff --git a/challenge-206/peter-campbell-smith/perl/ch-02.pl b/challenge-206/peter-campbell-smith/perl/ch-2.pl index b30a8f2d53..b30a8f2d53 100755 --- a/challenge-206/peter-campbell-smith/perl/ch-02.pl +++ b/challenge-206/peter-campbell-smith/perl/ch-2.pl diff --git a/challenge-206/robert-dicicco/julia/ch-1.jl b/challenge-206/robert-dicicco/julia/ch-1.jl new file mode 100644 index 0000000000..de693e3a7d --- /dev/null +++ b/challenge-206/robert-dicicco/julia/ch-1.jl @@ -0,0 +1,60 @@ +#!/use/bin/env julia +#= +--------------------------------------- +AUTHOR: Robert DiCicco +DATE : 2023-02=28 +Challenge Shorteest Times ( Julia ) +--------------------------------------- +Remember, in Julia, array offsets start at 1 +=# +using Printf +using Combinatorics + +times = [["00:00", "23:55", "20:00"],["01:01", "00:50", "00:57"],["10:10", "09:30", "09:00", "09:55"]] +minutes = Any[] # array to store times converted to minutes + +function ResetValues() + shortest = 1441 # This should be more than the number of minutes in 24 hrs + empty!(minutes) # this clears out our holding array +end + +for tm in times + @printf("Input: @array = %s\n",tm) + ln = length(tm) # get the number of entries in our time array + for x in 1:ln # and for each entry, convert to minutes and store in our 'minutes' array + times_split = split(tm[x],':') + hrs = parse(Int64, times_split[1]) + mins = parse(Int64, times_split[2]) + if hrs == 0 && mins == 0 + hrs = 24 + end + hrs = hrs * 60 + time_in_mins = hrs+mins + push!(minutes,time_in_mins) + end + shortest = 1441 + for combos in combinations(minutes,2) # create a list of 2 times to compare to each other + minval = abs(combos[1] - combos[2]) + if minval < shortest # and find the shortest + shortest = minval + end + end + @printf("Output: %d\n\n",shortest) # print it, and reset values for next group + ResetValues() +end + +#= +--------------------------------------- +SAMPLE OUTPUT +julia .\ShortestTime.jl +Input: @array = ["00:00", "23:55", "20:00"] +Output: 5 + +Input: @array = ["01:01", "00:50", "00:57"] +Output: 4 + +Input: @array = ["10:10", "09:30", "09:00", "09:55"] +Output: 15 +--------------------------------------- +=# + diff --git a/challenge-206/robert-dicicco/julia/ch-2.jl b/challenge-206/robert-dicicco/julia/ch-2.jl new file mode 100644 index 0000000000..a1bde9ec60 --- /dev/null +++ b/challenge-206/robert-dicicco/julia/ch-2.jl @@ -0,0 +1,43 @@ +#!/usr/bin/env julia +#= +---------------------------------------- +AUTHOR: Robert DiCicco +DATE : 2023-03-01 +Challenge 206 Array Pairings ( Julia ) +lia---------------------------------------- +=# + +using Printf + +arrs = [[1,2,3,4],[0,2,1,3]] +pairs = [[0,1,2,3], [0,2,1,3], [0,3,1,2]] +maxval = 0 + +for arr in arrs + global maxval + @printf("Input: @array = %s\n",arr) + for pr in pairs + a1 = pr[1:2] + sum1 = arr[minimum(a1)+1] + a2 = pr[3:4] + sum2 = arr[minimum(a2)+1] + sum = sum1 + sum2 + if sum > maxval + maxval = sum + end + end + @printf("Output: %d\n\n",maxval) + maxval = 0 +end + +#= +---------------------------------------- +SAMPLE OUTPUT +julia .\ArrayPairings.jl +Input: @array = [1, 2, 3, 4] +Output: 4 + +Input: @array = [0, 2, 1, 3] +Output: 2 +---------------------------------------- +=# diff --git a/challenge-206/robert-dicicco/perl/ch-1.pl b/challenge-206/robert-dicicco/perl/ch-1.pl new file mode 100644 index 0000000000..aa7bb514ad --- /dev/null +++ b/challenge-206/robert-dicicco/perl/ch-1.pl @@ -0,0 +1,56 @@ +#!/usr/bin/env perl +=begin pod +--------------------------------------- +AUTHOR: Robert DiCicco +DATE : 2023-02-27 +Challenge 206 Shortest Time ( Perl ) +--------------------------------------- +=cut +use strict; +use warnings; +use feature "say"; +use Algorithm::Combinatorics qw(permutations combinations); + +my @times = (["00:00", "23:55", "20:00"],["01:01", "00:50", "00:57"],["10:10", "09:30", "09:00", "09:55"]); + +sub ToMins { + my $a = shift; + if (@$a[0] eq "00:00") { @$a[0] = "24:00"}; + if (@$a[1] eq "00:00") { @$a[1] = "24:00"}; + my ($hrs, $mins) = split(":",@$a[0]); + my $t0_mins = ($hrs * 60) + $mins; + ($hrs, $mins) = split(":",@$a[1]); + my $t1_mins = ($hrs * 60) + $mins; + return(abs($t0_mins - $t1_mins)); +} + +for my $tm (@times) { + print("Input: \@time = \(",@$tm,"\)\n"); + my $shortest = 1441; + my $iter = combinations($tm, 2); + while (my $c = $iter->next) { + my @arr = (); + push(@arr,@$c[0]); + push(@arr,@$c[1]); + my $diff = ToMins(\@arr); + if ( $diff < $shortest) {$shortest = $diff}; + } + print("Output: $shortest\n\n"); +} + +=begin pod +--------------------------------------- +SAMPLE OUTPUT +perl .\ShortestTime.pl +Input: @time = (00:0023:5520:00) +Output: 5 + +Input: @time = (01:0100:5000:57) +Output: 4 + +Input: @time = (10:1009:3009:0009:55) +Output: 15 +--------------------------------------- + + + diff --git a/challenge-206/robert-dicicco/perl/ch-2.pl b/challenge-206/robert-dicicco/perl/ch-2.pl new file mode 100644 index 0000000000..38a82e1272 --- /dev/null +++ b/challenge-206/robert-dicicco/perl/ch-2.pl @@ -0,0 +1,39 @@ +#!/usr/bin/env perl +#--------------------------------------- +# AUTHOR: Robert DiCicco +# DATE : 2023-03-02 +# Challenge 206 Array P:airings ( Perl ) +#-------------------------------------- +use strict; +use warnings; +use feature 'say'; +use List::Util qw/min/; + +my @arrs = ([1,2,3,4],[0,2,1,3]); +my @pairs = ([0,1,2,3], [0,2,1,3], [0,3,1,2]); +my $maxval = 0; + +for my $arr (@arrs) { + say "Input: \@array = [",@$arr,"]"; + for my $pr (@pairs) { + my @a1 = @$pr; + my $sum = @$arr[min(@a1[0..1])] + @$arr[min(@a1[2..3])]; + if ($sum > $maxval) { + $maxval = $sum; + } + } + print "Output: $maxval\n\n"; + $maxval = 0; +} + +#--------------------------------------- +# SAMPLE OUTPUT +# perl .\ArrayPairings.pl +# Input: @array = [1234] +# Output: 4 + +# Input: @array = [0213] +# Output: 2 +#--------------------------------------- + + diff --git a/challenge-206/robert-dicicco/python/ch-1.py b/challenge-206/robert-dicicco/python/ch-1.py new file mode 100644 index 0000000000..175285f134 --- /dev/null +++ b/challenge-206/robert-dicicco/python/ch-1.py @@ -0,0 +1,49 @@ +#!/usr/bin/env python +''' +--------------------------------------- +AUTHOR: Robert DiCicco +DATE : 2023-02=28 +Challenge 206 Shortest Times ( Python ) +--------------------------------------- +''' +from itertools import combinations + +times = [["00:00", "23:55", "20:00"],["01:01", "00:50", "00:57"],["10:10", "09:30", "09:00", "09:55"]] + +for tm in times: + print("Input: @array = ", tm) + shortest = 1441 + minutes = [] + absval = 0 + + for n in range(len(tm)): + time_split = tm[n].split(':') + hrs = int(time_split[0]) + mins = int(time_split[1]) + time_in_mins = (hrs * 60) + mins + if (time_in_mins == 0): + time_in_mins = 1440 + minutes.append(time_in_mins) + + ax = combinations(minutes, 2) + for res in list(ax): + absval = abs(res[0] - res[1]) + if (absval < shortest): + shortest = absval + + print(f"Output: {shortest}\n") + +''' +--------------------------------------- +SAMPLE OUTPUT +python .\ShortestTime.py +Input: @array = ['00:00', '23:55', '20:00'] +Output: 5 + +Input: @array = ['01:01', '00:50', '00:57'] +Output: 4 + +Input: @array = ['10:10', '09:30', '09:00', '09:55'] +Output: 15 +--------------------------------------- +''' diff --git a/challenge-206/robert-dicicco/python/ch-2.py b/challenge-206/robert-dicicco/python/ch-2.py new file mode 100644 index 0000000000..bd0c36db36 --- /dev/null +++ b/challenge-206/robert-dicicco/python/ch-2.py @@ -0,0 +1,42 @@ +#!/usr/bin/env python +''' +---------------------------------------- +AUTHOR: Robert DiCicco +DATE : 2023-03-01 +Challenge 206 Array Pairings ( Python ) +---------------------------------------- +''' + +import array as a + +arrs = [[1,2,3,4],[0,2,1,3]] +pairs = [[0,1,2,3], [0,2,1,3], [0,3,1,2]] +maxval = 0 + +for arr in arrs: + print("Input: @array = ",arr) + for pr in pairs: + a1 = pr[0:2] + sum1 = arr[min(a1)] + a2 = pr[2:] + sum2 = arr[min(a2)] + sum = sum1 + sum2 + if sum > maxval : + maxval = sum + + print("Output: ",maxval,"\n") + maxval = 0 + +''' +---------------------------------------- +SAMPLE OUTPUT +python .\ArrayPairings.py +Input: @array = [1, 2, 3, 4] +Output: 4 + +Input: @array = [0, 2, 1, 3] +Output: 2 +---------------------------------------- +''' + + diff --git a/challenge-206/robert-dicicco/raku/ch-1.raku b/challenge-206/robert-dicicco/raku/ch-1.raku new file mode 100644 index 0000000000..3eb45da979 --- /dev/null +++ b/challenge-206/robert-dicicco/raku/ch-1.raku @@ -0,0 +1,52 @@ +#!/usr/bin/env raku +#`{ +--------------------------------------- +AUTHOR: Robert DiCicco +DATE : 2023-02-27 +Challenge 206 Shortest Time ( Raku ) +--------------------------------------- +} +use v6; + +my @times = (["00:00", "23:55", "20:00"],["01:01", "00:50", "00:57"],["10:10", "09:30", "09:00", "09:55"]); + +sub ToMins(@tm) { + if (@tm[0] eq "00:00") { @tm[0] = "24:00"}; + if (@tm[1] eq "00:00") { @tm[1] = "24:00"}; + my ($hrs, $mins) = split(":",@tm[0]); + my $t0_mins = ($hrs * 60) + $mins; + ($hrs, $mins) = split(":",@tm[1]); + my $t1_mins = ($hrs * 60) + $mins; + return(abs($t0_mins - $t1_mins)); +} + +for (@times) -> @tm { + print("Input: \@time = \(",@tm,"\)\n"); + my $shortest = 1441; + for (0..@tm.elems-1).combinations: 2 -> @res { + my @arr =(); + @arr.push(@tm[@res[0]]); + @arr.push(@tm[@res[1]]); + my $diff = ToMins(@arr); + if $diff < $shortest { + $shortest = $diff; + } + } + print("Output: $shortest\n\n"); +} + +#`{ +--------------------------------------- +SAMPLE OUTPUT +raku .\ShortestTime.rk +Input: @time = (00:00 23:55 20:00) +Output: 5 + +Input: @time = (01:01 00:50 00:57) +Output: 4 + +Input: @time = (10:10 09:30 09:00 09:55) +Output: 15 +} + + diff --git a/challenge-206/robert-dicicco/raku/ch-2.raku b/challenge-206/robert-dicicco/raku/ch-2.raku new file mode 100644 index 0000000000..1b1b9e5423 --- /dev/null +++ b/challenge-206/robert-dicicco/raku/ch-2.raku @@ -0,0 +1,47 @@ +#!/usr/bin/env raku +#`{ +---------------------------------------- +AUTHOR: Robert DiCicco +DATE : 2023-03-01 +Challenge 206 Array Pairings ( Raku ) +---------------------------------------- +} + +use v6; + +my @arrs = ([1,2,3,4],[0,2,1,3]); +my @pairs = ([0,1,2,3], [0,2,1,3], [0,3,1,2]); +my $maxval = 0; + +for (@arrs) -> @a { + say "Input: \@array = ",@a; + for (@pairs) -> @p { + #$maxval = 0; + + my @a1 = @p[0..1]; + my $val1 = @a[@a1].min; + + my @a2 = @p[2..3]; + my $val2 = @a[@a2].min; + + my $sum = $val1 + $val2; + if $sum > $maxval { + $maxval = $sum; + } + } + say $maxval; + say " "; + $maxval = 0; +} + +#`{ +---------------------------------------- +SAMPLE OUTPUT +raku .\ArrayPairings.rk +Input: @array = [1 2 3 4] +4 + +Input: @array = [0 2 1 3] +2 +---------------------------------------- +} diff --git a/challenge-206/robert-dicicco/ruby/ch-1.rb b/challenge-206/robert-dicicco/ruby/ch-1.rb new file mode 100644 index 0000000000..827c375856 --- /dev/null +++ b/challenge-206/robert-dicicco/ruby/ch-1.rb @@ -0,0 +1,57 @@ +#!/usr/bin/env ruby +=begin +--------------------------------------- |
