PWC 182
PWC 182
Challenge 1 (max index)
Given a list of numbers, we are asked to give the index of the first occurrence of the biggest number in the list.
This is easy to do via one-liners in all three languages.
| |||||
It would be more compact with a trailing if as in say if (...), but I think that's just a cosmetic improvement. I'll let the uglier syntax above stand.
Julia has the convenient argmax function which returns the index of the maximum item.
julia -e 'println(argmax(ARGS)-1)' 5 2 9 1 7 6
My solutions differ slightly from the specification in that they return the indices of every occurrence of the biggest item, not just the first occurrence.
Challenge 2 (Common Path)
Given a list of Linux absolute file paths, we are asked to find the deepest common path.
In my last blog post, I said that I would do the next challenge in Perl 5 using lexical variables only, if possible. This is as a counterpoint to earlier challenges where I used Perl 4 syntax meaning only package variables.
Strictly using only lexical variables means all named subroutines must be lexical. Also arguments must be passed to subroutines using signatures, because @_ is not a lexical variable. I set 'use v5.36' to ensure that both signatures and lexical subs are available and non-experimental. (This probably violates the policy immediately, since I think it calls a non-lexical import function, but I'll ignore this. After all, I did stick a "use strict" on top of my "Perl-4-simulated" script last time, and "use strict" was definitely not available in Perl 4)
By the way, my Challenge 1 one-liner violates a strict "only lexical variables" policy in at least two ways. ARGV is not lexical, and there is the call to the external package-variable subroutine List::Util::max.
By contrast to last week's challenge, where I used Perl 4 syntax, my Perl 5 script for this challenge is hyper-modern (in syntax, not in style). It is closer to Raku in its syntax than to Perl 4.
But I do use the old-fashioned device of a flat hash that simulates an irregularly shaped multi-dimensional array. I find this easier than an array of arrays.
Also, I indulge a bad habit of over-using the same variable name for different things. This might dimly make sense with a symbol table, but not with lexical variables. Still, it seems harmless, and I enjoy the mantra-like effect that it creates in my code.
It was easy to port my hyper-modern Perl 5 solution to Raku, needing just minor edits. Raku forces me to correct the bad habit a bit as you may notice.
I did not do this challenge in Julia. It can be done of course, as Robert diCicco shows.
One can learn a lot from fellow-contributors. James Smith has a compact way to do this in Perl 5 using the bitwise xor operator on strings combined with a regex (his blog here). Certainly something useful to add to my toolkit.
Why bother to try Perl 4 syntax?
Our good reviewer Colin Crain has expressed bemusement on why anyone would want to look back to Perl 4 given the huge game-changing advantages that 5.0 brought (CPAN for starters). Let me attempt a rationale for why I sometimes bring on Perl 4 syntax in a challenge.
Many of my fellow-contributors use the challenge to try out experimental new features in the latest version, or to demonstrate how something like a functional idiom can be used. That is certainly a very useful way to handle a challenge.
But just for contrast, it might be interesting to answer a different and somewhat orthogonal question: how well can one meet this challenge using just a minimal core subset of the syntax? (Of course, as long as this does not require re-writing Math::Prime::Util or Date::Manip!). Perl 4 syntax is a good candidate for that minimal core subset in Perl 5. It is smaller than microperl. It does have that pesky lack of lexical variables or references, but it's a good question as to whether these are needed in a minimal subset.
Like Colin, I certainly wouldn't want to turn back the clock to pre-5.0. But sometimes it is just fun and interesting to play with some retro toys.
Comments