Posts

Showing posts from December, 2022

PWC 197

PWC 197 I did not attempt the challenges in Julia this time. I have solutions in Perl 5 and Raku. Challenge 1 (Move Zero) We are given a list of integers, say (3,0,1,0,5). We are asked to sort the list so that all zeros move to the end, and the remaining items are in their original relative order. In my example, the sorted list would be (3,1,5,0,0). This is a one-liner in Perl 5 and Raku. Perl 5: perl -wl -e ' my @o = sort { ($a == 0) ? 1 : ( ($b == 0) ? -1 : 0 ) } @ARGV; print "@o" ' $@ Raku: raku -e ' say @*ARGS.sort( { ($^a == 0) ?? 1 !! ( ($^b == 0) ?? -1 !! 0 ) } ) ' $@ The logic is via the block argument to sort. This takes two parameters $a and $b representing two elements in the list being compared. If the comparison returns -1, then $a precedes $b in the sorted list. If the comparison returns 1, then $b precedes $a in the sorted list. If the comparison returns 0, then the order doesn't matter. The sort pattern here follows this logic: If both ...

PWC 196

PWC 196 Aside on Advent of Code I have done the Advent of Code challenges in Perl 5 up to #14 so far. Here are my solutions. I probably should stop there at least for now, since I have a lot of work despite the holidays. But let's see, #15 looks intriguing, and I'm sure the later ones are too. Back to discussing our Weekly Challenges for this week. This week, I submitted only Perl 5 and Raku solutions, not Julia. Challenge 1 (Pattern 132) We are given a list of at least three integers. We are required to extract one sub-list of three integers that satisfies the following restrictions: If i,j,k respectively are the indices in the original list of the extracted items, then i < j < k. If a[i], a[j], a[k] respectively are the items in the sub-list in order, then we have: a[k] > a[i], and a[j] > a[k] (similar to the ordering in the list 1,3,2 whence the name). This is a one-liner in Raku, using the combinations operator. I did not directly implement restriction 1 above...

PWC 195

PWC 195 Challenge 1 (Special integer) We are given a positive integer n. We are asked to find the number of positive integers between 1 to n inclusive of both ends, i.e., 1 .. $n in Perl- or Raku-speak, such that no digit is repeated in the integer. For example, 11 or 1313 are not okay, but 12 or 1302 are. After my over-engineered challenge 2 solution last week, brute force seems very appealing, and that is how I go with both challenges this week. I should point out though that in this case, brute force is strictly dominated by a standard analytical formula. We can calculate the number of permutations of the digits 0 .. 9 taken 1 at a time, 2 at a time, 3 at a time, etc. with adjustments to handle the upper bound n. Fellow-contributor Luis Mochan has spelled out the formula in his blog . In Raku, the brute force solution is a short and sweet one-liner: raku -e ' (1 .. @*ARGS[0]).grep({$_ == $_.comb.unique.join}).elems.say ' $@ I tried to translate this to an equivalent Perl 5 ...

PWC 194

PWC 194 Aside on Advent of Code I also discovered Advent of Code and got started on it. Not sure if I will continue, because the daily cycle may be too much for me. I thought initially of doing it using Raku, but looking at the challenges, immediately decided to go with Perl 5. The tasks so far all involve reading an input file with some data in an arbitrary text-based format, then doing some computations with that data, and finally spitting out some output which one submits to Advent of Code for validation. This is exactly what Perl was designed to do. Raku of course is a development from the same source, but it's a bit less focused on the "practical extraction and reporting" stuff. Here is the repo with my solutions. And now, back to our regular scheduled weekly challenge. Challenge 1 (Digital Clock) We are given an input string which is a 24-hour clock time, in the format 'hh:mm'. For example, 23:59. From the examples, 24:00 is not allowed. One number is missi...