diff options
| author | Aaron Sherman <ajs@ajs.com> | 2019-06-24 09:32:14 -0400 |
|---|---|---|
| committer | Aaron Sherman <ajs@ajs.com> | 2019-06-24 09:32:14 -0400 |
| commit | d1ccfc3697669b3a1d419b7a8ac4c73947787463 (patch) | |
| tree | 8413382db4f276b6da79e902c2944fce1728f9ed | |
| parent | 070f2dfb2c6a88b25b0d8745806fa96c106bfe7e (diff) | |
| download | perlweeklychallenge-club-d1ccfc3697669b3a1d419b7a8ac4c73947787463.tar.gz perlweeklychallenge-club-d1ccfc3697669b3a1d419b7a8ac4c73947787463.tar.bz2 perlweeklychallenge-club-d1ccfc3697669b3a1d419b7a8ac4c73947787463.zip | |
Much more flexibility and docs for US Words
| -rw-r--r-- | challenge-014/aaron-sherman/README.md | 15 | ||||
| -rwxr-xr-x | challenge-014/aaron-sherman/us-words.p6 | 88 |
2 files changed, 82 insertions, 21 deletions
diff --git a/challenge-014/aaron-sherman/README.md b/challenge-014/aaron-sherman/README.md index d042f35b39..03516ee2e1 100644 --- a/challenge-014/aaron-sherman/README.md +++ b/challenge-014/aaron-sherman/README.md @@ -6,3 +6,18 @@ This week's first puzzle happens to be one that I just finished adding to [`Math::Sequences::Integer`](https://github.com/ajs/perl6-Math-Sequences) so my solution is an underwhelming reference to and plug for that module. [Here is the specific definition](https://github.com/ajs/perl6-Math-Sequences/blob/cb8d9f58092718f85debf68d2f183288741bdfad/lib/Math/Sequences/Numberphile.pm#L196). + +## Challenge #2: State Words + +My solution is pretty simple. It uses Perl 6's powerful regex engine +to match the state abbreviations against a wordlist. A short example +wordlist is provided, but it can also use a web version that is VERY +large and then the solution takes much longer (feels like a performance +bug to me). + +### Examples + + $ ./us-words.p6 + decade + $ ./us-words.p6 --verbose + Delaware + California + Delaware = decade diff --git a/challenge-014/aaron-sherman/us-words.p6 b/challenge-014/aaron-sherman/us-words.p6 index 38456b212d..1506d335fd 100755 --- a/challenge-014/aaron-sherman/us-words.p6 +++ b/challenge-014/aaron-sherman/us-words.p6 @@ -6,6 +6,9 @@ # Here is the list of U.S. states abbreviations as per wikipedia page. # This challenge was proposed by team member Neil Bowers. +# My solution is really just the one line: +# say @wordlist.grep(/:i^@states+$/).max(*.chars); + use v6.c; use WWW; @@ -13,30 +16,73 @@ use WWW; our $online-wordlist = 'https://raw.githubusercontent.com/dwyl/english-words/master/words_alpha.txt'; our $local-wordlist = '/usr/share/dict/words'; -sub get-words() { - sub get-local-words($path) { - $path.IO.e ?? $path.IO.lines>>.trim !! Nil - } - get-local-words($local-wordlist) // get-local-words('words') // - get($online-wordlist).words; +sub get-words(Bool :$web-words) { + sub get-local-words($path) { + $path.IO.e ?? $path.IO.lines>>.trim !! Nil + } + return get($online-wordlist).words if $web-words; + get-local-words($local-wordlist) // get-local-words('words') // + get($online-wordlist).words; } -sub get-states(:$territories) { - my $states = < - AL AK AZ AR CA CO CT DE FL GA HI ID IL IN IA KS KY LA ME MD MA MI MN MS - MO MT NE NV NH NJ NM NY NC ND OH OK OR PA RI SC SD TN TX UT VT VA WA WV - WI WY>; - my $others = <DC AS GU MP PR VI FM MH PW AA AE AP CM CZ NB PI TT>; - |$states, |($territories ?? $others !! ()); +sub get-states-map() { + my @states = < + AL AK AZ AR CA CO CT DE FL GA HI ID IL IN IA KS KY LA ME MD MA MI MN MS + MO MT NE NV NH NJ NM NY NC ND OH OK OR PA RI SC SD TN TX UT VT VA WA WV + WI WY>; + my @others = <DC AS GU MP PR VI FM MH PW AA AE AP CM CZ NB PI TT>; + my @names = |<Alabama Alaska Arizona Arkansas California Colorado + Connecticut Delaware Florida Georgia Hawaii + Idaho Illinois Indiana Iowa Kansas Kentucky Louisiana Maine Maryland + Massachusetts Michigan Minnesota Mississippi Missouri Montana + Nebraska Nevada New Hampshire New Jersey New Mexico New York North + Carolina North Dakota Ohio Oklahoma Oregon Pennsylvania>, + |['Rhode Island'], + |<South Carolina South Dakota Tennessee Texas Utah Vermont Virginia + Washington West Virginia Wisconsin Wyoming>; + my @others-names = + "District of Columbia", "American Samoa", "Guam", + "Northern Mariana Islands", "Puerto Rico", "U.S. Virgin Islands", + "Micronesia", "Marshall Islands", "Palau", + "U.S. Armed Forces – Americas", "U.S. Armed Forces – Europe", + "U.S. Armed Forces – Pacific", "Northern Mariana Islands", + "Panama Canal Zone", "Nebraska", "Philippine Islands", + "Trust Territory of the Pacific Islands"; + return { + states => Hash.new(@states Z @names), + others => Hash.new(@others Z @others-names), + }; } proto MAIN(|) {*} multi MAIN(Bool :$test!) { - use Test; - my @states = get-states; - cmp-ok 'code', '~~', /:i^@states$/, "Pattern match a word"; + use Test; + my $states-map = get-states-map; + my @states = $states-map<states>.keys; + like 'code', /:i^@states+$/, "Pattern match a word"; + is $states-map<states><CA>, 'California', 'CA lookup'; +} +multi MAIN( + Bool :$territories, #= Include US Territories + Bool :$verbose, #= Verbose output + Bool :$web-words #= Force downloading large wordlist +) { + my @wordlist = get-words(:$web-words).grep({.defined}); + my $states-map = get-states-map; + my @states = $states-map<states>.keys; + if $territories { + @states.push($states-map<others>.keys); + } + @states .= sort; + my $answer = @wordlist.grep(/:i^@states+$/).max(*.chars); + if not $answer { + say "Could not find a match"; + } + if $verbose { + my @parts = $answer ~~ m:g/(\w\w)/; + say "{@parts>>.map({ + $states-map<states>{uc $_}//$states-map<others>{uc $_} + }).join(' + ')} = $answer"; + } else { + say $answer; + } } -multi MAIN(Bool :$territories) { - my @wordlist = get-words.grep({.defined}); - my @states = get-states(:$territories); - say @wordlist.grep(/:i^@states+$/).max(*.chars); -}
\ No newline at end of file |
