diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-01-15 11:12:12 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-01-15 11:12:12 +0000 |
| commit | 444cb86c72a33fc4ce48de2bcf8f3926b880daeb (patch) | |
| tree | 7dacdd09cdb4ced618ec2e839fe02d4c82d50042 | |
| parent | 7fb81ee0bb75e62678fe1da28514471dda9681cb (diff) | |
| parent | bc65d5acee0f99b8586d0b561b4592fde01d3cf1 (diff) | |
| download | perlweeklychallenge-club-444cb86c72a33fc4ce48de2bcf8f3926b880daeb.tar.gz perlweeklychallenge-club-444cb86c72a33fc4ce48de2bcf8f3926b880daeb.tar.bz2 perlweeklychallenge-club-444cb86c72a33fc4ce48de2bcf8f3926b880daeb.zip | |
Merge pull request #3266 from pauloscustodio/014
Add Perl solution to challenge 014
| -rw-r--r-- | challenge-014/paulo-custodio/README | 1 | ||||
| -rw-r--r-- | challenge-014/paulo-custodio/perl/ch-1.pl | 41 | ||||
| -rw-r--r-- | challenge-014/paulo-custodio/perl/ch-2.pl | 47 | ||||
| -rw-r--r-- | challenge-014/paulo-custodio/test.pl | 50 |
4 files changed, 139 insertions, 0 deletions
diff --git a/challenge-014/paulo-custodio/README b/challenge-014/paulo-custodio/README new file mode 100644 index 0000000000..87dc0b2fbd --- /dev/null +++ b/challenge-014/paulo-custodio/README @@ -0,0 +1 @@ +Solution by Paulo Custodio diff --git a/challenge-014/paulo-custodio/perl/ch-1.pl b/challenge-014/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..f465d076d8 --- /dev/null +++ b/challenge-014/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,41 @@ +#!/usr/bin/env perl + +# 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. + +use strict; +use warnings; +use 5.030; + +sub van_eck_iter { + my @hist; + my @init = (0, 0); # first two terms + return sub { + if (@init) { + push @hist, shift @init; + return $hist[-1]; + } + else { + for my $m (reverse 0 .. $#hist-1) { + if ($hist[$m]==$hist[-1]) { + push @hist, $#hist - $m; + return $hist[-1]; + } + } + push @hist, 0; + return $hist[-1]; + } + }; +} + +my $iter = van_eck_iter(); +my $sep = ""; +for (0..96) { + print $sep, $iter->(); + $sep = ", "; +} +print "\n"; diff --git a/challenge-014/paulo-custodio/perl/ch-2.pl b/challenge-014/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..d243206b26 --- /dev/null +++ b/challenge-014/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,47 @@ +#!/usr/bin/env perl + +# 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 + +use strict; +use warnings; +use 5.030; +use Locale::US; + +use Data::Dump 'dump'; + +my $u = Locale::US->new; +my @codes = $u->all_state_codes; # 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 + +# find all words that match, save longest ones +my @longest; +open(my $fh, "<", "words.txt") or die "open words.txt: $!\n"; +while (<$fh>) { + chomp; + next unless /$regex/; # filter words that match state codes + if (!@longest || length($_) > length($longest[0])) { + @longest = ($_); + } + elsif (length($_) == length($longest[0])) { + push @longest, $_; + } +} + +# show longest words in form: word = state + state + ... +for my $word (@longest) { + my @states = map {$_ = $u->{code2state}{uc($_)}} grep {$_} split /(..)/, $word; + say $word, " = ", join(" + ", @states); +} diff --git a/challenge-014/paulo-custodio/test.pl b/challenge-014/paulo-custodio/test.pl new file mode 100644 index 0000000000..7bebc81eed --- /dev/null +++ b/challenge-014/paulo-custodio/test.pl @@ -0,0 +1,50 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use 5.030; +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 + +done_testing; + + +sub capture { + my($cmd) = @_; + my $out = `$cmd`; + $out =~ s/[ \r\t]*\n/\n/g; + return $out; +} |
