Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Re: "Countdown" (golf)

by chipmunk (Parson)
on Dec 01, 2001 at 11:01 UTC ( #128840=note: print w/replies, xml ) Need Help??

in reply to "Countdown" (golf)

I decided to write this as a one-liner that accepts the set of letters as the first argument.

Here's my best solution, at 86 characters: perl -ne'INIT{$l=shift}$r=$L=$l;$r&&=$L=~s/$_//for/./g;$w[$r&&y///c].=$_}{print$w[-1]'
That one assumes that there will be at least one match (otherwise it will print the entire word list), and it stores the entire word list in memory.

This next solution avoids both those problems, at the cost of one character. 87 characters: perl -ne'INIT{$l=shift}$r=$L=$l;$r&&=$L=~s/$_//for/./g;$w[y///c].=$_ if$r}{print$w[-1]'
Both solutions output all longest matches and work when the set of letters includes duplicates.

Example usage: perl -ne'INIT{$l=shift}$r=$L=$l;$r&&=$L=~s/$_//for/./g;$w[y///c].=$_ if$r}{print$w[-1]' dunscaeyz wordlist

Replies are listed 'Best First'.
Re: Re: "Countdown" (golf)
by blakem (Monsignor) on Dec 01, 2001 at 14:56 UTC
    If you reverse the order of the arguments you can use pop instead of shift to save two strokes....


    This attempt is about the same length, though a few chars shorter in the actual -e argument. BTW, yours was a very *evil* script... unbalanced brackets in the -e of a -n? I didn't even realize that was legal, let alone useful! ;-)

    perl -aF// -ne'INIT{$l=pop}$L=$l.$/;$L=~s/$_// or$#F=0 for@F;$w[@F].=" +@F"}{print$w[-1]'


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://128840]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2020-01-20 08:32 GMT
Find Nodes?
    Voting Booth?