aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2019-06-24 11:07:47 +0100
committerGitHub <noreply@github.com>2019-06-24 11:07:47 +0100
commitdad3948fd0b6f57afd2683db06de512d967d4c3b (patch)
tree92b0b17d978d48790883e7abe255a4e7c9ce6326
parent6f77e37cd4c56a8be6a4e9cfa223039312b6ac5f (diff)
parenta5665f2577c2877e3708316c0256150c03e18948 (diff)
downloadperlweeklychallenge-club-dad3948fd0b6f57afd2683db06de512d967d4c3b.tar.gz
perlweeklychallenge-club-dad3948fd0b6f57afd2683db06de512d967d4c3b.tar.bz2
perlweeklychallenge-club-dad3948fd0b6f57afd2683db06de512d967d4c3b.zip
Merge pull request #294 from jacoby/p14
P14
-rw-r--r--challenge-014/dave-jacoby/p14c1.pl66
-rw-r--r--challenge-014/dave-jacoby/p14c2.pl59
2 files changed, 125 insertions, 0 deletions
diff --git a/challenge-014/dave-jacoby/p14c1.pl b/challenge-014/dave-jacoby/p14c1.pl
new file mode 100644
index 0000000000..b20de9f4c1
--- /dev/null
+++ b/challenge-014/dave-jacoby/p14c1.pl
@@ -0,0 +1,66 @@
+#!/usr/bin/env perl
+
+use strict ;
+use warnings ;
+use feature qw{ postderef say signatures state } ;
+no warnings qw{ experimental::postderef experimental::signatures } ;
+
+use Carp;
+
+# 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.
+
+
+# 0, 0, 1, 0, 2, 0, 2, 2, 1, 6, 0, 5, 0, 2, 6, 5, 4, 0, 5 ... [1]
+
+# let a[0] = 0
+my @a ;
+push @a , 0;
+
+for my $n ( 0 .. 30 ) {
+
+ # otherwise a[n+1]=0
+ $a[$n+1] = 0;
+
+ # for n >= 0, if there exists an m < n such that a[m] == a[n],
+ # take the largest such m and set a[n+1] = n-m,
+ for my $m ( 0 .. $n -1 ) {
+ $a[$n+1] = $n - $m if $a[$n]==$a[$m];
+ }
+ say qq{n = $n \t a[n] = $a[$n]};
+}
+
+__DATA__
+
+n = 0 a[n] = 0
+n = 1 a[n] = 0
+n = 2 a[n] = 1
+n = 3 a[n] = 0
+n = 4 a[n] = 2
+n = 5 a[n] = 0
+n = 6 a[n] = 2
+n = 7 a[n] = 2
+n = 8 a[n] = 1
+n = 9 a[n] = 6
+n = 10 a[n] = 0
+n = 11 a[n] = 5
+n = 12 a[n] = 0
+n = 13 a[n] = 2
+n = 14 a[n] = 6
+n = 15 a[n] = 5
+n = 16 a[n] = 4
+n = 17 a[n] = 0
+n = 18 a[n] = 5
+n = 19 a[n] = 3
+n = 20 a[n] = 0
+n = 21 a[n] = 3
+n = 22 a[n] = 2
+n = 23 a[n] = 9
+n = 24 a[n] = 0
+n = 25 a[n] = 4
+n = 26 a[n] = 9
+n = 27 a[n] = 3
+n = 28 a[n] = 6
+n = 29 a[n] = 14
+n = 30 a[n] = 0
diff --git a/challenge-014/dave-jacoby/p14c2.pl b/challenge-014/dave-jacoby/p14c2.pl
new file mode 100644
index 0000000000..a36c3983e4
--- /dev/null
+++ b/challenge-014/dave-jacoby/p14c2.pl
@@ -0,0 +1,59 @@
+#!/usr/bin/env perl
+
+use feature qw{ say };
+use strict;
+use warnings;
+
+# 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.
+
+# using the %states hash makes for easy testing
+my %states = map { $_ => 1 } qw{
+ 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
+ CD TN TX UT VT VA WA WV WI WY
+};
+
+# more universally available than my go-to dictionary DB
+my @words;
+if ( open my $fh, '<', '/usr/share/dict/words' ) {
+ @words = map { chomp; uc $_ } <$fh>;
+}
+
+my $longest = '';
+FOR: for my $word (@words) {
+ my @word = $word =~ /(\w{2})/g;
+
+ # there are apostrophes and unicode in that list
+ # this ensures that what we're looking at is valid.
+ my $join = join '', @word;
+ next unless $join eq $word;
+
+ # we check every letter pair and determine if it's in
+ # the states hash. we have labelled the outside for loop
+ # as FOR, so we can `next` on the outer loop, not just the
+ # inner loop.
+ for my $wo ( @word ) {
+ my $n = $states{$wo} ? 1 : 0;
+ next FOR unless $n;
+ }
+
+ # words that are not entirely made out of state abbreviations
+ # will not reach this point. And this will ignore words of
+ # identical size to the found longest, but I know they don't
+ # exist.
+ $longest = $word if length $longest < length $word;
+}
+say $longest;
+
+__DATA__
+
+CACOGALACTIA
+
+ noun In pathology, a bad condition of the milk.
+