[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

RE: [FWP] words words words



> -----Original Message-----
> From: owner-fwp@technofile.org [mailto:owner-fwp@technofile.org]On
> Behalf Of Steven Alexander
> Sent: Friday, May 19, 2000 2:40 AM
> To: fwp@technofile.org
> Subject: [FWP] words words words
> 
> 
> What would be a fun (short | elegant | fast) way, given two files
> with nine- and three-letter words one per line, respectively, of
> listing only those nines which are the concatenation of three
> threes?
> 
> Anyone who wants to go so far as to do relative benchmarks
> should use the three- and nine-letter words from
> <http://www.geocities.com/TimesSquare/Castle/5057/TWL98.zip>
> 
> Here's the shortest I have (modulo whitespace):
> 
> #!/usr/local/bin/perl -nw
> BEGIN {
>     open T, "threes";
>     chomp (@t = <T>);
>     $re = join '|', @t;
> }
> /^(?:$re)*$/o && print;
> 
> and this is my fastest:
> 
> #!/usr/local/bin/perl -w
> open T, "threes"; chomp(@t = <T>);
> undef $/;
> open N, "nines" ;       $n = <N>;
> for(@t) {
>     $n =~ s/$_(?=(?:...)*\n)/\L$_/g;
> }
> $n =~ s/^[a-z]*[A-Z][A-Za-z]+\n//gm;
> print $n;

Here's my shot at it. I didn't benchmark, though. But, if you do, then
please let me know:

	open T, "threes" or die $!;
	chomp(my @three = <T>);
	my %three;
	@three{@three} = ();
	close T;

	open N, "nines" or die $!;
	while (<N>) {
	    exists $three{$1} && exists $three{$2} && exists $three{$3} &&
	        print if /(...)(...)(...)/;
	}

And here's a compact version of the same program:

	@ARGV="threes";
	chomp and$three{$_} = 1 for<>;
	@ARGV="nines";
	/(...)(...)(...)/&&exists$three{$1}&&
	                   exists$three{$2}&&
	                   exists$three{$3}&&
	                   print 
		for<>;

(which is actually just 4 lines long)

--Ala

==== Want to unsubscribe from Fun With Perl?  Well, if you insist...
==== Send email to <fwp-request@technofile.org> with message _body_
====   unsubscribe