in reply to Challenge: prefix($x, $y) . suffix($x, $z) eq $x
This seems too easy, which usually means I've misinterpreted the rules somewhere?
#! perl -sw use 5.010; use strict; use Data::Dump qw[ pp ]; my $start = time; open WORDS, '<', 'words.txt' or die $!; my %words; chomp, undef $words{ $_ } while <WORDS>; close WORDS; my $word = shift @ARGV or die "Need a word"; die "$word not in dictionary" unless exists $words{ $word }; my( %pre, %suf ); for ( keys %words ) { for my $p ( 1 .. length() -1 ) { push @{ $pre{ substr $_, 0, $p } }, $_ if substr( $_, 0, $p+1 ) ne substr( $word, 0, $p+1 ); push @{ $suf{ substr $_, - $p } }, $_ if substr( $_, -( $p+1 ) ) ne substr( $word, - ( $p+1 ) ); } } for my $p ( 1 .. length( $word ) - 1 ) { my( $pre, $suf ) = ( substr( $word, 0, $p ), substr( $word, - ( length( $word ) - $p ) ) ); if( exists $pre{ $pre } and exists $suf{ $suf } ) { my( $nPre, $nSuf ) = ( scalar @{ $pre{ $pre } }, scalar @{ $suf{ $suf } } ); say "$pre . $suf = $word"; printf "\t$pre ( %s )\n", $nPre <= 10 ? join( ' ', @{ $pre{ $pre } } ) : join( ' ', @{ $pre{ $pre } }[ 0 .. 9 ], '...', $nPre ); printf "\t$suf ( %s )\n", $nSuf <= 10 ? join( ' ', @{ $suf{ $suf } } ) : join( ' ', @{ $suf{ $suf } }[ 0 .. 9 ], '...', $nSuf ); } } printf "Took: %.2f seconds\n", time() -$start; __END__ [21:21:15.55] c:\test>759369 hello h . ello = hello h ( householder hulled hipster hops hikings honorary hacienda +hob hunts harebrained ... 2352 ) ello ( cello violoncello jello bordello ) he . llo = hello he ( heartier hesitation hearings heaver heftinesses heartache +s hectic hepatitides hemisphere heeds ... 550 ) llo ( cigarillo peccadillo hallo hullo armadillo ) hel . lo = hello hel ( helium helpfully helve helpless heliocentric helm held h +elicoptering helps helping ... 46 ) lo ( gigolo polo tangelo buffalo halo tremolo pueblo silo solo + kilo ... 11 ) hell . o = hello hell ( hellion hellhole hellebore hellish hellions hellholes h +ellebores helluva hells hellishly ... 15 ) o ( dido no legato magneto canto stereo wino commando cockatoo + bongo ... 386 ) Took: 3.00 seconds [21:21:50.32] c:\test>759369 today to . day = today to ( tonsils tormentor toiled towellings tortellinis totterer +tost topographies tonal touring ... 531 ) day ( doomsday someday holiday payday workday heyday weekday b +irthday noonday everyday ... 14 ) tod . ay = today tod ( toddy toddies toddled toddling toddlers toddle toddles t +oddler ) ay ( underway swordplay defray overpay driveway shay hooray ru +naway unsay stairway ... 165 ) Took: 2.00 seconds [21:22:16.70] c:\test>759369 tumultuous tumult . uous = tumultuous tumult ( tumults ) uous ( vacuous assiduous ambiguous unambiguous tenuous conspic +uous incongruous strenuous promiscuous innocuous ... 26 ) Took: 2.00 seconds [21:22:22.57] c:\test>759369 freedom fre . edom = freedom fre ( frequented frescoes freaked fretfulness frequent freckli +ng freighters freakishness freshets freshening ... 75 ) edom ( boredom princedom dukedom ) free . dom = freedom free ( freezing freeholds freezes freer freethinking freeware +freewill freebee freebooter freelancers ... 60 ) dom ( fiefdom random stardom officialdom martyrdom chiefdom se +ldom earldom kingdom sheikdom ... 18 ) freed . om = freedom freed ( freedman freedmen ) om ( backroom unbosom darkroom legroom doom gloom venom custom + phenom maelstrom ... 90 ) Took: 2.00 seconds
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Challenge: prefix($x, $y) . suffix($x, $z) eq $x
by Limbic~Region (Chancellor) on Apr 22, 2009 at 22:11 UTC | |
by BrowserUk (Patriarch) on Apr 23, 2009 at 00:44 UTC |