aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordcw <d.white@imperial.ac.uk>2019-06-30 21:15:28 +0100
committerdcw <d.white@imperial.ac.uk>2019-06-30 21:15:28 +0100
commitad63d68ec14d384b87f802ad6d47440fabb32572 (patch)
tree8906c2472e382e399989d8e21996314ec40de5f7
parentde90c1b66edaef0680509661944d5ffb2f82e9d2 (diff)
downloadperlweeklychallenge-club-ad63d68ec14d384b87f802ad6d47440fabb32572.tar.gz
perlweeklychallenge-club-ad63d68ec14d384b87f802ad6d47440fabb32572.tar.bz2
perlweeklychallenge-club-ad63d68ec14d384b87f802ad6d47440fabb32572.zip
imported my challenge 14 solutions
-rw-r--r--challenge-014/duncan-c-white/README58
-rwxr-xr-xchallenge-014/duncan-c-white/perl5/ch-1.pl59
-rwxr-xr-xchallenge-014/duncan-c-white/perl5/ch-2.pl86
-rw-r--r--challenge-014/duncan-c-white/perl5/data50
4 files changed, 224 insertions, 29 deletions
diff --git a/challenge-014/duncan-c-white/README b/challenge-014/duncan-c-white/README
index 818ecf50b2..38610de25a 100644
--- a/challenge-014/duncan-c-white/README
+++ b/challenge-014/duncan-c-white/README
@@ -1,39 +1,39 @@
-Challenge 1: "Write a script to print the date of last Friday of every
-month of a given year. For example, if the given year is 2019 then it
-should print the following:
-
-2019/01/25
-2019/02/22
-2019/03/29
-2019/04/26
-2019/05/31
-2019/06/28
-2019/07/26
-2019/08/30
-2019/09/27
-2019/10/25
-2019/11/29
-2019/12/27
+Challenge 1: "Write a script to generate Van Eck's sequence
+starting with 0. Van Eck's sequence is defined by:
+Let a0 = 0. Then, for n >= 0, if there exists an m < n such that am =
+an, take the largest such m and set an+1 = n - m; otherwise an+1 = 0.
+
+Thus the first occurrence of an integer in the sequence is followed by
+a 0, and the second and subsequent occurrences are followed by the size
+of the gap between the two most recent occurrences.
+
+The sequence is named after Jan Ritsema van Eck, who submitted it to
+the On-Line Encyclopedia of Integer Sequences.
+
+The first few terms of the sequence are(OEIS: A181391):
+
+ 0, 0, 1, 0, 2, 0, 2, 2, 1, 6, 0, 5, 0, 2, 6, 5, 4, 0, 5..
+"
My notes:
-Date::Manip should be able to do that easily enough.
+Sounds easy enough (and not, in fact, obviously recursive).
-Challenge 2: Write a script to demonstrate Mutually Recursive methods. Two
-methods are mutually recursive if the first method calls the second and
-the second calls first in turn. Using the mutually recursive methods,
-generate Hofstadter Female and Male sequences.
- F ( 0 ) = 1 ; M ( 0 ) = 0
- F ( n ) = n - M ( F ( n - 1 ) ) , n > 0
- M ( n ) = n - F ( M ( n - 1 ) ) , n > 0."
+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
+"
- My notes:
+My notes:
- Ok, looks straight forward enough, and I always liked Douglas Hofstadter
- and especially his book Godel, Esher and Bach. Initially, I did it as
- straightforward mutually recursive functions, but then I added Memoize
- optimization when I realized how slow it gets as N increases (eg to 80..)..
+Ok, looks like an interesting search problem.
diff --git a/challenge-014/duncan-c-white/perl5/ch-1.pl b/challenge-014/duncan-c-white/perl5/ch-1.pl
new file mode 100755
index 0000000000..9bb906a25e
--- /dev/null
+++ b/challenge-014/duncan-c-white/perl5/ch-1.pl
@@ -0,0 +1,59 @@
+#!/usr/bin/perl
+
+# Challenge 1: "Write a script to generate Van Eck's sequence starts with 0.
+# Van Eck's sequence is defined by:
+#
+# Let a0 = 0. Then, for n >= 0, if there exists an m < n such that am = an, take
+# the largest such m and set an+1 = n - m; otherwise an+1 = 0.
+#
+# Thus the first occurrence of an integer in the sequence is followed by
+# a 0, and the second and subsequent occurrences are followed by the size
+# of the gap between the two most recent occurrences.
+#
+# The first few terms of the sequence are(OEIS: A181391):
+#
+# 0, 0, 1, 0, 2, 0, 2, 2, 1, 6, 0, 5, 0, 2, 6, 5, 4, 0, 5..
+# "
+#
+# My notes:
+#
+# Sounds easy enough (and not, in fact, obviously recursive).
+
+
+use strict;
+use warnings;
+use Function::Parameters;
+use Data::Dumper;
+
+die "Usage: ch-1.pl NTERMS\n" unless @ARGV==1;
+my $nterms = shift;
+
+
+#
+# my $next = nextitem( $n, $seq );
+# Given @$seq[0..$n] have been calculated,
+# calculate the next value in the sequence,
+# ie. what will become $seq[$n+1]
+#
+fun nextitem( $n, $seq )
+{
+ my $target = $seq->[$n];
+ for( my $m=$n-1; $m>=0; $m-- )
+ {
+ return $n-$m if $seq->[$m] == $target;
+ }
+ return 0;
+}
+
+
+my @seq = ( 0 );
+
+print "seq[0] = 0\n";
+
+foreach my $n (0..$nterms)
+{
+ my $next = nextitem( $n, \@seq );
+ my $np1 = $n+1;
+ print "seq[$np1] = $next\n";
+ push @seq, $next;
+}
diff --git a/challenge-014/duncan-c-white/perl5/ch-2.pl b/challenge-014/duncan-c-white/perl5/ch-2.pl
new file mode 100755
index 0000000000..05f14ce797
--- /dev/null
+++ b/challenge-014/duncan-c-white/perl5/ch-2.pl
@@ -0,0 +1,86 @@
+#!/usr/bin/perl
+
+# 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
+# "
+#
+# My notes:
+#
+# Well, at first glance that looks like an interesting search problem,
+# but on second thoughts isn't that just.. a regex?
+#
+# After I did the basic thing, I added a "--territories" flag to include
+# US territories such as "AS" for American Samoa.
+
+use strict;
+use warnings;
+use Function::Parameters;
+use Data::Dumper;
+use Getopt::Long;
+
+my $territories = 0;
+die "Usage: ch-2.pl [--territories\n" unless
+ GetOptions( "territories" => \$territories ) && @ARGV == 0;
+
+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";
+$states .= "|as|dc|fm|gu|mh|mp|pw|pr|vi" if $territories;
+
+
+#
+# my %dict = readdict( $filename );
+# Read a dictionary, build a set of all words.
+# ONLY THOSE OF EVEN LENGTH.
+#
+fun readdict( $filename )
+{
+ open( my $in, '<', $filename ) || die;
+ my %dict;
+ while( <$in> )
+ {
+ chomp;
+ next unless length($_) % 2 == 0;
+ $dict{lc($_)}++;
+ }
+ close( $in );
+ return %dict;
+}
+
+my %dict = readdict( "/usr/share/dict/words" );
+
+my @result = grep { m|^($states)+$| } sort keys %dict;
+
+#print Dumper( \@result );
+
+my $longestw = "";
+my $maxlen = 0;
+
+foreach my $w (@result)
+{
+ my $l = length($w);
+ if( $l > $maxlen )
+ {
+ $maxlen = $l;
+ $longestw = $w;
+ }
+}
+
+print "$maxlen letters long\n";
+
+print "longest words made up only of US state abbreviations ";
+print "and territories " if $territories;
+print "are:\n";
+foreach my $w (@result)
+{
+ print "$w\n" if length($w) == $maxlen;
+}
+
diff --git a/challenge-014/duncan-c-white/perl5/data b/challenge-014/duncan-c-white/perl5/data
new file mode 100644
index 0000000000..741db8b1c5
--- /dev/null
+++ b/challenge-014/duncan-c-white/perl5/data
@@ -0,0 +1,50 @@
+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