PWC 181

PWC 181

Challenge 1 (Sentence Order)

This challenge asks us to take a given paragraph, and then:

  1. first, break it into sentences, and then
  2. sort the words and punctuation marks in each sentence into alphanumeric order
  3. print the sentences with the sorted word order

Since this task does not need anything from CPAN, I did it using only Perl 4 syntax. I've done this in some earlier challenges. To add a bit of further challenge, I stuck a  use strict on top. Also, I used one single hydra-headed variable to do everything. To be precise, I used just one symbol-table entry,  main::teststring, or in Perl 4 syntax, main'teststring.

Dynamic scope allows some strange things. For example, I locally redefine my &main'teststring subroutine inside the subroutine and then call the redefined subroutine from inside the original (a non-recursive call). A nested subroutine is not really needed here, but hey why not?

I can't be sure that my script would actually work on a Perl 4 interpreter, since I don't have one. My script actually ran on Perl 5.36.0.

Update and disclaimer: These programs will not work on Perl 4 as I discovered later when I found and installed a Perl 4.019 executable. For one thing, the ability to assign a subroutine to a locally scoped typeglob was not available in Perl 4.

It's nice that Perl 5 is so backward compatible that I can run such an archaic script on the latest Perl 5 version, and with use strict turned on to boot.

The gedit syntax highlighting was confused by the old-fashioned syntax, particularly the ' instead of ::. Github too. Atom too. Both emacs and vi were unfazed though.

My Raku script for this task is modern and straightforward. I'll let it speak for itself.

I did not write a Julia script for this challenge. Of course, Julia can do it, as  Robert DiCicco shows here

 
Next time there is a task that needs no external libraries, maybe I'll try the opposite extreme and do it with lexical variables only. Strictly interpreted, that means no public subroutines.

Challenge 2 (Hot day)

Challenge 2 provides a text file that lists dates in %Y-%m-%d format, and temperatures on each date. We are asked to read in the file and then print out the dates where the temperature was higher than on the preceding day.

In Perl 5, this is easy with the Date::Manip suite of packages. There are several other date packages, but Date::Manip is  a one-stop shop for anything date-related.  Its OO syntax looks verbose, but I find it easy and intuitive.

In Raku and Julia, this task is also easy with the standard date support, built-in in Raku, and via the Dates standard library module in Julia.

Julia is surprisingly quite a bit slower than both Perl 5 and Raku for this task (runtime is around 0.5 seconds for Perl 5, 1 second for Raku, and 1.3 seconds for Julia).




Comments

Popular posts from this blog

PWC 258

PWC 253

PWC 251