Posts

Showing posts from 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  and a list in

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[$ranks[$i]]==$ints[$ranks[$i-1]]) {    $

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)) {   $s{$i}++;  }  foreach $i (split(//,$t)) {   $t{$i}

PWC 254

PWC 254 I do the challenges this week in perl 4.019 on DOSBOX, and for comparison, also in python 1.4 beta on DOSBOX. My perl solutions also run on the current Perl 5.38, In the python case, my challenge 1 solution runs on Python 2.7.18, but my challenge 2 solution does not. Challenge 1 (Three Power) Links to code: Perl4.019 Python1.4b We are given an integer, and asked to verify if it represents the cube of another integer. The solution is to take the cube root and compare it to its integer portion. Here is the key perl 4 snippet: sub three_power {  local( $n )=@_;  $n=($n**(1/3));  $n==sprintf("%d",$n); } Here is the python 1.4 beta equivalent: def three_power(n):  n=n**(1.0/3.0)  return (n==int(n)) Challenge 2 (Reverse Vowels) Links to code: Perl4.019 Python1.4b We are given a string of alphabets. We are asked to identify which of the elements are vowels, and then to return a string in which the order of the vowel elements is reversed. For example, if 'a' is at

PWC 253

PWC 253 I do both challenges this week using old software on DOSBOX: perl 4.019, and for comparison, also python 1.4 beta. My solutions also work on the current versions of Perl 5 (5.38) and Python 2 (2.7.18) respectively. Challenge 1 (Split Strings) Links to code: perl4.019 python1.4b We are given a list of strings, and a single character say sep . We are asked to return the list, with the strings split into component substrings using sep as a separator. We should not include any empty strings in the output. In perl 4, I first stick a backslash in front of the separator character, so that it is escaped if entered in a regex. This is because the perl split function takes a regex as its first argument indicating the separator. If we have a character like '$' or ',', we want to escape it so that it is read literally when parsing the regex. Here is the key subroutine:   sub split_strings {  local ($separator, @words)=@_;  local (@retval)=(); #array to be returned  #  #--

PWC 252

PWC 252 I have attempted both challenges this week using antique software on DOSBOX. I used perl 4.019, and as a comparison, also did the challenges in python using python 1.4 beta for DOS. Both my solutions also run on current versions of the relevant software: Perl 5  (5.38), and Python 2 (2.7.18) respectively. Of course, the python code will not run on Python 3, and the Perl code will break with 'use strict'. Perl 4 is the version referenced in the seminal first edition of "Programming perl", while Python 1.4b is the version referenced in "Internet programming with python", co-authored by Guido, which first introduced python to the world. I like to refer to both books even in 2024, and doing the challenges in these old versions is a good excuse to do so.  Challenge 1 (Special Numbers) Solution: perl4.019 python1.4b   We are given an array of integers of length n , say ints . We are asked to return the sum of selected squared elements of ints , that is, s

PWC 251

PWC 251 This time I tried both challenges using antique software on DOSBOX: Perl 4.019 for DOS, and for comparison Python 1.4 beta for DOS. My solutions also run on the current versions of Perl 5 (5.38) and Python 2 (2.7.18). Hooray for backward compatibility! Challenge 1 (Concatenation Value) Links to solution: Perl Python We are given a list of positive integers, say ints . We are asked to follow the following procedure: Concatenate ints[0] and ints[-1] (last element). Store the result in retval say. Concatenate ints[1] and ints[-2] (second-last element), and add the result to retval . Concatenate ints[2] and ints[-3] .... etc. until we have reached the middle of the list. Return retval . Here is the Perl 4.019 subroutine (it also works fine on the latest Perl 5.38). sub concatenation_value {   local(@ints)=@_;   local($x,$retval)=(0,0);   foreach $x (0 .. @ints/2-1) {    $retval += $ints[$x] . $ints[$#ints-$x];   }   (@ints % 2) && ($retval += $ints[$#ints/2]);   return $r