PWC 258
PWC 258
Last week (#257)
I finished challenge 2 and my blog post for challenge 257 late last week, so had only submitted challenge 1. Here is my blog post for PWC 257, including the full code for my challenge 2 solution.
This week, I have done both challenges in perl 4.019 on DOSBOX. Both scripts also work fine on the latest Perl 5.38. I also did the challenges in Raku, where they are one-liners.
Challenge 1 (Count Even Digits Number)
We are given a list of integers, and asked to count the number of elements whose base-10 representation has an even number of digits.
In Raku, this is a one-liner. Here it is:
It could be a one-liner in Perl 4 too, but since I run it on DOSBOX, my script would not be a bash shell script. Here is the short subroutine that does the job.
The full code is here.
Challenge 2 (Sum of values)
We are given an integer k and a list ints of integers. We are asked to first find the indices of ints whose base-2 representation contains exactly k instances of the digit 1. We then return the sum of the elements of ints at those indices.
In Raku, this is a one-liner. Here it is:
We find the base-2 representation of each index, split it, and then sum the elements, which gives the number of 1's.
In perl 4, I have a limited solution, which only works for lists of 10 or less elements. For the numbers 0 to 9, we can use the vec(.) function to get a length-4 vector of the digits in its binary representation (backwards, but that doesn't matter for our purpose).
Here is a helper subroutine to calculate the sum of binary digits of a given string (should be a number from 0 to 9 to work correctly, but I don't bother with input validation)
Here is the primary subroutine. It calls &binsum within a loop, which my "Programming perl 1e" warns against, but the inefficiency shouldn't matter here.
Comments