diff options
| -rw-r--r-- | challenge-014/paulo-custodio/Makefile | 2 | ||||
| -rw-r--r-- | challenge-014/paulo-custodio/perl/ch-2.pl | 59 | ||||
| -rw-r--r-- | challenge-014/paulo-custodio/python/ch-1.py | 40 | ||||
| -rw-r--r-- | challenge-014/paulo-custodio/python/ch-2.py | 119 | ||||
| -rw-r--r-- | challenge-014/paulo-custodio/t/test-1.yaml | 5 | ||||
| -rw-r--r-- | challenge-014/paulo-custodio/t/test-2.yaml | 20 | ||||
| -rw-r--r-- | challenge-014/paulo-custodio/test.pl | 49 |
7 files changed, 241 insertions, 53 deletions
diff --git a/challenge-014/paulo-custodio/Makefile b/challenge-014/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-014/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-014/paulo-custodio/perl/ch-2.pl b/challenge-014/paulo-custodio/perl/ch-2.pl index b02097809c..6549b608a1 100644 --- a/challenge-014/paulo-custodio/perl/ch-2.pl +++ b/challenge-014/paulo-custodio/perl/ch-2.pl @@ -15,10 +15,61 @@ # California + Louisiana + Massachusetts + Rhode Island = Calamari use Modern::Perl; -use Locale::US; -my $u = Locale::US->new; -my @codes = $u->all_state_codes; # all states two letter codes +my %us_states = ( + AL => 'Alabama', + AK => 'Alaska', + AZ => 'Arizona', + AR => 'Arkansas', + CA => 'California', + CO => 'Colorado', + CT => 'Connecticut', + DE => 'Delaware', + FL => 'Florida', + GA => 'Georgia', + HI => 'Hawaii', + ID => 'Idaho', + IL => 'Illinois', + IN => 'Indiana', + IA => 'Iowa', + KS => 'Kansas', + KY => 'Kentucky', + LA => 'Louisiana', + ME => 'Maine', + MD => 'Maryland', + MA => 'Massachusetts', + MI => 'Michigan', + MN => 'Minnesota', + MS => 'Mississippi', + MO => 'Missouri', + MT => 'Montana', + NE => 'Nebraska', + NV => 'Nevada', + NH => 'New Hampshire', + NJ => 'New Jersey', + NM => 'New Mexico', + NY => 'New York', + NC => 'North Carolina', + ND => 'North Dakota', + OH => 'Ohio', + OK => 'Oklahoma', + OR => 'Oregon', + PA => 'Pennsylvania', + RI => 'Rhode Island', + SC => 'South Carolina', + SD => 'South Dakota', + TN => 'Tennessee', + TX => 'Texas', + UT => 'Utah', + VT => 'Vermont', + VA => 'Virginia', + WA => 'Washington', + WV => 'West Virginia', + WI => 'Wisconsin', + WY => 'Wyoming', +); + +my @codes = keys %us_states; # all states two letter codes my $codes = join("|", @codes); # regex to match any codes my $regex = qr/^($codes)+$/i; # regex to match word composed of codes @@ -38,6 +89,6 @@ while (<$fh>) { # show longest words in form: word = state + state + ... for my $word (@longest) { - my @states = map {$_ = $u->{code2state}{uc($_)}} grep {$_} split /(..)/, $word; + my @states = map {$_ = $us_states{uc($_)}} grep {$_} split /(..)/, $word; say $word, " = ", join(" + ", @states); } diff --git a/challenge-014/paulo-custodio/python/ch-1.py b/challenge-014/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..6852909991 --- /dev/null +++ b/challenge-014/paulo-custodio/python/ch-1.py @@ -0,0 +1,40 @@ +#!/usr/bin/python3 + +# Challenge 014 +# +# Challenge #1 +# Write a script to generate Van Eck's sequence starts with 0. For more +# information, please check out wikipedia page. This challenge was proposed by +# team member Andrezgz. + +def van_eck_iter(): + hist = [] + # first two terms + hist.append(0) + yield hist[-1] + + hist.append(0) + yield hist[-1] + + while True: + found = False + for m in range(len(hist)-2, -1, -1): + if hist[m]==hist[-1]: + hist.append(len(hist)-1-m) + yield hist[-1] + found = True + break + if not found: + hist.append(0) + yield hist[-1] + +sep = "" +output = "" +count = 0 +for n in van_eck_iter(): + output += sep + str(n) + sep = ", " + count += 1 + if count > 96: + break +print(output) diff --git a/challenge-014/paulo-custodio/python/ch-2.py b/challenge-014/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..fd1890993e --- /dev/null +++ b/challenge-014/paulo-custodio/python/ch-2.py @@ -0,0 +1,119 @@ +#!/usr/bin/python3 + +# Challenge 014 +# +# Challenge #2 +# Using only the official postal (2-letter) abbreviations for the 50 U.S. +# states, write a script to find the longest English word you can spell? Here +# is the list of U.S. states abbreviations as per wikipedia page. This challenge +# was proposed by team member Neil Bowers. +# +# For example, +# Pennsylvania + Connecticut = PACT +# Wisconsin + North Dakota = WIND +# Maine + Alabama = MEAL +# California + Louisiana + Massachusetts + Rhode Island = Calamari + +import re +import sys + +us_states = { + 'AL':'Alabama', + 'AK':'Alaska', + 'AZ':'Arizona', + 'AR':'Arkansas', + 'CA':'California', + 'CO':'Colorado', + 'CT':'Connecticut', + 'DE':'Delaware', + 'FL':'Florida', + 'GA':'Georgia', + 'HI':'Hawaii', + 'ID':'Idaho', + 'IL':'Illinois', + 'IN':'Indiana', + 'IA':'Iowa', + 'KS':'Kansas', + 'KY':'Kentucky', + 'LA':'Louisiana', + 'ME':'Maine', + 'MD':'Maryland', + 'MA':'Massachusetts', + 'MI':'Michigan', + 'MN':'Minnesota', + 'MS':'Mississippi', + 'MO':'Missouri', + 'MT':'Montana', + 'NE':'Nebraska', + 'NV':'Nevada', + 'NH':'New Hampshire', + 'NJ':'New Jersey', + 'NM':'New Mexico', + 'NY':'New York', + 'NC':'North Carolina', + 'ND':'North Dakota', + 'OH':'Ohio', + 'OK':'Oklahoma', + 'OR':'Oregon', + 'PA':'Pennsylvania', + 'RI':'Rhode Island', + 'SC':'South Carolina', + 'SD':'South Dakota', + 'TN':'Tennessee', + 'TX':'Texas', + 'UT':'Utah', + 'VT':'Vermont', + 'VA':'Virginia', + 'WA':'Washington', + 'WV':'West Virginia', + 'WI':'Wisconsin', + 'WY':'Wyoming', +} + +def read_file(filename): + with open(filename) as f: + return f.readlines() + +def read_words(lines): + words = [] + for line in lines: + word = line.strip() + if not re.search(r"\W", word): + words.append(word) + return words + +def word_regexp(): + state_abbr = [] + for state in us_states: + state_abbr.append(state) + return r"(?i:^("+"|".join(state_abbr)+r")+$)" + +def words_like_states(words): + matched = [] + regexp = word_regexp() + for word in words: + if re.match(regexp, word): + matched.append(word) + return matched + +def longest_words(words): + longest = [] + size = 0 + for word in words: + if len(word) > size: + longest = [word] + size = len(word) + elif len(word) == size: + longest.append(word) + return longest + +def word_to_states(word): + states = [] + for i in range(0, len(word), 2): + cc = word[i:i+2].upper() + states.append(us_states[cc]) + return " + ".join(states) + +words = longest_words(words_like_states(read_words(read_file("words.txt")))) +for word in words: + print(word+" = "+word_to_states(word)) diff --git a/challenge-014/paulo-custodio/t/test-1.yaml b/challenge-014/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..ebf698340d --- /dev/null +++ b/challenge-014/paulo-custodio/t/test-1.yaml @@ -0,0 +1,5 @@ +- setup: + cleanup: + args: 2019 + input: + output: 0, 0, 1, 0, 2, 0, 2, 2, 1, 6, 0, 5, 0, 2, 6, 5, 4, 0, 5, 3, 0, 3, 2, 9, 0, 4, 9, 3, 6, 14, 0, 6, 3, 5, 15, 0, 5, 3, 5, 2, 17, 0, 6, 11, 0, 3, 8, 0, 3, 3, 1, 42, 0, 5, 15, 20, 0, 4, 32, 0, 3, 11, 18, 0, 4, 7, 0, 3, 7, 3, 2, 31, 0, 6, 31, 3, 6, 3, 2, 8, 33, 0, 9, 56, 0, 3, 8, 7, 19, 0, 5, 37, 0, 3, 8, 8, 1 diff --git a/challenge-014/paulo-custodio/t/test-2.yaml b/challenge-014/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..c89ea9f41a --- /dev/null +++ b/challenge-014/paulo-custodio/t/test-2.yaml @@ -0,0 +1,20 @@ +- setup: 0==system("aspell -d en dump master | aspell -l en expand > words.txt") + cleanup: unlink("words.txt") + args: + input: + output: | + armorial = Arkansas + Missouri + Rhode Island + Alabama + calamine = California + Louisiana + Michigan + Nebraska + coalmine = Colorado + Alabama + Michigan + Nebraska + calamari = California + Louisiana + Massachusetts + Rhode Island + Concorde = Colorado + North Carolina + Oregon + Delaware + Ganymede = Georgia + New York + Maine + Delaware + landmine = Louisiana + North Dakota + Michigan + Nebraska + melamine = Maine + Louisiana + Michigan + Nebraska + moorland = Missouri + Oregon + Louisiana + North Dakota + malarial = Massachusetts + Louisiana + Rhode Island + Alabama + memorial = Maine + Missouri + Rhode Island + Alabama + mainland = Massachusetts + Indiana + Louisiana + North Dakota + Mandarin = Massachusetts + North Dakota + Arkansas + Indiana + mandarin = Massachusetts + North Dakota + Arkansas + Indiana + mescalin = Maine + South Carolina + Alabama + Indiana diff --git a/challenge-014/paulo-custodio/test.pl b/challenge-014/paulo-custodio/test.pl deleted file mode 100644 index 0a5cd72e18..0000000000 --- a/challenge-014/paulo-custodio/test.pl +++ /dev/null @@ -1,49 +0,0 @@ -#!/usr/bin/perl - -use Modern::Perl; -use Test::More; - -# build list of words for testing -ok 0==system("aspell -d en dump master | aspell -l en expand > words.txt"); - - -is capture("perl perl/ch-1.pl 2019"), <<END; -0, 0, 1, 0, 2, 0, 2, 2, 1, 6, 0, 5, 0, 2, 6, 5, 4, 0, 5, 3, 0, 3, 2, 9, 0, 4, 9, 3, 6, 14, 0, 6, 3, 5, 15, 0, 5, 3, 5, 2, 17, 0, 6, 11, 0, 3, 8, 0, 3, 3, 1, 42, 0, 5, 15, 20, 0, 4, 32, 0, 3, 11, 18, 0, 4, 7, 0, 3, 7, 3, 2, 31, 0, 6, 31, 3, 6, 3, 2, 8, 33, 0, 9, 56, 0, 3, 8, 7, 19, 0, 5, 37, 0, 3, 8, 8, 1 -END - -is capture("perl perl/ch-2.pl"), <<END; -armorial = ARKANSAS + MISSOURI + RHODE ISLAND + ALABAMA -inguinal = INDIANA + GUAM + INDIANA + ALABAMA -calamine = CALIFORNIA + LOUISIANA + MICHIGAN + NEBRASKA -coalmine = COLORADO + ALABAMA + MICHIGAN + NEBRASKA -calamari = CALIFORNIA + LOUISIANA + MASSACHUSETTS + RHODE ISLAND -complain = COLORADO + NORTHERN MARIANA ISLANDS + LOUISIANA + INDIANA -Campinas = CALIFORNIA + NORTHERN MARIANA ISLANDS + INDIANA + AMERICAN SAMOA -Concorde = COLORADO + NORTH CAROLINA + OREGON + DELAWARE -Ganymede = GEORGIA + NEW YORK + MAINE + DELAWARE -cascaras = CALIFORNIA + SOUTH CAROLINA + ARKANSAS + AMERICAN SAMOA -landmine = LOUISIANA + NORTH DAKOTA + MICHIGAN + NEBRASKA -melamine = MAINE + LOUISIANA + MICHIGAN + NEBRASKA -moorland = MISSOURI + OREGON + LOUISIANA + NORTH DAKOTA -malarial = MASSACHUSETTS + LOUISIANA + RHODE ISLAND + ALABAMA -memorial = MAINE + MISSOURI + RHODE ISLAND + ALABAMA -mainland = MASSACHUSETTS + INDIANA + LOUISIANA + NORTH DAKOTA -mandalas = MASSACHUSETTS + NORTH DAKOTA + ALABAMA + AMERICAN SAMOA -Mandarin = MASSACHUSETTS + NORTH DAKOTA + ARKANSAS + INDIANA -mandarin = MASSACHUSETTS + NORTH DAKOTA + ARKANSAS + INDIANA -mescalin = MAINE + SOUTH CAROLINA + ALABAMA + INDIANA -mascaras = MASSACHUSETTS + SOUTH CAROLINA + ARKANSAS + AMERICAN SAMOA -Victoria = VIRGIN ISLANDS + CONNECTICUT + OREGON + IOWA -Vineland = VIRGIN ISLANDS + NEBRASKA + LOUISIANA + NORTH DAKOTA -END - -unlink "words.txt"; -done_testing; - - -sub capture { - my($cmd) = @_; - my $out = `$cmd`; - $out =~ s/[ \r\t]*\n/\n/g; - return $out; -} |
