Posts

Showing posts from May, 2023

PWC 218

PWC 218 Challenge 1 (Maximum Product) We are given a list of integers, which can include negative integers and zero as well as positive integers. We are asked to find the product of any three elements of the list which is the highest of any product so formed. It is stipulated that the list must have at least three elements. To solve, I sort the list in numeric order. Then the maximum product is given by one of the following: The product of the lowest two elements and the maximum element. This is if the lowest two elements are both negative (so that their product is positive). The product of the highest three elements.  Here is the core subroutine: use List::Util qw(max); local *maximum_product = sub {          local *myprod=sub {         $_[0]*$_[1]*$_[2];     };     #-- main part of &maximum_product     my @list = sort {$a <=> $b} @_;          (scalar(@list) < 2) && (die "Need at least 3 elements in input");     max(         &myprod( $list[0], $list[

PWC 217

PWC 217 Challenge 1 (Sorted Matrix) We are given a n by n matrix of numbers, and asked to report the third-smallest number in the matrix.  The specification seems to allow arbitrary non-numeric elements too, but since all the test examples are numeric, I will restrict to numeric matrices. PDL is of course the go-to tool for this. The core subroutine: sub sorted_matrix {my ($pdl)=@_; $pdl=$pdl->flat->qsort; $pdl(2);}   The flat method flattens the matrix piddle to a vector piddle, qsort sorts the vector in ascending order, then we just return the third element.   Challenge 2 (Max Number) We are given a list of positive integers of arbitrary lengths, and asked to concatenate them so as to produce the largest possible concatenated number. Thus, given 1 and 23, we would produce 231, not 123.  To solve this, we exploit the fact that Perl can treat the elements interchangeably as numbers or as strings. We first sort the numbers using a routine that puts $b before $a if $b.$a is great

PWC 215

PWC 215 I have attempted the Perl Weekly Challenge after a hiatus of several weeks, as I was too busy at work. Still busy, so I have just attempted the current challenge in Perl 5. Like I like to do, I have restricted myself to perl 4 syntax as specified in the first edition of Programming perl. 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. Challenge 1 (Odd one out) In this challenge, we are given a list of alphabetic strings, and asked to return the list without the strings in which the characters are not sorted (I assume that this means sorted in lexical ascending order). We are also asked to print the number of strings that were removed (i.e., that were not sorted).  I use a subroutine &odd_one_out(@words) with a nested helper sub-sub-routine to check if a string is sorted. Here i