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. :)

--- 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

    For the second task, it looks to me like you want to choose ...

    the two largest numbers as the first pair and the next two largest as the second pair, and add the smaller of each: my @descending = sort { $b <=> $a } @input; say $descending[1] + $descending[3];

    Edited: I misread the task.

      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.