develooper Front page | | Postings from July 2002

Re: My solution annotated

Thread Previous | Thread Next
Eugene van der Pijll
July 8, 2002 09:36
Re: My solution annotated
Message ID:
En op 08 juli 2002 sprak Ala Qumsieh:
> Along the same lines, here's my solution:

And although I'm not a top golfer...

-lan0 sub f{$p{$_}||=1/!$g{$_}+++map(&f,$x=~/^(.*) (?!\1
/mg)+print}$x=$_;f for@F

was my best solution, which cost me a whole night of sleep. But I just
couldn't let `/anick and /-\ndrew beat me.

#!perl -lan0

sub f{

# The routine f() is doing all the work. For each node, f prints first
# the preceding nodes (recursively) and then the node itself. The node
# is not passed as an argument to this routine, but as $_.


# If $p{$_} is false, this node has not been printed yet, and the right
# hand side of the expression will be evaluated. If this node has
# already been printed, we skip it, because of the short-circuiting ||.


# $g{$_} can only be set if f is called for a node twice, before it is
# printed (because $p{$_} is set when f() exits). This is only possible
# when the node precedes itself, in which case we die with an "Illegal
# division by zero" error.

+map(&f,$x=~/^(.*) (?!\1

# For each node that precedes $_, call f(). The (?!\1\n) is there to
# handle the isolated nodes, "a a".


# After the preceding nodes have been printed, print this node. $p{$_}
# is set to the value of the three terms above, which is at least 2.


# As we use $_ to pass the nodes to f(), we need to save the original
# input to $x.

f for@F

# Call f() for all nodes. Many nodes will be called more than once, but
# that's no problem; we check for that in f().

Ala Qumsieh wrote:
> $_&&&
> ## This little monster is well-known to the more
> ## experienced golfers. I believe it was proposed
> ## by (-ugene.

Piers Cawley, actually.

This course was very interesting (though I didn't like it at first,
stuck at about 180), because of the many different algorithms used, but
also the number of different ways of die'ing. Here are some of the error
messages my solutions returned when fed a cyclic graph:

Undefined subroutine &main::x called
Illegal division by zero
Can't take log of 0

And my favorite:

Not a subroutine reference

Were there more possibilities?


The competent programmer is fully aware of the strictly limited size of
his own skull; therefore he approaches the programming task in full
humility, and among other things he avoids clever tricks like the
plague. -- (-dsger |)ijkstra

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About