Posts

Showing posts from January, 2024

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