diff options
| author | Dave Jacoby <jacoby.david@gmail.com> | 2019-07-01 09:31:04 -0400 |
|---|---|---|
| committer | Dave Jacoby <jacoby.david@gmail.com> | 2019-07-01 09:31:04 -0400 |
| commit | f7efd0862cd1c28c3d270d9f75ffb5fda9864800 (patch) | |
| tree | 14d70e94e8aa109877365f75d5086b2e1191de94 /challenge-014 | |
| parent | b0898d6b9f102aa5f24df350ad9d87f21f37ba17 (diff) | |
| parent | d058cc897094733ded2988aa03af754c92e7ab92 (diff) | |
| download | perlweeklychallenge-club-f7efd0862cd1c28c3d270d9f75ffb5fda9864800.tar.gz perlweeklychallenge-club-f7efd0862cd1c28c3d270d9f75ffb5fda9864800.tar.bz2 perlweeklychallenge-club-f7efd0862cd1c28c3d270d9f75ffb5fda9864800.zip | |
Merge branch 'master' of github.com:manwar/perlweeklychallenge-club
Diffstat (limited to 'challenge-014')
66 files changed, 1112927 insertions, 31 deletions
diff --git a/challenge-014/adam-russell/blog.txt b/challenge-014/adam-russell/blog.txt new file mode 100644 index 0000000000..54f5ab38b5 --- /dev/null +++ b/challenge-014/adam-russell/blog.txt @@ -0,0 +1 @@ +https://adamcrussell.livejournal.com/4974.html diff --git a/challenge-014/adam-russell/perl5/ch-1.pl b/challenge-014/adam-russell/perl5/ch-1.pl new file mode 100644 index 0000000000..5779cbd9e0 --- /dev/null +++ b/challenge-014/adam-russell/perl5/ch-1.pl @@ -0,0 +1,15 @@ +use strict; +use warnings; +## +# Write a script to generate Van Eck’s sequence starts with 0. +## +my $i = 1; +my @sequence = (0, 0); +print join(" ", @sequence) . " "; +{ + my $last_index = (sort { $b <=> $a} grep { $sequence[$_] == $sequence[$i] } 0..(@sequence - 2))[0]; + $i++; + $sequence[$i] = defined($last_index) ? ($i - $last_index - 1) : 0; + print $sequence[$i] . " "; + redo; +} diff --git a/challenge-014/adam-russell/perl5/ch-2.pl b/challenge-014/adam-russell/perl5/ch-2.pl new file mode 100644 index 0000000000..917dc75f02 --- /dev/null +++ b/challenge-014/adam-russell/perl5/ch-2.pl @@ -0,0 +1,82 @@ +use strict; +use warnings; +## +# 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. +## +my %word_product; +my %letter_factor = ( + AL => 2, + AK => 2, + AZ => 2, + AR => 2, + CA => 2, + CO => 2, + CT => 2, + DE => 2, + FL => 2, + GA => 2, + HI => 2, + ID => 2, + IL => 2, + IN => 2, + IA => 2, + KS => 2, + KY => 2, + LA => 2, + ME => 2, + MD => 2, + MA => 2, + MI => 2, + MN => 2, + MS => 2, + MO => 2, + MT => 2, + NE => 2, + NV => 2, + NH => 2, + NJ => 2, + NM => 2, + NY => 2, + NC => 2, + ND => 2, + OH => 2, + OK => 2, + OR => 2, + PA => 2, + RI => 2, + SC => 2, + SD => 2, + TN => 2, + TX => 2, + UT => 2, + VT => 2, + VA => 2, + WA => 2, + WV => 2, + WI => 2, + WY => 2 +); +map { $letter_factor{ lc($_) } = $letter_factor{ $_ }; delete($letter_factor{ $_ }) } keys %letter_factor; + +## +# Main +## +while(<>){ + chomp($_); + my $word = lc($_); + $word =~ tr/-//d; + my @letters = split(//, $word); + if(@letters % 2 == 0){ + my @two_letter_chunks; + for(my $i = 0; $i < @letters; $i+=2){ + push @two_letter_chunks, $letters[$i] . $letters[$i + 1]; + } + my $product = 1; + map {$product *= $_} map{defined($letter_factor{$_}) ? $letter_factor{$_} : 0} @two_letter_chunks; + $word_product{$word} = $product if($product > 0); + } +} +my @sorted = sort {$word_product{$b} <=> $word_product{$a}} keys %word_product; +print $sorted[0] . "\n"; diff --git a/challenge-014/arne-sommer/blog.txt b/challenge-014/arne-sommer/blog.txt new file mode 100644 index 0000000000..1a6fc2ea08 --- /dev/null +++ b/challenge-014/arne-sommer/blog.txt @@ -0,0 +1 @@ +https://perl6.eu/van-eck-state.html diff --git a/challenge-014/arne-sommer/perl6/ch-1.p6 b/challenge-014/arne-sommer/perl6/ch-1.p6 new file mode 100755 index 0000000000..0a454685d1 --- /dev/null +++ b/challenge-014/arne-sommer/perl6/ch-1.p6 @@ -0,0 +1,60 @@ +#! /usr/bin/env perl6 + +unit sub MAIN (Int $limit where 2 <= $limit <= 100000 = 10, :$verbose = False, :$test = False); + +my @van-eck = (0); + +my %seen; + +for ^($limit -1) -> $pos +{ + %seen{@van-eck[*-1]}.defined + ?? @van-eck.push: $pos - %seen{@van-eck[*-1]} + !! @van-eck.push: 0; + + %seen{@van-eck[*-2]} = $pos; +} + +if $test +{ + use LWP::Simple; + + my $ok = 0; + my $error = 0; + my $index = 1; + + for LWP::Simple.get('https://oeis.org/A181391/b181391.txt').lines -> $line + { + last if $index > $limit; + + my $my = @van-eck[$index -1]; + my $def = $line.words[1]; + + if $my == $def + { + say "$index: $my == $def (OK)"; + $ok++; + } + else + { + say "$index: $my != $def (Error)"; + $error++; + } + + $index++; + } + + say "\nOK: $ok"; + say "Error: $error"; +} + +elsif $verbose +{ + my $count = 1; + say "{ $count++ }: $_" for @van-eck; +} + +else +{ + say @van-eck.join(", "); +} diff --git a/challenge-014/arne-sommer/perl6/ch-2.p6 b/challenge-014/arne-sommer/perl6/ch-2.p6 new file mode 100755 index 0000000000..d8ebda41fe --- /dev/null +++ b/challenge-014/arne-sommer/perl6/ch-2.p6 @@ -0,0 +1,53 @@ +#! /usr/bin/env perl6 + +unit sub MAIN ($dictionary where $dictionary.IO.e && $dictionary.IO.r = "/usr/share/dict/british-english", :$all); + +my %states = + "AK Alaska LA Louisiana OH Ohio + AL Alabama MA Massachusetts OK Oklahoma + AR Arkansas MD Maryland OR Oregon + AZ Arizona ME Maine PA Pennsylvania + CA California MI Michigan RI Rhode Island + CO Colorado MN Minnesota SC South Carolina + CT Connecticut MO Missouri SD South Dakota + DE Delaware MS Mississippi TN Tennessee + FL Florida MT Montana TX Texas + GA Georgia NC North Carolina UT Utah + HI Hawaii ND North Dakota VA Virginia + IA Iowa NE Nebraska VT Vermont + ID Idaho NH New Hampshire WA Washington + IL Illinois NJ New Jersey WI Wisconsin + IN Indiana NM New Mexico WV West Virginia + KS Kansas NV Nevada WY Wyoming + KY Kentucky NY New York".split(/\s\s+/).hash; + +sub get-dictionary ($file where $file.IO.r) +{ + return $file.IO.lines>>.uc.grep({ .chars %% 2 }).grep(* !~~ /\W/).unique.sort({ $^b.chars cmp $^a.chars }); # As "aß" -> "ASS" +} + +my $found = 0; + +for get-dictionary($dictionary) -> $word +{ + last if $word.chars < $found; + + check-word($word); + + last if $found && !$all; +} + +say "\nWord length: $found."; + +sub check-word ($word) +{ + my @parts = $word.comb(2); + + return unless %states{$_} for @parts; + + say "{ @parts.map({ %states{$_} }).join(" + ") } = $word"; + + $found = $word.chars; +} + + diff --git a/challenge-014/arne-sommer/perl6/dictionary-list-by-length b/challenge-014/arne-sommer/perl6/dictionary-list-by-length new file mode 100755 index 0000000000..4b629793e1 --- /dev/null +++ b/challenge-014/arne-sommer/perl6/dictionary-list-by-length @@ -0,0 +1,10 @@ +#! /usr/bin/env perl6 + +my $dictionary = "/usr/share/dict/british-english"; + +.say for get-dictionary($dictionary); + +sub get-dictionary ($file where $file.IO.r) +{ + return $file.IO.lines.grep(* !~~ /\W/)>>.uc.sort({ $^b.chars cmp $^a.chars }); +} diff --git a/challenge-014/arne-sommer/perl6/us-states b/challenge-014/arne-sommer/perl6/us-states new file mode 100755 index 0000000000..14abc8b987 --- /dev/null +++ b/challenge-014/arne-sommer/perl6/us-states @@ -0,0 +1,23 @@ +#! /usr/bin/env perl6 + +my %states = + "AK Alaska LA Louisiana OH Ohio + AL Alabama MA Massachusetts OK Oklahoma + AR Arkansas MD Maryland OR Oregon + AZ Arizona ME Maine PA Pennsylvania + CA California MI Michigan RI Rhode Island + CO Colorado MN Minnesota SC South Carolina + CT Connecticut MO Missouri SD South Dakota + DE Delaware MS Mississippi TN Tennessee + FL Florida MT Montana TX Texas + GA Georgia NC North Carolina UT Utah + HI Hawaii ND North Dakota VA Virginia + IA Iowa NE Nebraska VT Vermont + ID Idaho NH New Hampshire WA Washington + IL Illinois NJ New Jersey WI Wisconsin + IN Indiana NM New Mexico WV West Virginia + KS Kansas NV Nevada WY Wyoming + KY Kentucky NY New York".split(/\s\s+/).hash; + +say %states.keys.sort; + diff --git a/challenge-014/arne-sommer/perl6/van-eck-list b/challenge-014/arne-sommer/perl6/van-eck-list new file mode 100755 index 0000000000..22d831458e --- /dev/null +++ b/challenge-014/arne-sommer/perl6/van-eck-list @@ -0,0 +1,24 @@ +#! /usr/bin/env perl6 + +unit sub MAIN (Int $limit = 10); + +my @van-eck = (0); + +@van-eck.push(van-help(@van-eck)) for ^($limit -1); + +say @van-eck.join(", "); + +sub van-help(@array is copy) +{ + my $last = @array.pop; + + if any(@array) == $last + { + for @array.reverse + { + state $delta++; + return $delta if $_ == $last; + } + } + return 0; +}
\ No newline at end of file diff --git a/challenge-014/arne-sommer/perl6/van-eck-verbose b/challenge-014/arne-sommer/perl6/van-eck-verbose new file mode 100755 index 0000000000..00cde75024 --- /dev/null +++ b/challenge-014/arne-sommer/perl6/van-eck-verbose @@ -0,0 +1,32 @@ +#! /usr/bin/env perl6 + +unit sub MAIN (Int $limit = 10, :$verbose = False); + +my @van-eck = (0); + +@van-eck.push(van-help(@van-eck)) for ^($limit -1); + +if $verbose +{ + my $count = 1; + say "{ $count++ }: $_" for @van-eck; +} +else +{ + say @van-eck.join(", "); +} + +sub van-help(@array is copy) +{ + my $last = @array.pop; + + if any(@array) == $last + { + for @array.reverse + { + state $delta++; + return $delta if $_ == $last; + } + } + return 0; +}
\ No newline at end of file diff --git a/challenge-014/athanasius/blog.txt b/challenge-014/athanasius/blog.txt new file mode 100644 index 0000000000..d4e3ec5bdc --- /dev/null +++ b/challenge-014/athanasius/blog.txt @@ -0,0 +1 @@ +http://blogs.perl.org/users/athanasius/2019/06/perl-weekly-challenge-014.html diff --git a/challenge-014/athanasius/perl5/ch-1.pl b/challenge-014/athanasius/perl5/ch-1.pl new file mode 100644 index 0000000000..6f5b2b73e4 --- /dev/null +++ b/challenge-014/athanasius/perl5/ch-1.pl @@ -0,0 +1,65 @@ +#!perl + +################################################################################ +=comment + +Perl Weekly Challenge 014 +========================= + +Challenge #1 +------------ + +Write a script to generate Van Eck's sequence starts with 0. For more +information, please check out wikipedia +[ https://en.wikipedia.org/wiki/Van_Eck%27s_sequence |page]. This challenge was +proposed by team member *Andrezgz*. + +=cut +################################################################################ + +#--------------------------------------# +# Copyright © 2019 PerlMonk Athanasius # +#--------------------------------------# + +use strict; +use warnings; +use Const::Fast; +use Regexp::Common; + +const my $LENGTH => 27; +const my $USAGE => "USAGE: perl $0 [<length>]"; + +$| = 1; + +MAIN: +{ + scalar @ARGV <= 1 + or die "\n$USAGE\n"; + + my $length = $ARGV[0] // $LENGTH; + + $length =~ /^$RE{num}{int}$/ && $length > 0 + or die "\nInvalid length '$length': must be an integer > 0\n"; + + print "\nThe first $length terms in Van Eck's sequence are:\n", + join( ', ', van_eck($length)->@* ), "\n"; +} + +sub van_eck +{ + my ($len) = @_; + my @seq = (undef, 0); + my %indices; + + for my $n (1 .. $len - 1) + { + my $old_term = $seq[$n]; + push @seq, exists $indices{$old_term} ? $n - $indices{$old_term} : 0; + $indices{$old_term} = $n; + } + + shift @seq; + return \@seq; +} + +################################################################################ diff --git a/challenge-014/athanasius/perl5/ch-2.pl b/challenge-014/athanasius/perl5/ch-2.pl new file mode 100644 index 0000000000..cee1b11946 --- /dev/null +++ b/challenge-014/athanasius/perl5/ch-2.pl @@ -0,0 +1,147 @@ +#!perl + +################################################################################ +=comment + +Perl Weekly Challenge 014 +========================= + +Challenge #2 +------------ + +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 +[ https://en.wikipedia.org/wiki/List_of_U.S._state_abbreviations |page]. This +challenge was proposed by team member *Neil Bowers*. + + For example, + Pennsylvania + Connecticut = PACT + Wisconsin + North Dakota = WIND + Maine + Alabama = MEAL + California + Louisiana + Massachusetts + Rhode Island = Calamari + +=cut +################################################################################ + +#--------------------------------------# +# Copyright © 2019 PerlMonk Athanasius # +#--------------------------------------# + +use strict; +use utf8; +use warnings; +use Const::Fast; +use constant TIMER => 1; + +# Abbreviations according to the "USPS" (United States Postal Service) column of +# the Table in https://en.wikipedia.org/wiki/List_of_U.S._state_abbreviations + +const my %STATES_POSTAL => + ( + AL => 'Alabama', AK => 'Alaska', AZ => 'Arizona', + AR => 'Arkansas', CA => 'California', CO => 'Colorado', + CT => 'Connecticut', DE => 'Delaware', FL => 'Florida', + GA => 'Georgia', HI => 'Hawaii', ID => 'Idaho', + IL => 'Illinois', IN => 'Indiana', IA => 'Iowa', + KS => 'Kansas', KY => 'Kentucky', LA => 'Louisiana', + ME => 'Maine', MD => 'Maryland', MA => 'Massachusetts', + MI => 'Michigan', MN => 'Minnesota', MS => 'Mississippi', + MO => 'Missouri', MT => 'Montana', NE => 'Nebraska', + NV => 'Nevada', NH => 'New Hampshire', NJ => 'New Jersey', + NM => 'New Mexico', NY => 'New York', NC => 'North Carolina', + ND => 'North Dakota', OH => 'Ohio', OK => 'Oklahoma', + OR => 'Oregon', PA => 'Pennsylvania', RI => 'Rhode Island', + SC => 'South Carolina', SD => 'South Dakota', TN => 'Tennessee', + TX => 'Texas', UT => 'Utah', VT => 'Vermont', + VA => 'Virginia', WA => 'Washington', WV => 'West Virginia', + WI => 'Wisconsin', WY => 'Wyoming', + ); + +# Regular expression to match (case-insensitively, but otherwise exactly) any +# one of the 50 state postal abbreviations + +const |
