Posts

Showing posts from February, 2024

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: raku -e 'say +@*ARGS.grep({$_.chars %% 2})' $@ 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.  sub count_even_digits {  scalar( grep( ((length($_) % 2) == 0), @_) ); } The full code is here . Challenge 2 (Sum of values) We are given an integer k  an...

PWC 257

PWC 257 I finished late this week, so I just submitted Challenge 1. But I finished Challenge 2 later without submitting it to github and will just post the entire code on this blog. I did both challenges in perl 4.019 on DOSBOX. For challenge 1, I also did it in python 1.4 beta on DOSBOX. Challenge 2 I only attempted in Perl. My solutions work in current versions of perl 5 (5.38) and python 2, though with warnings of the deprecated perl 4 package separator when I run challenge 2 in perl 5.  Challenge 1 (Smaller than current) We are given a list of integers ints say. We are asked to return a list of numbers giving at each index i the count of how many elements in ints are less than the element at ints[i]. Here is the perl 4 subroutine [ link to github ]: sub smaller_than_current {  local(@ints)=@_;  local(@ranks)=(sort {$ints[$a]<=>$ints[$b];} 0..$#ints);  local($i,@retval);    $retval[$ranks[0]]=0;  foreach $i (1 .. $#ranks){   if ($ints[$...

PWC 256

PWC 256 I do both challenges in perl 4.019 on DOSBOX. For comparison, I also do them in python 1.4 beta on DOSBOX. My perl and python solutions to both challenges also run fine on the current versions of Perl 5 (5.8) and Python 2 (2.7.18). Challenge 1 (Maximum Pairs) Links: PERL4.019 PYTHON1.4b We are given a list of strings. We are asked to count how many pairs of the form ( string, reverse(string)) we can form from the elements of the list. In perl 4, I use a single pass through the list that does the following: For each element, check if I have seen the reverse of that element before. (I use a hash called %seen to track this). If I have seen the reverse before: Count one more pair of conformant elements (I increment a counter called $count .) Reset the hash entry for the reverse of the element to be 0 or false (to prevent for example ('ab','ba','ba') from being counted as two pairs instead of one). If I have not seen the reverse before, I just update %see...

PWC 255

PWC 255 I solve both challenges using perl 4.019 on DOSBOX. For comparison, I also solve them using python 1.4 beta on DOSBOX. The Perl solutions also run fine on Perl 5.38. My Python solution to challenge 1 is also forward compatible with python 2.7.18, but my python solution to challenge 2 does not work in python 2. Challenge 1 (Odd Character) Links to code: perl4.019 python1.4b We are given two strings, s  and t  say, where t  is a jumbled version of s  plus one extra character. We are asked to find the extra character. I eschew input validation chores, so I assume that the inputs satisfy the requirements. My approach is to use hashes to count the incidence of each character in s  and t , then we compare the hashes to find the element of t  whose frequency is higher than that of the same character in s .  Here is the perl 4.019 subroutine: sub odd_character {  local($s,$t)=@_;  # ...  local(%s,%t,$i);  foreach $i (split(//,$s)) ...