develooper Front page | | Postings from July 2002

My solution annotated

Thread Next
Stephen Turner
July 8, 2002 02:38
My solution annotated
Message ID:
So, last month I annotated all the top 12 solutions. People seemed to
appreciate it, but there's no way I've got time to do it again this month!

So I thought it would be interesting if players annotated their own
solutions this month. I for one would like to see what people say about
their own solutions.

I volunteer to start. :) I actually annotated another solution of the same
length as my best one -- it has worse tie-breaker but is more readable.

#!perl -lan0
# Assuming that there are no NULs in the file, -n0 puts the whole file
# into $_ before running the program. Note that -0777 is almost never
# useful in golf, because -0 is almost always equivalent.
# -l will add a new line after every print.
# -a splits $_ into @F; so now @F contains the nodes (but with
# replicates).
# Run through the list of nodes as many times as the total number of
# nodes. (This many times is certainly sufficient; in fact it's
# generous).
# We have to escape non-alphanumerics in $a otherwise they mess up the
# regexps. My actual solution put \Q$a\E's in the regexps, but it's
# less readable that way, and doesn't save any characters.
  /^(?!$b ).* $b\n/m
# We want to find an earliest node to print. So look for lines finishing
# " $b", but which don't start "$b ". These stop $b being an earliest
# node at this point.
# (I wanted to save two strokes with /(?<!^$b) $b\n/ -- I don't know why
# it doesn't work.)
# If there are no such lines, then delete all occurrences of
# $b-whitespace. (?<!\S) checks that we haven't got a non-whitespace
# character just before $b, i.e., $b is a whole word not just the end of
# one.
# The effect of this line is to change "b x\n" to "x\n", "b\n" to "" and
# "b b\n" to "".
# Now if we couldn't find any $b's to delete, we've printed $b earlier.
# Otherwise print it (or actually its unescaped version $a) now.
# Once we've gone through the for loop enough times, there shouldn't be
# anything left in $_. If there is, some nodes were unprintable because
# the problem was inconsistent (contained a cycle); so die.

Stephen Turner, Cambridge, UK
"This is Henman's 8th Wimbledon, and he's only lost 7 matches." BBC, 2/Jul/01

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