in reply to Knight's Tour Problem in Perl

Some of your lines are more than one line. The python crowed would likely try to stab your eyes out. At least that's my experience. ... perhaps the only good way to compare program size is by bytes? Also, I'm basically certain that perl golf will not impress python coders. In fact, people like merylin have argued that golf doesn't impress would be perl coders either. ;)

Still, I value this kind of competition with the python coders. TIMTOWTDI doesn't count there.

UPDATE: I suppose I wasn't very clear. Many of the one line things in that perl program would most definitely be more than one line in python ... or the python people would get mad and point out that there's only one way to do it. Any way you look at it, perl's meanings can go much farther to the right (if you're in the mood to do so), so a byte count is still better.

-Paul

Replies are listed 'Best First'.
Re^2: Knight's Tour Problem in Perl
by GrandFather (Saint) on Dec 01, 2008 at 02:38 UTC

    Actually hardly any of the OP's lines were more than one line. However, processing the OP code through Perl tidy and with a little hand tidying (including adding strictures and some blank lines) the code is still only 62 lines long:

    use strict; use warnings; print "\nUse: SIZE START_X START_Y [0,1]" and exit if @ARGV < 4; my ($N, $X0, $Y0, $VERBOSE) = @ARGV; my $MOVES = 0; my @moves = ([-2, -1], [-1, -2], [-2, 1], [-1, 2], [2, -1], [1, -2], [ +2, 1], [1, 2]); my @board; my $last_move = [$X0, $Y0] and inform_board (); $last_move = inform_board ($last_move) while $MOVES < $N * $N; sub init { return ($_[0] > $N / 2) ? init ($N - 1 - $_[0], $_[1]) : ( $_[1] > $N / 2 ? init ($_[0], $N - 1 - $_[1]) : ( $_[0] >= 2 && $_[1] >= 2 ? 8 : ( ($_[0] >= 2 && $_[1] == 1 || $_[1] >= 2 && $_[0] == +1) ? 6 : ($_[0] + $_[1] > 1 ? 4 : ($_[0] + $_[1] == 1 ? 3 : 2 +))) ) ); } sub inRange { my ($pos, $D) = @_; my ($x, $y) = ($pos->[0] + $D->[0], $pos->[1] + $D->[1]); return ($x >= 0 && $x < $N && $y >= 0 && $y < $N && $board[$x][$y] + >= 0) ? [$x, $y] : 0; } sub inform_board { my ($moved_to) = @_; if ($moved_to) { $MOVES++; print "\n$MOVES.$moved_to->[0], $moved_to->[1]" if $VERBOSE; my ($MIN, $next_to_move) = (10, []); foreach (@moves) { if (my $finalPos = inRange ($moved_to, $_)) { my $value_ref = \$board[$finalPos->[0]][$finalPos->[1] +]; ($MIN, $next_to_move, $board[$moved_to->[0]][$moved_to +->[1]]) = ($$value_ref, $finalPos, 0) if (--$$value_ref >= 0 && $$value_ref < $MIN); } } return $next_to_move; } else { push @board, [(0) x $N] for (1 .. $N); for my $i (0 .. $N - 1) { $board[$i][$_] = init ($i, $_) for 0 .. $N - 1; } } }

    Perl's payment curve coincides with its learning curve.
Re^2: Knight's Tour Problem in Perl
by ptoulis (Scribe) on Dec 01, 2008 at 04:47 UTC
    I can't see which statement should span to more lines. Perhaps the most obscure is the init() function, but this is called once to initialize the board based on hard-wired if-else and so there is no point in making it more 'verbal'. There are also some (a,b,c...)=(func1(), func2 ,func3(),...) statements which I find very handy if done carefully.

    I am not much familiar with Python but I can't see how the equivalent Python code is more readable than this. Even Peter Norvig "former-Lisp-legend", now "Googley-Python-Evangelist", seems to admit there is no real improvement in program readability

    Anyway, I will agree this 'competition' is just for fun (or to keep our teeth sharp!)

      I can't see which statement should span to more lines

      Seeing as you have

      foreach(@moves) { if(my $finalPos = inRange($moved_to, $_))
      and
      for my $i (0..$N-1)  { $board[$i][$_] = init($i,$_) for (0..$N-1);},
      I'd say you do.

      Since all "more lines" statements in Perl are conventions, you could just as well mash all into a one long line and claim yourself the winner. Regardless of anything, counting SLOC in Python have an actual meaning, since the syntax is forcing itself on your style. In Perl, every GNU-style coding is just a K&R away from 30% "improvement".

      "A core tenant of the greater Perl philosophy is to trust that the developer knows enough to solve the problem" - Jay Shirley, A case for Catalyst.