Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: "Countdown" (golf)

by blakem (Monsignor)
on Nov 30, 2001 at 12:59 UTC ( [id://128580]=note: print w/replies, xml ) Need Help??


in reply to "Countdown" (golf)

Here is my initial entry... I took a few liberties though:

1. Opened a words file outside the subroutine
2. Passed in letters as array not array ref or string (method wasn't specified above)
3. Case sensitive... I don't match 'Sunday' because I wasn't passed a capital S.
4. Returns a large set of matching words, w/o looking for the longest ones..... that wasn't the "interesting" part of the course for me, so I punted. ;-)

So, here's my first second fourth attempt at 64 chars:

my @letters = qw(d u n s c a e y t); open(D,"/usr/dict/words") or die $!; my @words = f(@letters); print "$_\n" for @words; sub f { # 1 2 3 4 5 6 #234567890123456789012345678901234567890123456789012345678901234 $;=join'?',sort@_,$;;grep{chop;(join'',sort split//)=~/^$;$/}<D> } __END__ =head1 SAMPLE OUTPUT ace aces acne act acted acute ad ads an and [SNIP] uneasy unsteady [SNIP]
Historical Incantations:
# 1 2 3 4 5 6 7 #234567890123456789012345678901234567890123456789012345678901234567890 +123456 $,=join'?',sort(@_),'';grep{chop;$;=join'',(sort(split//,$_));$;=~/^$, +$/}<D> $,=join'?',sort(@_),'';grep{chop;(join'',(sort(split//,$_)))=~/^$,$/}< +D> $,=join'?',sort(@_),'';grep{chop;(join'',sort split//)=~/^$,$/}<D> $;=join'?',sort@_,$;;grep{chop;(join'',sort split//)=~/^$;$/}<D> # with japhy's help... $;=join'?',sort@_;grep{chop;(join'',sort split//)=~/^$;?$/}<D> @_=sort@_;$"='?';grep{chop;(join'',sort split//)=~/^@_?$/}<D> # with dragonchild's help, and a 'perl -l' trick: @_=sort@_;$"='?';grep{(join'',sort split//)=~/^$\@_?$/}<D> # stealing a bit from gbarr @_=sort@_;$"='?';grep{(join'',sort/./g)=~/^@_?$/}<D>

-Blake

Replies are listed 'Best First'.
Re: Re: "Countdown" (golf)
by japhy (Canon) on Nov 30, 2001 at 14:19 UTC
    I hate to take credit for this 3-character reduction...
    $;=join'?',sort@_;grep{chop;(join'',sort split//)=~/^$;$/}<D>
    Why were you sorting $; as well?

    _____________________________________________________
    Jeff[japhy]Pinyan: Perl, regex, and perl hacker.
    s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

      The regex needs a '?' on the end... Its the difference between /^a?b?c$/ and /a?b?c?$/. Looking at it again, I only need to add one char to get it back though: 62 chars
      # 1 2 3 4 5 6 #2345678901234567890123456789012345678901234567890123456789012 $;=join'?',sort@_;grep{chop;(join'',sort split//)=~/^$;?$/}<D>
      Ah, but I can get it back with a little rearranging.... 61 chars
      # 1 2 3 4 5 6 #234567890123456789012345678901234567890123456789012345678901 @_=sort@_;$"='?';grep{chop;(join'',sort split//)=~/^@_?$/}<D>

      -Blake

      Why does neither of these return 'blade'?

      my @letters = qw(b a l d y a e y t);

      __PASTE__

      ~/perl_stuff> grep blade /usr/dict/words blade switchblade ~/perl_stuff> perl golf.pl aye baldy bay bayed beady bey by bye byte day delay dey dye lady lay lye y ye yea yet

      <a href="http://www.graq.co.uk">Graq</a>

        Check out my reply to japhy, and notice that all your results contain at least one 'y'.... Add the '?' to the end of the regex, and 'blade' will show up.

        -Blake

Re: Re: "Countdown" (golf)
by dragonchild (Archbishop) on Nov 30, 2001 at 18:35 UTC
    59 chars...
    # 1 2 3 4 5 6 7 #234567890123456789012345678901234567890123456789012345678901234567890 +123456 @_=sort@_;$"='?';grep{(join'',sort split//)=~m!^$/@_?$!}<D>

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

      I like it... though the words returned now have trailing newlines. I can move one char from the sub to the command line options, thus improving the score by 1 under normal golf rules. 58 chars
      #!perl -l # 1 2 3 4 5 #234567890123456789012345678901234567890123456789012345678 @_=sort@_;$"='?';grep{(join'',sort split//)=~/^$\@_?$/}<D>

      -Blake

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2024-04-25 05:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found