Posts

Showing posts from September, 2022

PWC 184

PWC 184 Both the tasks for this week have narrowly defined input requirements. I don't write a function to handle them. I want to avoid input validation and error handling chores, and any self-respecting function needs both. I present throwaway scripts or one-liners with the inputs explicitly written into the script or ARGV.  Challenge 1 (Sequence number) We are given a list of strings, each one formatted as two lower-case alphabets (/[a-z]/) followed by 4  numeric characters (/[0-9/), e.g.,  ('ab1234', 'cd5678', 'ef1342'). We are asked to replace the starting alphabets with a two-digit string of numbers indicating their position in the list starting with zero, e.g., ('001234', '015678', '021342'). I do this with one-liners in Perl 5, Raku and Julia. Perl 5: perl -e ' my $ctr=0; for my $x ("00" .. "99") {print $x . substr($ARGV[$ctr++],2,4) . " "; last if $ctr >= @ARGV;} print "\n"; '

PWC 183

PWC 183 Challenge 1 (Unique array) Given an array of arrays, we are asked to remove any duplicate elements (arrays) and return the sub-array with only unique elements. Thus, given ([1,2],[3,4],[5,6],[1,2]), the script should return ([1,2],[3,4],[5,6]). Perl 5 In Perl 5, PDL offers an immediate solution via the uniqvec method. One just reads the list into a piddle: $list = pdl ([1,2],[3,4],[5,6],[1,2]); and then calls uniqvec on it: $list->uniqvec  The uniqvec method still works if the piddle has rows of unequal lengths, as in ([1,2],[3,4],[5,6,7],[1,2]). In this case, it pads the shorter rows with zeroes in the output: [     [1,2,0]     [3,4,0]     [5,6,7] ] I have presented my Perl 5 solution as a perldl executable command history. Here is my perldl script. Julia Julia has something similar to uniqvec in the unique function. println(unique(arry)) Just like PDL, it handles rows of unequal lengths. It does not pad the shorter rows with zeroes. println(unique([[1,2],[3,4],[5,6,7

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. Here is my Raku one-liner raku -e ' say (0 .. @*ARGS.elems-1).grep({@*ARGS[$_] == @*ARGS.max}) ' 5 2 9 1 7 6 Here is my Perl 5 one-liner perl -MList::Util= ' max ' -E ' for (0 .. $#ARGV) {if ($ARGV[$_]==max(@ARGV)) {say;}} ' 5 2 9 1 7 6         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. Here is my Julia one-liner 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

PWC 181

PWC 181 Challenge 1 (Sentence Order) This challenge asks us to take a given paragraph, and then: first, break it into sentences, and then sort the words and punctuation marks in each sentence into alphanumeric order 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 inter