Posts

Showing posts from January, 2023

Robbie Hatley's Perl Solutions To The Weekly Challenge #202

For those not familiar with "The Weekly Challenge", it is a weekly programming puzzle, usually with two parts, cycling every Sunday. You can find it here: The Weekly Challenge This week (2023-01-29 through 2023-02-04) is weekly challenge #202. Task 1 I found super easy: "You are given an array of integers. Write a script to print 1 if there are 3 consecutive odds in the given array; otherwise print 0." The sub I wrote to do that was just a few simple lines which I wrote in 5 minutes flat: sub tco (@a){ for (my $i = 0 ; $i <= $#a-2 ; ++$i){ if ( !($a[$i+0]%2) ) {$i += 0; next;} if ( !($a[$i+1]%2) ) {$i += 1; next;} if ( !($a[$i+2]%2) ) {$i += 2; next;} return 1;} return 0;} Task 2, however, I found much more challenging: "Given a profile as a list of altitudes, return the leftmost widest valley. A valley is defined as a subarray of the profile consisting of two parts: the first part is non-increasing and the second part

Robbie Hatley's Perl Solutions To The Weekly Challenge #201

For those not familiar with "The Weekly Challenge", it is a weekly programming puzzle, usually with two parts, cycling every Sunday. You can find it here: The Weekly Challenge This week (2023-01-22 through 2023-01-28) is weekly challenge #201. For a change, I was the "submitted by" person for challenge #2 (M. Anwar submitted #1). Challenge #1 is: "Given an array of unique integers, write a script to find out all missing integers in the range 0..$n where $n is the array size." This is simple, but quirky, because the results may not quite be what one expects. For example, given the sequence (14, 15, 18, 19) -- or, for that matter, (18, 14, 19, 15) -- one might expect the "missing numbers" to be (16, 17). But nope, they'll be (0, 1, 2, 3). A more-useful script might be to find all missing integers in the range $min..$max rather than 0..$size. So I wrote a script to do both, the main body of which looks like this: my $size = scalar @ARG

Robbie Hatley's Perl Solutions To The Weekly Challenge #200

For those not familiar with "The Weekly Challenge", it is a weekly programming puzzle, usually with two parts, cycling every Sunday. You can find it here: The Weekly Challenge This week (2023-01-15 through 2023-01-21) is weekly challenge #200. For a change, I found this week's challenge #1 to be much more challenging than challenge #2. Challenge #1 is: "Given an array of integers, write a script to find out all Arithmetic Slices". The problem is, these slices will come in all sizes, so a simple iterative (or nested-iterative) approach won't work. This stumped me for a long time, and I was about to resort to some complicated recursive method when it occurred to me that it's actually easy to find all subsets of a set: simply use all possible binary masks by just counting. So the function I came up with to find all arithmetic slices of an array looked like this: sub get_arith_slices($array_ref){ my @array = @{$array_ref}; my @slices = ();

Robbie Hatley's Perl Solutions to The Weekly Challenge #199

For those not familiar with "The Weekly Challenge", it is a weekly programming puzzle, usually with two parts, cycling every Sunday. You can find it here: The Weekly Challenge This week (2023-01-08 through 2023-01-14) is weekly challenge #199. I found both parts of this week's challenge quite easy. Part 1 is "Good Pairs" of list indices, where a "Good Pair" is an ordered pair of indices ($i, $j) such that $i<$j and $array[$i] == $array[$j]. This is easily done with a pair of nested 3-part for loops, like so: for (@arrays){ my @array = @{$_}; my @good; for ( my $i = 0 ; $i ≤ $#array - 1 ; ++$i ){ for ( my $j = $i + 1 ; $j ≤ $#array - 0 ; ++$j ){ if ( $array[$i] == $array[$j] ){ push @good, [$i, $j];}}} my $good_count = scalar(@good); say ''; say "list = (@array)"; say "Found $good_count \"Good Pairs\" of list indices:"; say "(@{$_})" f

Robbie Hatley's Perl Solutions to The Weekly Challenge #198

For those not familiar with "The Weekly Challenge", it is a weekly programming puzzle, usually with two parts, cycling every Sunday. You can find it here: The Weekly Challenge This week (2023-01-01 through 2023-01-07) is weekly challenge #198. For a change, part 1 was the more thought-provoking of the two puzzles this week: "Given a list of integers, write a script to find the total pairs of consecutive elements of the sorted list which have max gap. If the list contains less then 2 elements then return 0." Clearly the first step is to make a sorted version of the list; but how to proceed from there? I considered using a hash of gaps then using some funky key sorting; but then it occurred to me that no such thing was necessary, and that I only needed two scalar variables: one to record "max gap seen so far" and one to record "count of pairs with that gap". This works because each time a new "max gap" is found the old count becom