From 257944a211cf381d6dcd24f1e1c08b59ea6c322d Mon Sep 17 00:00:00 2001 From: "Gustavo L. de M. Chaves" Date: Mon, 24 Jun 2019 23:19:43 -0300 Subject: Gustavo Chaves's solutions to challenge 014 --- challenge-014/gustavo-chaves/perl5/README.pod | 67 +++++++++++++++++++++++++++ challenge-014/gustavo-chaves/perl5/ch-1a.pl | 36 ++++++++++++++ challenge-014/gustavo-chaves/perl5/ch-1b.pl | 30 ++++++++++++ challenge-014/gustavo-chaves/perl5/ch-2.pl | 32 +++++++++++++ 4 files changed, 165 insertions(+) create mode 100644 challenge-014/gustavo-chaves/perl5/README.pod create mode 100755 challenge-014/gustavo-chaves/perl5/ch-1a.pl create mode 100755 challenge-014/gustavo-chaves/perl5/ch-1b.pl create mode 100755 challenge-014/gustavo-chaves/perl5/ch-2.pl 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. 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. This +challenge was proposed by team member Neil Bowers. + +=back + +This was an easy one. More so because I could use te L 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; -- cgit