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, sum of ints[i]*ints[i], where our summation is restricted to elements where n%i=0, or where n is divisible by i. The array index starts from 1 for this challenge, not from 0.

This could be done in modern Perl via a compact map or grep statement, but with the older version, I stick to a loop. Here is the key subroutine.

sub special_numbers {
    local (@ints)=@_;
    local ($n) = scalar(@ints);
    local ($i);
    local ($retval) = 0;
    foreach $i (1 .. $n) {
    ($n % $i) || ($retval += ($ints[$i-1] * $ints[$i-1]) );
    }
    $retval;
}

In deference to modern safe practices, I declare all my variables using the best available scope restriction via the "local" keyword. In contrast, the examples in Programming perl 1e casually use global variables almost everywhere (though they do localize temporary variables in subroutines like I did here). 

Here is my python 1.4b code, also using a loop. Python of course enforces something like lexical scope by default.

def special_numbers( ints ):
 retval=0
 n=len(ints)
 for i in range(n):
   if ((n % (i+1)) == 0):
     retval = retval + (ints[i]*ints[i])
 return retval

Challenge 2 (Unique sum zero)

Solutions: perl4.019 python1.4b

We are given a positive integer n and asked to return a list of length n, consisting of unique integers (no duplicates), such that the sum of elements is zero.

One way to do this is to add pairs of elements like -1 and 1, -2 and 2 to the output list, until we have reached the right length. If n is odd, we start the output list with 0, and then add the pairs if needed.

Here is my Perl 4 code:

sub unique_sum_zero {
 local($n)=@_;
 local(@retval)=();
 ($n % 2) && push( @retval, 0);
 local ($i);
 for $i (1 .. ($n/2)) {
  push( @retval,(-$i, $i) );
 }
 @retval;
}

Here is my Python 1.4b code:

def unique_sum_zero(n):
 retval=[]
 if ((n % 2) > 0):
   retval.append(0)
 for i in range(n/2):
   retval.append(-(i+1))
   retval.append(i+1)
 return retval

 

Comments

Popular posts from this blog

PWC 258

PWC 253

PWC 251