Posts

Showing posts from June, 2023

PWC 220

PWC 220 I switched to Raku this week. Both challenges are easier to tackle with Raku. Challenge 1 (Common characters) We are given a list of words, and asked to return a sorted list of the characters that are found in every word in the list (ignoring case). Here is the subroutine: sub common_characters( @words ) {     [(&)] @words.map( {$_.lc.comb.Set} ) } For each word in @words, lowercase it, then split into characters, then store each unique character in a set . Then working on the resulting list of sets, call the reduce meta-operator [..] on the set intersection "(&)" operator, i.e., call [(&)] or "reduce using intersection" on the list of sets, to get the intersection of every set, or the characters that are found in every set. This returns a set, whose say method automatically prints the elements in sorted order (one of the task specifications). Challenge 2 (Squareful) We are given a list of integers @ints say. We are asked to identify every permu

PWC 219

PWC 219 Challenge 1 (Sorted Squares) This is an easy challenge, though as often happens in these challenges, it is a warm-up exercise for a more difficult Challenge 2. We are given a list of numbers. We are asked to return a list of the numbers squared, sorted in increasing order. I implement this in PDL, submitting a pdl script. The key snippet is: sub sorted_squares {$list=pdl @_; ($list*$list)->qsort; } Convert the input list to a piddle. Square it and then call the qsort method on the squared list. Challenge 2 (Travel Expenditure) We are given two numeric lists. The first one, costs say, has three elements, corresponding to the per-card price of three types of travel cards: 1-day, 7-day, and 30-day. Presumably, purchasing one of the cards entitles you to travel without additional ticketing (on say a metro or bus system)  for the day of purchase, 7 days ahead from the day of purchase,  or 30 days ahead from the day of purchase, respectively for the 1-day, 7-day and 30-day cards.