aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2019-06-25 07:28:20 +0100
committerGitHub <noreply@github.com>2019-06-25 07:28:20 +0100
commit2e5218ee2ba9de05fb331ee104a34c59e25cd861 (patch)
treed12fb1bdf6a5ac553bcfb2e8039b6267deef94de
parentc4493e173baa7d5c1276171c464049c8e3fac0e0 (diff)
parent257944a211cf381d6dcd24f1e1c08b59ea6c322d (diff)
downloadperlweeklychallenge-club-2e5218ee2ba9de05fb331ee104a34c59e25cd861.tar.gz
perlweeklychallenge-club-2e5218ee2ba9de05fb331ee104a34c59e25cd861.tar.bz2
perlweeklychallenge-club-2e5218ee2ba9de05fb331ee104a34c59e25cd861.zip
Merge pull request #301 from gnustavo/014
Gustavo Chaves's solutions to challenge 014
-rw-r--r--challenge-014/gustavo-chaves/perl5/README.pod67
-rwxr-xr-xchallenge-014/gustavo-chaves/perl5/ch-1a.pl36
-rwxr-xr-xchallenge-014/gustavo-chaves/perl5/ch-1b.pl30
-rwxr-xr-xchallenge-014/gustavo-chaves/perl5/ch-2.pl32
4 files changed, 165 insertions, 0 deletions
diff --git a/challenge-014/gustavo-chaves/perl5/README.pod b/challenge-014/gustavo-chaves/perl5/README.pod
new file mode 100644
index 0000000000..3eb671d407
--- /dev/null
+++ b/challenge-014/gustavo-chaves/perl5/README.pod
@@ -0,0 +1,67 @@
+=pod
+
+=encoding utf8
+
+=head1 #1 Van Eck's sequence
+
+=over 4
+
+Write a script to generate Van Eck’s sequence starts with 0. For more
+information, please check out wikipedia
+L<page|https://en.wikipedia.org/wiki/Van_Eck%27s_sequence>. This challenge was
+proposed by team member Andrezgz.
+
+=back
+
+I wrote two solutions for this challenge: one recursive and one iterative.
+
+The recursive solution is a little easier to grasp, I think, because it's a more
+direct expression of the sequence's definition. But even using memoization it's
+33 times slower than the iterative version.
+
+ $ ./ch-1a.pl 10
+ 0
+ 0
+ 1
+ 0
+ 2
+ 0
+ 2
+ 2
+ 1
+ 6
+
+ $ ./ch-1b.pl 10
+ 0
+ 0
+ 1
+ 0
+ 2
+ 0
+ 2
+ 2
+ 1
+ 6
+
+=head1 #2 Postal Codes
+
+=over 4
+
+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
+L<page|https://en.wikipedia.org/wiki/List_of_U.S._state_abbreviations>. This
+challenge was proposed by team member Neil Bowers.
+
+=back
+
+This was an easy one. More so because I could use te L<Locale:US> module to grok
+the US Postal Codes.
+
+But I was dumbfounded when I ran it and saw the result:
+
+ $ ./ch-2.pl
+ Campinas
+
+Campinas is the name of the city where I live, in Brazil. But I don't have a
+clue what is it doing in an English dictionary! :-)
diff --git a/challenge-014/gustavo-chaves/perl5/ch-1a.pl b/challenge-014/gustavo-chaves/perl5/ch-1a.pl
new file mode 100755
index 0000000000..98002f0cad
--- /dev/null
+++ b/challenge-014/gustavo-chaves/perl5/ch-1a.pl
@@ -0,0 +1,36 @@
+#!/usr/bin/env perl
+
+# Write a script to generate Van Eck’s sequence starts with 0. For more
+# information, please check out wikipedia page
+# (https://en.wikipedia.org/wiki/Van_Eck%27s_sequence). This challenge was
+# proposed by team member Andrezgz.
+
+# This is a recursive implementation.
+
+use 5.026;
+use strict;
+use warnings;
+use Memoize;
+
+my $sequence_size = shift or die "Usage: $0 SIZE\n";
+
+memoize('van_ecks');
+sub van_ecks {
+ my ($n) = @_;
+
+ return 0 if $n <= 1;
+
+ my $previous_value = van_ecks($n-1);
+
+ for (my $i=$n-2; $i >= 0; --$i) {
+ if (van_ecks($i) == $previous_value) {
+ return ($n-1) - $i;
+ }
+ }
+
+ return 0;
+}
+
+for my $i (0 .. $sequence_size-1) {
+ say van_ecks($i);
+}
diff --git a/challenge-014/gustavo-chaves/perl5/ch-1b.pl b/challenge-014/gustavo-chaves/perl5/ch-1b.pl
new file mode 100755
index 0000000000..3b24e8a570
--- /dev/null
+++ b/challenge-014/gustavo-chaves/perl5/ch-1b.pl
@@ -0,0 +1,30 @@
+#!/usr/bin/env perl
+
+# Write a script to generate Van Eck’s sequence starts with 0. For more
+# information, please check out wikipedia page
+# (https://en.wikipedia.org/wiki/Van_Eck%27s_sequence). This challenge was
+# proposed by team member Andrezgz.
+
+# This is an iterative implementation, which is more than 30x faster than the
+# recursive one.
+
+use 5.026;
+use strict;
+use warnings;
+
+my $sequence_size = shift or die "Usage: $0 SIZE\n";
+
+my @sequence = (0);
+
+VALUE:
+for my $i (0 .. $sequence_size-2) {
+ for (my $j=$i-1; $j >= 0; --$j) {
+ if ($sequence[$j] == $sequence[$i]) {
+ push @sequence, $i-$j;
+ next VALUE;
+ }
+ }
+ push @sequence, 0;
+}
+
+say foreach @sequence;
diff --git a/challenge-014/gustavo-chaves/perl5/ch-2.pl b/challenge-014/gustavo-chaves/perl5/ch-2.pl
new file mode 100755
index 0000000000..66a3bc60bd
--- /dev/null
+++ b/challenge-014/gustavo-chaves/perl5/ch-2.pl
@@ -0,0 +1,32 @@
+#!/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?
+# Here is the list of U.S. states abbreviations as per wikipedia page
+# (https://en.wikipedia.org/wiki/List_of_U.S._state_abbreviations). This
+# challenge was proposed by team member Neil Bowers.
+
+use 5.026;
+use strict;
+use autodie;
+use warnings;
+use Locale::US;
+use List::Util qw(all);
+
+my @postal_codes = Locale::US->new()->all_state_codes();
+my %postal_codes = map {lc($_) => undef} @postal_codes;
+
+my $largest_word_so_far = '';
+
+open my $dict, '<', '/usr/share/dict/words';
+while (<$dict>) {
+ chomp;
+ my $word = $_;
+ next unless length($word) > length($largest_word_so_far);
+ next unless (length($word) % 2) == 0;
+ next unless all {exists $postal_codes{$_}} (lc($word) =~ m/../g);
+ $largest_word_so_far = $word;
+}
+close $dict;
+
+say $largest_word_so_far;