diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2023-03-04 12:05:02 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-03-04 12:05:02 +0000 |
| commit | e1779b58b69af9f64e1c8d3c21ae01c8367143c4 (patch) | |
| tree | c135bf210d7d7b494a1d27d0600592c0bb645414 | |
| parent | 2492a97b5e15f22b3ad04a2bf0c29730fc4ab4a6 (diff) | |
| parent | efd14311becc29f4f7c672a23d27c4e065bb189d (diff) | |
| download | perlweeklychallenge-club-e1779b58b69af9f64e1c8d3c21ae01c8367143c4.tar.gz perlweeklychallenge-club-e1779b58b69af9f64e1c8d3c21ae01c8367143c4.tar.bz2 perlweeklychallenge-club-e1779b58b69af9f64e1c8d3c21ae01c8367143c4.zip | |
Merge pull request #7658 from 0rir/206
206
| -rw-r--r-- | challenge-206/0rir/raku/ch-1.raku | 79 | ||||
| -rw-r--r-- | challenge-206/0rir/raku/ch-2.raku | 71 |
2 files changed, 150 insertions, 0 deletions
diff --git a/challenge-206/0rir/raku/ch-1.raku b/challenge-206/0rir/raku/ch-1.raku new file mode 100644 index 0000000000..d6e641f339 --- /dev/null +++ b/challenge-206/0rir/raku/ch-1.raku @@ -0,0 +1,79 @@ +#!/usr/bin/env raku +# :vim ft=raku sw=4 expandtab # 🦋 ∅ ≡ ∩ ≢ ∈ «  » ∴ +use v6.d; +use Test; + +=begin comment + +206 1: Shortest Time Submitted by: Mohammad S Anwar +Given a list of time points, at least 2, in the 24-hour clock format HH:MM, +find 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 + +=end comment + +=begin dispute + +"00:00" would be midnight which is the start of a day. To assume that times +are in differend days is nonsensical unless there is knowledge of the dataset +not given here. A commonplace klugie solution is provided here; the ':night' +flag shifts the after noon values by 24 hours so midnight is centered in the range of possible values. + +=end dispute + + +enum Test-idx <IN EXP NIGHT>; +my @Test = + # IN EXP NIGHT + [ ("00:00", "23:55", "20:00"), 235, 5], + [ ("00:15", "23:55", "00:35"), 20, 20], + [ ("00:15", "23:55", "00:36"), 21, 20], + [ ("00:15", "11:55", "23:15"), 680, 60], + [ ("00:15", "23:55", "00:35"), 20, 20], + [ ("01:01", "00:50", "00:57"), 4, 4], + [ ("10:10", "09:30", "09:00", "09:55"), 15, 15], + [ ("12:00", "00:00"), 720, 720], + [ ("09:00", "17:00", "00:00"), 480, 420], + [ ("12:00", "12:00"), 0, 0], + [ ("22:00", "01:00"), 1260, 180], +; + +sub smallest-time-span( @t, Bool :$night = False --> Int) { + + my $offset = $night ?? -24*60*60 !! 0; + + constant midday = 24*60*60÷2; + + return + map( { .[1] - .[0] }, + @t.map({ + die "bad food \"$_\"" unless /^ \d \d ':' \d \d $/; + my $t = .substr(0,2).Int×60×60 + .substr(3,2).Int×60; + $t = $t > midday ?? $t + $offset !! $t; + }).sort.rotor( 2 => -1) + ).min.Int div 60 +} +say smallest-time-span( @Test[2][0]); + +plan 2 × @Test; + +for @Test -> ( @in, $exp, $night) { + is smallest-time-span( @in), $exp, "$exp\t<- @in[]"; + is smallest-time-span( @in, :night), $night, "$night\t<- @in[] :night"; +} +done-testing; + +my @time =("00:00", "23:55", "20:00"); +say "\nInput: @time = @time.raku()\nOutput: &smallest-time-span( @time)"; + diff --git a/challenge-206/0rir/raku/ch-2.raku b/challenge-206/0rir/raku/ch-2.raku new file mode 100644 index 0000000000..6da080f585 --- /dev/null +++ b/challenge-206/0rir/raku/ch-2.raku @@ -0,0 +1,71 @@ +#!/usr/bin/env raku +# :vim ft=raku sw=4 expandtab # 🦋 ∅ ≡ ∩ ≢ ∈ «  » ∴ +use v6.d; +use Test; + +=begin comment + +206-2: Array Pairings Submitted by: Mohammad S Anwar + +Given an array of integers having even number of elements, 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 maximum 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. +=end comment + +my @Test = + ( [,].Array, 0), + ( Empty.Array, 0), + ( Array, 0), + ([1,2], 1), + ([0,1,3,2], 2), + ([2,3,4,1], 4), + ([1,4,1,4], 5), + ([4,1,4,4], 5), + ([4,4,4,4], 8), + ([1,2,3,4,5,6], 9), + ([3,4,5,6,9,10], 17), + ([1,4,5,6,9,10], 15), + ([1,4,6,10,10,10], 17), + + ([1,2,3,4,5,6,7,8], 16), + ([1,8,9,10,11,12,33,14,15,16], 51), +; + +multi sub max-sum-of-mins-for-dyads( @a where @a.elems %% 2 --> Int) { + # sort and take very other one + sum @a.sort( { $^a <=> $^b })[ (0..@a.end).grep(* %% 2)]; +} +multi sub max-sum-of-mins-for-dyads( @a where not *.defined --> Int) { 0 } +multi sub max-sum-of-mins-for-dyads( @a where *.elems == 0 --> Int) { 0 } + +plan +@Test; + +for @Test -> ( @in, $exp) { + is max-sum-of-mins-for-dyads( @in), $exp, "@in.raku() => $exp"; +} + +done-testing; + +my @ary = 1,8,9,10,11,12,33,14,15,16; +say "\nInput: @array = @ary.raku()\nOutput: &max-sum-of-mins-for-dyads( @ary)"; + |
