in reply to Re: The evolution of a solution to the 11/23 NPR puzzler.
in thread The evolution of a solution to the 11/23 NPR puzzler.

The following solution only passes once through the dictionary, and doesn't need an additional pass through the hash. It also stores less in the hash. It takes advantage of the fact that the dictionary is sorted, and that a word of the form Xm sorts before Xrn.
#!/usr/bin/perl use strict; use warnings; my $file = "/usr/share/dict/words"; open my $fh => $file or die; my %seen; while (<$fh>) { chomp; if (/m$/) { $seen {+lc} ++; next; } elsif (/rn$/) { if ($seen {lc "$`m"}) { print "$`m/$_\n"; } next; } } __END__ bum/burn chum/churn dam/darn hom/horn modem/modern stem/stern tom/torn
Abigail

Replies are listed 'Best First'.
Re: The evolution of a solution to the 11/23 NPR puzzler.
by Coruscate (Sexton) on Jan 20, 2004 at 21:26 UTC

    Ingenious :). I'd been looking for a way to utilize the pre-sorting to my advantage but didn't come up with anything similar to this. ++Abigail-II.