diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2019-06-24 23:30:16 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-06-24 23:30:16 +0100 |
| commit | 8fd2836ca56225353fda2f0ad395244fa0862c86 (patch) | |
| tree | a9c362f82cb63772b944c8f99c12ce041995d7b9 | |
| parent | ad39658bba41950a0c9c66b20e4440ae25c3f3f2 (diff) | |
| parent | a543d8cc9724f838b37f4dd45210114364b207c0 (diff) | |
| download | perlweeklychallenge-club-8fd2836ca56225353fda2f0ad395244fa0862c86.tar.gz perlweeklychallenge-club-8fd2836ca56225353fda2f0ad395244fa0862c86.tar.bz2 perlweeklychallenge-club-8fd2836ca56225353fda2f0ad395244fa0862c86.zip | |
Merge pull request #299 from choroba/e-ch-14
Add solution to 014 by E. Choroba
| -rwxr-xr-x | challenge-014/e-choroba/perl5/ch-1.pl | 27 | ||||
| -rwxr-xr-x | challenge-014/e-choroba/perl5/ch-2.pl | 33 |
2 files changed, 60 insertions, 0 deletions
diff --git a/challenge-014/e-choroba/perl5/ch-1.pl b/challenge-014/e-choroba/perl5/ch-1.pl new file mode 100755 index 0000000000..b47e1705f0 --- /dev/null +++ b/challenge-014/e-choroba/perl5/ch-1.pl @@ -0,0 +1,27 @@ +#!/usr/bin/perl +use warnings; +use strict; +use feature qw{ say }; + +sub van_eck { + my %latest; + my ($n, $a_n) = (0, 0); + return sub { + my $r = $a_n; + my $a_n_plus_1 = exists $latest{$a_n} + ? $n - $latest{$a_n} + : 0; + $latest{$a_n} = $n++; + $a_n = $a_n_plus_1; + return $r + } +} + +use Test::More; + +my $iterator = van_eck(); + +is_deeply([map $iterator->(), 1 .. 19], + [0, 0, 1, 0, 2, 0, 2, 2, 1, 6, 0, 5, 0, 2, 6, 5, 4, 0, 5]); + +done_testing(); diff --git a/challenge-014/e-choroba/perl5/ch-2.pl b/challenge-014/e-choroba/perl5/ch-2.pl new file mode 100755 index 0000000000..ca5a4dc3e8 --- /dev/null +++ b/challenge-014/e-choroba/perl5/ch-2.pl @@ -0,0 +1,33 @@ +#!/usr/bin/perl +use warnings; +use strict; +use feature qw{ say }; + +my %states; +@states{qw(AL AK AZ AR CA CO CT DE DC 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)} = (); + +my $dictionary = '/usr/share/dict/british'; +open my $in, '<', $dictionary or die $!; +my @longest = (""); +while (my $word = <$in>) { + chomp $word; + next if 1 & length $word; + + my $uc_word = uc $word; + my @pairs = $uc_word =~ /\G(..)/g; + + next if length $uc_word != 2 * @pairs; + + next if grep ! exists $states{$_}, @pairs; + + next if length($word) < length $longest[0]; + + if (length($word) == length $longest[0]) { + push @longest, $word; + } else { + @longest = ($word); + } +} +say for @longest; |
