Perl Translation [was Re: [SPOILER] Medium QOTW 1 solution]
Shlomi Fish <shlomif-ik1l9ssToec+JF/[email protected]> Tue, 18 Jan 2005 11:19:45 +0200
| Newsgroups | gmane.comp.lang.perl.qotw.discuss |
|---|---|
| Message-ID | <[email protected]> |
On Monday 17 January 2005 12:20, Zsban Ambrus wrote:
> Here's my solution to perl Medium QOTW 1.
>
I was surprised to see such a short solution can still work for every N, but
it seems to be the case. (passes all the tests in my Test-Suite.t and also
works for every number up to 380 or so).
To test it using my test-suite, I translated it to Perl:
<<<
use strict;
use warnings;
sub allocate_schedule
{
my $n = shift;
my $m = $n - 1;
my @ret;
for my $d (0 .. ($m-1))
{
my @day = ();
for my $k (0 .. ($m-1))
{
if ($d == $k)
{
push @day, $m;
}
else
{
push @day, ((2*$d-$k) % $m);
}
}
push @day, $d;
push @ret, [ @day ];
}
return \@ret;
}
1;
>>>
One thing that delayed me was the fact that I translated the loops to
for my $d (0 .. $m)
Instead of
for my $d (0 .. ($m-1))
Apparently the (0 ... m) operator in Ruby does not evaluate "m" itself, as
opposed to Perl.
In any case, this solution is very simple and short and still works. I only
looked at it late last night, when I was too tired to think. Now, I'll try to
better understand its philosophy.
Well done!
Regards,
Shlomi Fish
---------------------------------------------------------------------
Shlomi Fish shlomif-ik1l9ssToec+JF/[email protected]
Homepage: http://www.shlomifish.org/
Knuth is not God! It took him two days to build the Roman Empire.