Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
Not just a golf solution, its a golf solution generator:

Update: 'Borrowed' tilly's list of states. (my result is a 113 character string - though I too am now wondering where 'KT' is ... I'm told its a brand of whiskey, so I'll keep it in - besides, it is now the 'official' list for this challenge :-)

Another update: Added dws's fix.

#!/usr/local/bin/perl -l -w use strict; my @states=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 %states; @states{@states} = (); my @results; my $is_done; while (%states) { my ($letter, $is_first); ($letter, $is_first, $is_done) = get_next_letter(\%states); push(@results, keys %states), last if $is_done; my $pos = $is_first ? 0 : 1; my @states = grep { substr($_,$pos,1) eq $letter } keys %states; my $chr_class = '[' . join('', map { substr($_, 1-$pos, 1) } @states) . ']'; push @results, $is_first ? $letter.$chr_class : $chr_class.$letter; delete @states{@states}; } print '/^(',join("|", @results),')$/'; sub get_next_letter { my $states = shift; my (%first, %second, $is_done); for (keys %$states) { my ($first, $second) = split ''; $first{$first}++; $second{$second}++; } my ($first) = sort { $first{$b} <=> $first{$a} } keys %first; my ($second) = sort { $second{$b} <=> $second{$a} } keys %second; $is_done = 1 if $first{$first} == 1 and $second{$second} == 1; ($first{$first} > $second{$second}) ? ($first, 1, $is_done) : ($second, 0, $is_done); } # 113 characters! # Add 5 characters ('pop=~' to the beginning) if you want # it to work on @_ instead of $_ /^([WLPCVGIM]A|N[CDEHJMVY]|M[DEINOST]|[VKUC]T|A[ZKLR]|[WHR]I|O[HKR]|I[ +LND]|[SD]C|[KW]Y|T[XN]|KS|SD|DE|FL|WV|CO)$/

In reply to Re: Golf challange: match U.S. State names by runrig
in thread Golf challange: match U.S. State names by John M. Dlugosz

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others studying the Monastery: (10)
    As of 2015-07-07 16:53 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (92 votes), past polls