Posts

Showing posts from January, 2023

PWC 200

Image
PWC 200 This week again, due to work pressures, I have done the challenges only in Perl 5 and Raku, not in the guest language Julia. Challenge 1 (Arithmetic Slices) Given a list of integers, we are asked to find all slices of the list (sub-lists of consecutive elements) that have at least three elements, and where the difference between every consecutive element is the same. This is a one-liner in Raku . raku -e '(0 .. @*ARGS-1).combinations(2).grep({@_[1]-@_[0]>1}).map({@*ARGS[@_[0]..@_[1]]}).grep( {my @n=@_; (0 .. @n-3).map( {(@n[$_+1]-@n[$_]) == (@n[$_+2]-@n[$_+1])} ).sum==(@n.elems-2) } ).say' $@ The steps are: Get all slices by getting all combinations of two elements (bounds of the slice) Restrict to slices that are at least three elements long (difference between the slice bounds is greater than 1) Map the pairs of bounds to get slices from the list Use grep to check if the condition is true for each slice: For each slice, loop through the slice, checking that the dis...

PWC 199

PWC 199 Our good host has again given us an easy set of challenges, which works for me as I am busy at work. Again, I eschewed the guest language Julia this time, and have done the challenges only in Perl 5 and Raku. Challenge 1 (Good pairs) We are given a list of integers, and asked to count the number of pairs of items in the list say list[i] and list[j] where (i < j) and list[i]==list[j]. This is a one-liner in Raku using the combinations operator. raku -e '(0..@*ARGS-1).combinations(2).grep({@*ARGS[@_[0]]==@*ARGS[@_[1]]}).elems.say' $@ From the indices of the given list (command line argument), take all pairs of indices using the combinations operator, then use grep to extract the pairs of indices that satisfy the condition. The i < j condition is automatically taken care of by the combinations operator. I translated this to a Perl 5 one-liner using the combinations operator from Algorithm::Combinatorics. Similar logic to my Raku one-liner, but the steps are stacked ...

PWC 198

PWC 198 Our good host has given us easy challenges this week, which works for me as I am busy at work. Still, I have done them only in Perl 5 and Raku and skipped Julia this time. Challenge 2 (Prime Count) We are asked to find the number of prime integers between 0 and a given positive integer $n. The challenge specifies that we are looking for primes that are strictly less than $n (which I nearly missed, but caught in time). This is an easy one-liner if one does not try to implement a prime sieve from scratch (I don't). Here it is in Raku : raku -e ' (0..^@*ARGS[0]).grep(*.is-prime).elems.say ' $@ We need the ^ in ^@*ARGS[0] to exclude $n (the command-line argument) itself from the count.  In Perl 5 , I use the Math::Prime::Util function "prime_count".  perl -MMath::Prime::Util=prime_count -wl -e ' print prime_count($ARGV[0]-1) ' $@ We subtract one from the command-line argument to exclude it from the count. The "prime_count" routine gives th...