LanX has asked for the wisdom of the Perl Monks concerning the following question:

Hi

I stumbled over some blog posts choosing brute-forc'ish algos to solve the perl-weekly-challenge-206.

Here my take on it ... (I hid the "spoilers")

I hope my approaches are clever solutions, please correct me otherwise.

But I'm not too fond of my approach with List::Util::reduce() for task1. It's a hack to process successive pairs instead of even pairs.( 1-2 than 2-3 vs 3-4 )

Is there a more elegant way to do it with a function from List::Util ?

BTW: Generating the tests from the POD is nice too. :)

use v5.12.0; use warnings; #use Data::Dump; use List::Util qw/reduce min pairs sum/; use Test::More; my @tasks1 = pairs map { m/^\s+(?:Input:.*=|Output:) (.*)$/ } split /\n/, <<'=cut'; =pod 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 tw +o 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 mi +nutes). 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 =cut sub solve1 { my @times = @_; my $min = 24*60; # max start my @minutes = sort { $a <=> $b } map { ($a,$b) = split /:/; $a*60+ +$b } @times; push @minutes, $minutes[0] + $min; # wrap reduce { $min = min( $min, $b-$a); $b } @minutes; return $min; } say "--- Task 1"; is( solve1( eval($_->[0])) => $_->[1] => join " -> ", @$_ ) for @tasks1; my @tasks2 = pairs map { m/^\s+(?:Input:.*=|Output:) (.*)$/ } split /\n/, <<'=cut'; =pod 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. =cut sub solve2 { my @array = @_; return sum map {$_->[0]} pairs sort { $a <=> $b } @array; } say "--- Task 2"; is( solve2( eval($_->[0])) => $_->[1] => join " -> ", @$_ ) for @tasks2; done_testing;

--- Task 1 ok 1 - ("00:00", "23:55", "20:00") -> 5 ok 2 - ("01:01", "00:50", "00:57") -> 4 ok 3 - ("10:10", "09:30", "09:00", "09:55") -> 15 --- Task 2 ok 4 - (1,2,3,4) -> 4 ok 5 - (0,2,1,3) -> 2 1..5

Cheers Rolf
(addicted to the 𐍀𐌴𐍂𐌻 Programming Language :)
Wikisyntax for the Monastery

Replies are listed 'Best First'.
Re: Perl Weekly Challenge 206 with List::Util
by Anonymous Monk on Mar 17, 2023 at 18:56 UTC
Re: Perl Weekly Challenge 206 -- oneliner
by Discipulus (Canon) on Mar 18, 2023 at 09:55 UTC
    Just for fun and with many doubts on my own working solution, a nice oneliner for the first challange on time differences

    perl -le "@v=sort{push@r,abs$a-$b;$a-$b}map{($h,$m)=split':',$_;$M=60* +$h+$m;($M,$M+1440,$M+2880)}@ARGV;print+(sort{$a<=>$b}@r)[0]" @ARGV = 00:00 23:55 20:00 => 5 @ARGV = 01:01 00:50 00:57 => 4 @ARGV = 10:10 09:30 09:00 09:55 => 15

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re: Perl Weekly Challenge 206 -- oneliner
by Discipulus (Canon) on Mar 20, 2023 at 08:56 UTC
    ..as always for my own fun, the second task is easy using a hash with expected values as keys and values incremented for expected args $ARGV[0]..$ARGV[-1] and for actual args @ARGV so a key with a value of 1 is a missing number and a key with a value of 3 is a duplicate. If nothing of this happens return -1

    perl -E "map{$h{$_}++}$ARGV[0]..$ARGV[-1],@ARGV;map{$h{$_}==3?$r.=' Du +plicate is '.$_:$h{$_}==1?$r.=' Missing is '.$_:''}keys%h;say $r||-1"

    L*

    UPDATE ..oops! yes LanX is right I jumped to 208, sorry.

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
      that's an answer to challenge 206 task 2 ?

      I provided a test-suite in my OP, not sure how best to generalize it to fit for one-liner too. (probably by shelling out?)

      update

      did you skip to 208?

      Cheers Rolf
      (addicted to the 𐍀𐌴𐍂𐌻 Programming Language :)
      Wikisyntax for the Monastery

Re: Perl Weekly Challenge 206 with List::Util
by hv (Prior) on Mar 17, 2023 at 17:57 UTC
      Are you saying descending is better than ascending? 🤷🏻‍♂️

      The task guaranties even numbered lists, and it doesn't matter in which direction I sum up.

      Tho I could have avoided sum map ... pairs with a for loop and a

      • $sum += $_ if ++$i % 2

      inside, hence avoiding the dependency on List::Util

      Cheers Rolf
      (addicted to the 𐍀𐌴𐍂𐌻 Programming Language :)
      Wikisyntax for the Monastery

        My apologies, I misread the question: somehow I got it into my head that we wanted the sum of at most two numbers.