Posts

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

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

This is my first post to my first blog on a brand new blogger acct; this seems as good a place to start as any. 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 (2022-12-25 through 2022-12-31) is weekly challenge #197. Part 1 is a simple task of moving all zeros in an array to the end without disturbing the order of the non-zero elements. Of course this could be done by simply copying all non-zero elements to a second array, then padding the end of the output array with sufficient zeros. But that felt like cheating, so I took a completely different approach and spliced-out the zeros (using "splice") and tacked them on the end (also using "splice") so that I could edit the array in-situ with no need for multiple arrays, like so: for (@lists){ my @list = @{$_}; # Input. my $z;