aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2019-06-30 04:54:05 +0100
committerGitHub <noreply@github.com>2019-06-30 04:54:05 +0100
commit9bdae8b343ea215edd5cb46f7f972c0d146c5fe3 (patch)
tree018e489495e9e154d2d3b55950542ec02bc8eb3d
parentde90c1b66edaef0680509661944d5ffb2f82e9d2 (diff)
parent1d3323a11ba35d732407f92d4e13713369a2c71a (diff)
downloadperlweeklychallenge-club-9bdae8b343ea215edd5cb46f7f972c0d146c5fe3.tar.gz
perlweeklychallenge-club-9bdae8b343ea215edd5cb46f7f972c0d146c5fe3.tar.bz2
perlweeklychallenge-club-9bdae8b343ea215edd5cb46f7f972c0d146c5fe3.zip
Merge pull request #315 from dmanto/branch-for-challenge-014
my proposed solutions for challenge-014, p5 1 & 2
-rw-r--r--challenge-014/daniel-mantovani/perl5/ch-1.pl40
-rw-r--r--challenge-014/daniel-mantovani/perl5/ch-2.pl101
2 files changed, 141 insertions, 0 deletions
diff --git a/challenge-014/daniel-mantovani/perl5/ch-1.pl b/challenge-014/daniel-mantovani/perl5/ch-1.pl
new file mode 100644
index 0000000000..a4e4e045d2
--- /dev/null
+++ b/challenge-014/daniel-mantovani/perl5/ch-1.pl
@@ -0,0 +1,40 @@
+# 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 v5.10;
+
+my $end = shift; #
+
+# we will need to keep last position of every number
+# in the sequence.
+
+my @last_pos;
+
+# we start at position 0,
+# and first value of sequence is zero by definition
+my $pos = 0;
+my $seq = 0;
+while ( $pos <= $end ) {
+ print "$seq ";
+
+ # now we calculate new sequence
+ # if the current $seq is in @last_pos, we calculate
+ # the interval from last postion to $seq's position
+ # and if $seq is new (i.e. not in @last_pos)
+ # new value in sequence is 0, also by definition
+ my $new_seq =
+ defined $last_pos[$seq]
+ ? $pos - $last_pos[$seq]
+ : 0;
+
+ # not until now we annotate $seq's last position
+ $last_pos[$seq] = $pos;
+
+ # set $seq and $pos to their new values
+ $seq = $new_seq;
+ $pos++;
+}
+print "\n";
diff --git a/challenge-014/daniel-mantovani/perl5/ch-2.pl b/challenge-014/daniel-mantovani/perl5/ch-2.pl
new file mode 100644
index 0000000000..b1367806c1
--- /dev/null
+++ b/challenge-014/daniel-mantovani/perl5/ch-2.pl
@@ -0,0 +1,101 @@
+# 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 v5.10;
+
+# We will use list of 50 US states from http://www.softschools.com/social_studies/state_abbreviations/
+# pasted after __DATA__
+
+my %states;
+/(\w.+)\t(\w{2})$/, $states{$2} = $1 for <DATA>;
+
+# now the keys of %states hash are the 2 letter codes of all 50 states
+# we follow by noting that if we construct a regexp like:
+# /^(AL|AK|AZ .... |WY)+$/
+# it will only match words composed by some combination of the two letter codes
+# and that is exactly what the challenge asks
+
+my $qrstring = '^(' . join( '|', keys %states ) . ')+$';
+my $test_qr = qr/$qrstring/i;
+
+# we also need a word list to check against.
+# we will use '/usr/share/dict/words'
+# or the one user provides
+
+my $words_file = shift // '/usr/share/dict/words';
+
+open my $f, $words_file or die "Usage: perl $0 <path to words list file>";
+
+my $long_word = '';
+
+# we just read word by word our dictionary, and check if we have a longer
+# match against the constructed regexp
+while ( my $w = <$f> ) {
+ chomp $w;
+ $long_word = $w if length($w) > length($long_word) && $w =~ $test_qr;
+}
+
+# and that's it, we should have longest match already on $long_word
+say $long_word;
+
+__DATA__
+ ALABAMA AL
+ ALASKA AK
+ ARIZONA AZ
+ ARKANSAS AR
+ CALIFORNIA CA
+ COLORADO CO
+ CONNECTICUT CT
+ DELAWARE DE
+ FLORIDA FL
+ GEORGIA GA
+ HAWAII HI
+ IDAHO ID
+ ILLINOIS IL
+ INDIANA IN
+ IOWA IA
+ KANSAS KS
+ KENTUCKY KY
+ LOUISIANA LA
+ MAINE ME
+ MARYLAND MD
+ MASSACHUSETTS MA
+ MICHIGAN MI
+ MINNESOTA MN
+ MISSISSIPPI MS
+ MISSOURI MO
+ MONTANA MT
+ NEBRASKA NE
+ NEVADA NV
+ NEW HAMPSHIRE NH
+ NEW JERSEY NJ
+ NEW MEXICO NM
+ NEW YORK NY
+ NORTH CAROLINA NC
+ NORTH DAKOTA ND
+ OHIO OH
+ OKLAHOMA OK
+ OREGON OR
+ PENNSYLVANIA PA
+ RHODE ISLAND RI
+ SOUTH CAROLINA SC
+ SOUTH DAKOTA SD
+ TENNESSEE TN
+ TEXAS TX
+ UTAH UT
+ VERMONT VT
+ VIRGINIA VA
+ WASHINGTON WA
+ WEST VIRGINIA WV
+ WISCONSIN WI
+ WYOMING WY