diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2019-06-25 17:52:58 +0100 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2019-06-25 17:52:58 +0100 |
| commit | e3743a23276e1c2c5d1ec0304810ccd92812a129 (patch) | |
| tree | 1e31ec07a7016dac84b2d23c5d593afc16034495 /challenge-014 | |
| parent | 93abbffa6dd80caf004cfbe7a4a7eac21b5a552a (diff) | |
| download | perlweeklychallenge-club-e3743a23276e1c2c5d1ec0304810ccd92812a129.tar.gz perlweeklychallenge-club-e3743a23276e1c2c5d1ec0304810ccd92812a129.tar.bz2 perlweeklychallenge-club-e3743a23276e1c2c5d1ec0304810ccd92812a129.zip | |
- Added solutions by Duane Powell.
Diffstat (limited to 'challenge-014')
| -rw-r--r-- | challenge-014/duane-powell/README | 1 | ||||
| -rw-r--r-- | challenge-014/duane-powell/perl5/ch-1.pl | 45 | ||||
| -rw-r--r-- | challenge-014/duane-powell/perl5/ch-2.pl | 52 |
3 files changed, 98 insertions, 0 deletions
diff --git a/challenge-014/duane-powell/README b/challenge-014/duane-powell/README new file mode 100644 index 0000000000..3445422743 --- /dev/null +++ b/challenge-014/duane-powell/README @@ -0,0 +1 @@ +Solutions by Duane Powell. diff --git a/challenge-014/duane-powell/perl5/ch-1.pl b/challenge-014/duane-powell/perl5/ch-1.pl new file mode 100644 index 0000000000..310a23c8d4 --- /dev/null +++ b/challenge-014/duane-powell/perl5/ch-1.pl @@ -0,0 +1,45 @@ +#!/usr/bin/env perl +use strict; +use warnings; + +#https://perlweeklychallenge.org/blog/perl-weekly-challenge-014/ +#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. + +my $max; +my $valid_input = 0; +while (not $valid_input) { + print qq[Van_Eck iterations? ]; + chomp ($max = <STDIN>); + next unless ($max =~ /^[0-9]*$/); + $valid_input = 1; +} + +my @Van_Eck_seq = (0); +my $i = 0; +for (my $n=0; $n <= $max; $n++) { + $Van_Eck_seq[++$i] = 0; #set next in seq to 0 unless we find a previous m = n + #were looking for the largest m, so search backwards from m=n-1 to 0 + for (my $m=$n-1; $m >=0; $m--) { + if ($Van_Eck_seq[$n] == $Van_Eck_seq[$m]) { + $Van_Eck_seq[$i] = $n-$m; + last; + } + } +} + +print "$_, " foreach @Van_Eck_seq; + +__END__ + +In recreational mathematics van Eck's sequence is an Van_Eck_seqeger sequence defined recursively as follows. +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 Van_Eck_seqeger 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. + +./ch1.pl +Van_Eck iterations? 17 +0, 0, 1, 0, 2, 0, 2, 2, 1, 6, 0, 5, 0, 2, 6, 5, 4, 0, 5, + + diff --git a/challenge-014/duane-powell/perl5/ch-2.pl b/challenge-014/duane-powell/perl5/ch-2.pl new file mode 100644 index 0000000000..2252ff08a9 --- /dev/null +++ b/challenge-014/duane-powell/perl5/ch-2.pl @@ -0,0 +1,52 @@ +#!/usr/bin/env perl +use Modern::Perl; + +#https://perlweeklychallenge.org/blog/perl-weekly-challenge-014/ +#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. + +my @state_abbv = qw(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); +my %states = map {$_ => 1} @state_abbv; #create lookup table of state abbv + +#words.txt is all English words, thanks to JoeChen2 https://github.com/dwyl/english-words/blob/master/words.txt +open(my $fh, "<", "words.txt") or die "Can't open < words.txt: $!"; + +my $longest_word = ""; +while (my $word = <$fh>) { + chomp $word; + next if (length($word) % 2 == 1); #must be even length word + $word = uc($word); #convert to upper case, same case as state abbreviations + my @char_pairs = ( $word =~ m/(..)/g ); #split into array by every 2 chars + + my $all_match = 0; #if we match all 2 char pairs with a state abbv the word is a candidate for longest + foreach my $abbv (@char_pairs) { + $all_match = $states{$abbv}; + last unless ($all_match); + } + if ($all_match) { + $longest_word = $word if (length($word) >= length($longest_word)); #if there is tie for longest the last word wins + say "$word, $longest_word"; + + } +} + +__END__ + +./ch2.pl +AK, AK +AKAL, AKAL +AKAZGA, AKAZGA +... +ALKY, ALCAVALA +ALKYLAMINE, ALKYLAMINE +ALKYNE, ALKYLAMINE +ALLA, ALKYLAMINE +ALLAIN, ALKYLAMINE +... +WINY, CACOGALACTIA +WISC, CACOGALACTIA +WISD, CACOGALACTIA +WIWI, CACOGALACTIA +WV, CACOGALACTIA + |
