aboutsummaryrefslogtreecommitdiff
path: root/challenge-014
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2019-06-30 15:57:53 +0100
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2019-06-30 15:57:53 +0100
commitab20c670ec85cf45f3e9d6b1076c0d6f3ae89274 (patch)
tree507771e6b1f707252c1a81ebe3910a55c3d8481e /challenge-014
parent6c89b82dbae775546ebd469855ee2caa36393a3a (diff)
downloadperlweeklychallenge-club-ab20c670ec85cf45f3e9d6b1076c0d6f3ae89274.tar.gz
perlweeklychallenge-club-ab20c670ec85cf45f3e9d6b1076c0d6f3ae89274.tar.bz2
perlweeklychallenge-club-ab20c670ec85cf45f3e9d6b1076c0d6f3ae89274.zip
- Added solutions by Guillermo Ramos.
Diffstat (limited to 'challenge-014')
-rw-r--r--challenge-014/guillermo-ramos/perl5/ch-1.pl27
-rw-r--r--challenge-014/guillermo-ramos/perl5/ch-2.pl68
2 files changed, 95 insertions, 0 deletions
diff --git a/challenge-014/guillermo-ramos/perl5/ch-1.pl b/challenge-014/guillermo-ramos/perl5/ch-1.pl
new file mode 100644
index 0000000000..60db7e272e
--- /dev/null
+++ b/challenge-014/guillermo-ramos/perl5/ch-1.pl
@@ -0,0 +1,27 @@
+#!/usr/bin/env perl
+#
+# Write a script to generate Van Eck’s sequence starts with 0
+# (https://en.wikipedia.org/wiki/Van_Eck%27s_sequence)
+################################################################################
+
+use strict;
+use warnings;
+
+my $iterations = shift or die "Usage: $0 <iterations>\n";
+
+# Computed sequence
+my @vaneck = (0);
+
+# Map numbers to the last time they appear in sequence
+my %lastpos = (0 => 0);
+
+# For every n, compute (push) n+1
+foreach my $n (0 .. $iterations-2) {
+ my $curn = $vaneck[$n];
+ my $m = $lastpos{$curn};
+ my $nextn = defined $m ? $n-$m : 0;
+ push @vaneck, $nextn;
+ $lastpos{$curn} = $n;
+}
+
+print "@vaneck\n";
diff --git a/challenge-014/guillermo-ramos/perl5/ch-2.pl b/challenge-014/guillermo-ramos/perl5/ch-2.pl
new file mode 100644
index 0000000000..6cc4922068
--- /dev/null
+++ b/challenge-014/guillermo-ramos/perl5/ch-2.pl
@@ -0,0 +1,68 @@
+#!/usr/bin/env perl
+#
+# 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?
+# (https://en.wikipedia.org/wiki/List_of_U.S._state_abbreviations)
+################################################################################
+
+use strict;
+use warnings;
+
+use List::Util qw<max>;
+# https://github.com/dwyl/english-words
+my $DICTPATH = shift or die "Usage: $0 <dict>\n";
+
+# Extracted from Wikipedia + some Emacs macro wizardry
+my %STATES = ( AK => 'Alaska', AL => 'Alabama', AR => 'Arkansas', AZ => 'Arizona',
+ CA => 'California', CO => 'Colorado', CT => 'Connecticut', DE => 'Delaware', FL
+ => 'Florida', GA => 'Georgia', HI => 'Hawaii', IA => 'Iowa', ID => 'Idaho', IL =>
+ 'Illinois', IN => 'Indiana', KS => 'Kansas', KY => 'Kentucky', LA =>
+ 'Louisiana', MA => 'Massachusetts', MD => 'Maryland', ME => 'Maine', MI =>
+ 'Michigan', MN => 'Minnesota', MO => 'Missouri', MS => 'Mississippi', MT =>
+ 'Montana', NC => 'North Carolina', ND => 'North Dakota', NE => 'Nebraska', NH =>
+ 'New Hampshire', NJ => 'New Jersey', NM => 'New Mexico', NV => 'Nevada', NY =>
+ 'New York', OH => 'Ohio', OK => 'Oklahoma', OR => 'Oregon', PA =>
+ 'Pennsylvania', RI => 'Rhode Island', SC => 'South Carolina', SD => 'South Dakota',
+ TN => 'Tennessee', TX => 'Texas', UT => 'Utah', VA => 'Virginia', VT =>
+ 'Vermont', WA => 'Washington', WI => 'Wisconsin', WV => 'West Virginia', WY =>
+ 'Wyoming' );
+my @ABBREVS = keys %STATES;
+
+# For efficiency, keep the longest word found while filtering suitable words
+my $max_len = 0;
+
+# Given a word, return whether it can be composed using state abbreviations
+sub suitable {
+ $_ =~ s/^\s+|\s+$//g; # Trim
+
+ my $len = length $_;
+ return 0 if $len % 2 != 0; # Length must be even
+
+ # Iterate word in 2-letter chunks checking they are valid abbreviations
+ foreach my $i (0 .. $len/2-1) {
+ my $abbrev = uc(substr($_, $i*2, 2));
+ return 0 unless grep /^$abbrev$/, @ABBREVS;
+ }
+
+ $max_len = max $max_len, $len; # Update max_len if needed
+ return 1;
+}
+
+# Read words from $DICTPATH and store suitable ones in @suitable_words
+open(my $dictd, '<', $DICTPATH) or die 'Unable to open dictionary: $!';
+my @suitable_words = grep suitable, <$dictd>;
+close($dictd);
+
+# Find the longest word(s)...
+for my $word (@suitable_words) {
+ my $len = length $word;
+ if ($len == $max_len) {
+ # ... and print their composition (states)
+ my @states;
+ foreach my $i (0 .. $len/2-1) {
+ my $abbrev = uc(substr($word, $i*2, 2));
+ push @states, $STATES{$abbrev};
+ }
+ print join(" + ", @states), " = $word\n";
+ }
+}