Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Golf challange: match U.S. State names

by dws (Chancellor)
on Jun 06, 2001 at 09:39 UTC ( #86132=note: print w/replies, xml ) Need Help??


in reply to Golf challange: match U.S. State names

A hybrid approach, for 110 109 (including the honorary state KT).
print /^\w\w\z/&("MNCAKSCOHINMOKTNVALARIDCTX SDE GAZ ORIL"=~/$_/| /(FL|IA|KY|M[ADEIST]|N[DEHNJY]|PA|[UV]T|W[AIVY])/) for qw(AK AL AR AZ CA CO CT DC DE FL GA HI IA ID IL IN KS KT KY LA MA MD ME MI MN MO MS MT NC ND NE NH NJ NM NV NY OH OK OR PA RI SC SD TN TX UT VA VT WA WI WV WY)
Getting here involved finding the longest unbroken string that could hold overlapping state names, adding a few more with breaks (spaces), then handling the strays with a regexp. A close inspection will show that Rhode Island is handled twice. That actually saved strokes.

I started down the path of looking for the shortest string that contains all of the state names (with breaks). That script is still running... So far, the solution above is shorter.

Replies are listed 'Best First'.
Re: Re: Golf challange: match U.S. State names
by dws (Chancellor) on Jun 06, 2001 at 22:13 UTC
    Now 108
    /^\w\w\z/&("MNCAKSCOHINMOKTNVALARIDCTX SDE GAZ ORIL IAKY"=~/$_/| /(FL|M[ADEIST]|N[DEHNJY]|PA|[UV]T|W[AIVY])/)
    Another stoke off by adding a redundant check for Alaska, which allowed me to move Iowa and Kentucky out of the regexp and into the (more compact) string.

    This suggests that there's a lesson in here about how a bit of redundancy in data can lead to more compact representations. I think we often hear problems like "match every state and nothing else" as "match every state exactly once, and nothing else."

Re: Re: Golf challenge: match U.S. State names
by petral (Curate) on Jun 06, 2001 at 16:21 UTC
    Or 105:
    print /^..\z/&("MNCAKSCOHINMOKTNVALARIDCTX SDE GAZ ORIL"=~/$_/| /FL|IA|KY|M[ADEIST]|N[DEHJY]|PA|[UV]T|W[AIVY]/) for qw(AK AL AR AZ CA CO CT DC DE FL GA HI IA ID IL IN KS KT KY LA MA MD ME MI MN MO MS MT NC ND NE NH NJ NM NV NY OH OK OR PA RI SC SD TN TX UT VA VT WA WI WV WY MY MNC .* ), " SD", "AZ\n"
    (Dropping the inner parens around the or-chain and disregarding the state of NN which is not being honored today.)

    OOPSDATE: Yep, matches if $_='.*' (see runriq immediately below and slightly to the right).  It needs /\Q$_/, as so often happens, which defeats the character savings.  I was looking at the \w as avoiding matching the spaces (which it also does).    (Oh, and thanks runriq and I'm sorry too!)
      p

      Sorry, but your expression makes '.*' an honorary state :-)

      That's why dws uses '\w\w' instead of '..' in his answer.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://86132]
help
Chatterbox?
[perldigious]: Hmm, thanks Lotus1... perldigious scribbles note to buy lots of opossums as "pets" in the future to hang around outside his property. :-P
[1nickt]: You must twist and pull! If you burn, use tea tree oil etc, the tick barfs (inside your blood vessel) and you get all the loevely bacteria in his gut transferred to yours.
[1nickt]: tweesers work just fine
[hippo]: Hope everyone else is enjoying their lunch too.
[1nickt]: However, note that it takes between 2 and 4 hours for a tick to be able to start transmitting bacteria. So even if it has burrowed in and started eating, if you get it soon you should be OK.
[perldigious]: 1nickt: No more red meat! It would be more humane to just kill you, wouldn't it! :-)
[1nickt]: Just don't make it barf.
[1nickt]: perldigious that's exactly what my stepson says.
[1nickt]: the tick that can make you a vegetarian
[perldigious]: Your Mother: Yeah, I have a tool for ticks that I use (mostly on my dogs thankfully), but it's just a cheapo one I got from my vets office... it works pretty good though.

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (14)
As of 2017-05-24 13:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?