Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: (Golf) Anagram Finder

by Masem (Monsignor)
on Aug 04, 2001 at 10:10 UTC ( [id://102216]=note: print w/replies, xml ) Need Help??


in reply to (Golf) Anagram Finder

59 Characters:
grep{(join'',sort split//,$_[0])eq join'',sort/./g}@{$_[1]}

-----------------------------------------------------
Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain

Replies are listed 'Best First'.
Re: Re: (Golf) Anagram Finder
by tachyon (Chancellor) on Aug 04, 2001 at 16:09 UTC

    I like this method but why do you think this works (also 59):

    @dict = qw (foo oof fff bar baz); $letters = 'ofo'; print anagram($letters, \@dict); sub anagram { grep{(join'',sort split//,$_[0])eq join'',sort/./g}@{pop()} }

    But this does not

    grep{(join'',sort split//,$_[0])eq join'',sort/./g}@{pop}

    Why do you need the parens to make pop() work?

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

      @{pop} doesn't work as you intended, because it refers to the plain old array @pop. The parentheses in @{pop()} make it into an expression, which is evaluated.

      However, you could save a character by using a plus instead, as in @{+pop}.

      Update: But, if you wanted to save even more characters... :)

      sub anagram { grep"@{[sort$_[0]=~/./g]}"eq"@{[sort/./g]}",@{+pop} }
      51 characters.
        The @{[func]} method for inserting the result of an arbitrary function call into a string is amazing. Someone once asked if they could put subroutine calls into a double-quoted string and have them expand properly, but I don't think anyone gave a satisfactory answer.

        I had no idea that the double-quoted string parser was so robust.
        If you lift the requirement that you have to compare one word to many, you can get the following (which returns whether its two arguments are anagramic)
        sub anagram { @_=map{join"",sort/./g}@_;pop eq pop }
        36 characters.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (9)
As of 2024-04-19 07:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found