diff options
| author | Adam Russell <adam.russell@optum.com> | 2019-04-12 15:44:17 -0400 |
|---|---|---|
| committer | Adam Russell <adam.russell@optum.com> | 2019-04-12 15:44:17 -0400 |
| commit | df86f815d821bdf67f1dc84fd300b23f349871f0 (patch) | |
| tree | 528bdbc2f60ff57c3a3297a2ecf25f188213ec81 | |
| parent | 8b74c2038a052927add114b4a69f4b7c214e1ad0 (diff) | |
| parent | 8b880c7d6af0196945a1fbc84dd5f5f56cd0a683 (diff) | |
| download | perlweeklychallenge-club-df86f815d821bdf67f1dc84fd300b23f349871f0.tar.gz perlweeklychallenge-club-df86f815d821bdf67f1dc84fd300b23f349871f0.tar.bz2 perlweeklychallenge-club-df86f815d821bdf67f1dc84fd300b23f349871f0.zip | |
Merge remote-tracking branch 'upstream/master'
50 files changed, 1835 insertions, 989 deletions
diff --git a/challenge-001/alexdaniel/perl6/ch-1.p6 b/challenge-001/alex-daniel/perl6/ch-1.p6 index 047fbacbd1..047fbacbd1 100644 --- a/challenge-001/alexdaniel/perl6/ch-1.p6 +++ b/challenge-001/alex-daniel/perl6/ch-1.p6 diff --git a/challenge-001/alexdaniel/perl6/ch-2.p6 b/challenge-001/alex-daniel/perl6/ch-2.p6 index ce84a7530b..ce84a7530b 100644 --- a/challenge-001/alexdaniel/perl6/ch-2.p6 +++ b/challenge-001/alex-daniel/perl6/ch-2.p6 diff --git a/challenge-001/alexdaniel/README.md b/challenge-001/alexdaniel/README.md deleted file mode 100644 index c22d28b3e8..0000000000 --- a/challenge-001/alexdaniel/README.md +++ /dev/null @@ -1 +0,0 @@ -Solutions by [AlexDaniel](https://github.com/AlexDaniel/) diff --git a/challenge-001/nick-logan/perl5/ch-1.pl b/challenge-001/nick-logan/perl5/ch-1.pl index c35822d44b..e20ce33987 100644 --- a/challenge-001/nick-logan/perl5/ch-1.pl +++ b/challenge-001/nick-logan/perl5/ch-1.pl @@ -1 +1,3 @@ +# WARNING: this polyglot breaks best practices of both Perl 5 and Perl 6 in order to run on both + print((($_ eq "e") and uc($_) or $_)) for split("", "Perl Weekly Challenge") diff --git a/challenge-001/nick-logan/perl5/ch-2.pl b/challenge-001/nick-logan/perl5/ch-2.pl index cb8a9074af..00372d5c38 100644 --- a/challenge-001/nick-logan/perl5/ch-2.pl +++ b/challenge-001/nick-logan/perl5/ch-2.pl @@ -1,3 +1,3 @@ -print((join( "", (!($_ % 3) and "Fizz" or ""), (!($_ % 5) and "Buzz" or "") ) or $_), "\n") for 1..20; - +# WARNING: this polyglot breaks best practices of both Perl 5 and Perl 6 in order to run on both +print((join( "", (!($_ % 3) and "Fizz" or ""), (!($_ % 5) and "Buzz" or "") ) or $_), "\n") for 1..20; diff --git a/challenge-001/nick-logan/perl6/ch-1.p6 b/challenge-001/nick-logan/perl6/ch-1.p6 index c35822d44b..e20ce33987 100644 --- a/challenge-001/nick-logan/perl6/ch-1.p6 +++ b/challenge-001/nick-logan/perl6/ch-1.p6 @@ -1 +1,3 @@ +# WARNING: this polyglot breaks best practices of both Perl 5 and Perl 6 in order to run on both + print((($_ eq "e") and uc($_) or $_)) for split("", "Perl Weekly Challenge") diff --git a/challenge-001/nick-logan/perl6/ch-2.p6 b/challenge-001/nick-logan/perl6/ch-2.p6 index cb8a9074af..00372d5c38 100644 --- a/challenge-001/nick-logan/perl6/ch-2.p6 +++ b/challenge-001/nick-logan/perl6/ch-2.p6 @@ -1,3 +1,3 @@ -print((join( "", (!($_ % 3) and "Fizz" or ""), (!($_ % 5) and "Buzz" or "") ) or $_), "\n") for 1..20; - +# WARNING: this polyglot breaks best practices of both Perl 5 and Perl 6 in order to run on both +print((join( "", (!($_ % 3) and "Fizz" or ""), (!($_ % 5) and "Buzz" or "") ) or $_), "\n") for 1..20; diff --git a/challenge-002/nick-logan/perl5/ch-1.pl b/challenge-002/nick-logan/perl5/ch-1.pl index f7787b8a04..ffbf7bc003 100644 --- a/challenge-002/nick-logan/perl5/ch-1.pl +++ b/challenge-002/nick-logan/perl5/ch-1.pl @@ -1 +1,3 @@ +# WARNING: this polyglot breaks best practices of both Perl 5 and Perl 6 in order to run on both + my @ARGV = do { sub eval($_) { &EVAL($_) }; eval( ("0" and q|@*ARGS| or q|@ARGV|) ) }; print("$_\n") for map &{ sub ($_) { /^0(0|1|2|3|4|5|6|7|8|9)+/ and (0+$_) or $_ } }.(), @ARGV; diff --git a/challenge-002/nick-logan/perl5/ch-2.pl b/challenge-002/nick-logan/perl5/ch-2.pl index f6d6226cda..95628189ed 100644 --- a/challenge-002/nick-logan/perl5/ch-2.pl +++ b/challenge-002/nick-logan/perl5/ch-2.pl @@ -1,3 +1,5 @@ +# WARNING: this polyglot breaks best practices of both Perl 5 and Perl 6 in order to run on both + my @ARGV = do { sub eval { chomp &EVAL(@_) }; eval( ("0" and q|@*ARGS| or q|@ARGV|) ) }; my ($state, $result, $dict, $base35) = (1, "", {}, @ARGV[0]); $dict{$_} = $_ for "1".."9"; diff --git a/challenge-002/nick-logan/perl6/ch-1.p6 b/challenge-002/nick-logan/perl6/ch-1.p6 index f7787b8a04..ffbf7bc003 100644 --- a/challenge-002/nick-logan/perl6/ch-1.p6 +++ b/challenge-002/nick-logan/perl6/ch-1.p6 @@ -1 +1,3 @@ +# WARNING: this polyglot breaks best practices of both Perl 5 and Perl 6 in order to run on both + my @ARGV = do { sub eval($_) { &EVAL($_) }; eval( ("0" and q|@*ARGS| or q|@ARGV|) ) }; print("$_\n") for map &{ sub ($_) { /^0(0|1|2|3|4|5|6|7|8|9)+/ and (0+$_) or $_ } }.(), @ARGV; diff --git a/challenge-002/nick-logan/perl6/ch-2.p6 b/challenge-002/nick-logan/perl6/ch-2.p6 index f6d6226cda..95628189ed 100644 --- a/challenge-002/nick-logan/perl6/ch-2.p6 +++ b/challenge-002/nick-logan/perl6/ch-2.p6 @@ -1,3 +1,5 @@ +# WARNING: this polyglot breaks best practices of both Perl 5 and Perl 6 in order to run on both + my @ARGV = do { sub eval { chomp &EVAL(@_) }; eval( ("0" and q|@*ARGS| or q|@ARGV|) ) }; my ($state, $result, $dict, $base35) = (1, "", {}, @ARGV[0]); $dict{$_} = $_ for "1".."9"; diff --git a/challenge-002/nick-logan/perl6/to-base35.p6 b/challenge-002/nick-logan/perl6/to-base35.p6 index 683acda914..e4f7e98a95 100644 --- a/challenge-002/nick-logan/perl6/to-base35.p6 +++ b/challenge-002/nick-logan/perl6/to-base35.p6 @@ -1,3 +1,5 @@ +# WARNING: this polyglot breaks best practices of both Perl 5 and Perl 6 in order to run on both + # bin/to-base35 sub to_int($_) { my @i = split(/"."/, $_[0]); @i[0] }; my @ARGV = do { sub eval { chomp &EVAL(@_) }; eval( ("0" and q|@*ARGS| or q|@ARGV|) ) }; @@ -5,7 +7,7 @@ my ($result, $dict, $base10) = ("", {}, @ARGV[0]); $dict{$_} = $_ for "1".."9"; $dict{ord($_) - 55} = $_ for "A".."Y"; while ($base10 > 0) { - $result = join("", $dict{to_int($base10 % 35)}, $result); - $base10 = to_int($base10 / 35); + $result = join("", $dict{to_int($base10 % 35)}, $result); + $base10 = to_int($base10 / 35); } print("$result\n"); diff --git a/challenge-002/ruben-westerberg/ch-1.pl b/challenge-002/ruben-westerberg/perl5/ch-1.pl index ac4cf70614..ac4cf70614 100755 --- a/challenge-002/ruben-westerberg/ch-1.pl +++ b/challenge-002/ruben-westerberg/perl5/ch-1.pl diff --git a/challenge-002/ruben-westerberg/ch-2.pl b/challenge-002/ruben-westerberg/perl5/ch-2.pl index 66e78301c8..66e78301c8 100755 --- a/challenge-002/ruben-westerberg/ch-2.pl +++ b/challenge-002/ruben-westerberg/perl5/ch-2.pl diff --git a/challenge-002/ruben-westerberg/ch-1.p6 b/challenge-002/ruben-westerberg/perl6/ch-1.p6 index 4ce164234e..4ce164234e 100755 --- a/challenge-002/ruben-westerberg/ch-1.p6 +++ b/challenge-002/ruben-westerberg/perl6/ch-1.p6 diff --git a/challenge-002/ruben-westerberg/ch-2.p6 b/challenge-002/ruben-westerberg/perl6/ch-2.p6 index c011ae237c..c011ae237c 100755 --- a/challenge-002/ruben-westerberg/ch-2.p6 +++ b/challenge-002/ruben-westerberg/perl6/ch-2.p6 diff --git a/challenge-003/alex-daniel/README b/challenge-003/alex-daniel/README new file mode 100644 index 0000000000..65550789bd --- /dev/null +++ b/challenge-003/alex-daniel/README @@ -0,0 +1 @@ +Solution by Alex Daniel. diff --git a/challenge-003/andrezgz/perl5/ch-1.pl b/challenge-003/andrezgz/perl5/ch-1.pl new file mode 100644 index 0000000000..cec1731d72 --- /dev/null +++ b/challenge-003/andrezgz/perl5/ch-1.pl @@ -0,0 +1,17 @@ +#!/usr/bin/perl + +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-003/ +# Challenge #1 +# Create a script to generate 5-smooth numbers, whose prime divisors are less or equal to 5. +# They are also called Hamming/Regular/Ugly numbers. +# For more information, please check this wikipedia. https://en.wikipedia.org/wiki/Regular_number + +use strict; + +my $last = shift || 20; + +foreach my $n ( 1 .. $last ) { + my $r = $n; + for (2,3,5) { $r /= $_ until ($r % $_) } + print $n.$/ if ($r == 1); +} diff --git a/challenge-003/andrezgz/perl5/ch-2.pl b/challenge-003/andrezgz/perl5/ch-2.pl new file mode 100644 index 0000000000..3d9d8e208a --- /dev/null +++ b/challenge-003/andrezgz/perl5/ch-2.pl @@ -0,0 +1,27 @@ +#!/usr/bin/perl + +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-003/ +# Challenge #2 +# Create a script that generates Pascal Triangle. Accept number of rows from the command line. +# The Pascal Triangle should have at least 3 rows. +# For more information about Pascal Triangle, check this wikipedia page. https://en.wikipedia.org/wiki/Pascal%27s_triangle + +use strict; +use warnings; + +my $rows = $ARGV[0] || 0; + +if ($rows < 3) { + print "The Pascal Triangle should have at least 3 rows\n"; + $rows = 3; +} + +my @row = (1); + +while ($rows) { + print join(' ',@row) .$/; + $rows--; + @row = map {$row[$_] += $row[$_+1]} 0..@row-2; + unshift @row, 1; + push @row, 1; +} diff --git a/challenge-003/cliveholloway/README b/challenge-003/cliveholloway/README new file mode 100644 index 0000000000..b69201296e --- /dev/null +++ b/challenge-003/cliveholloway/README @@ -0,0 +1 @@ +Solution by Clive Holloway diff --git a/challenge-003/cliveholloway/perl5/ch-2.pl b/challenge-003/cliveholloway/perl5/ch-2.pl new file mode 100755 index 0000000000..be67bd4673 --- /dev/null +++ b/challenge-003/cliveholloway/perl5/ch-2.pl @@ -0,0 +1,23 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use v5.012; + +my @out=([1],[1,1]); + +for (3..$ARGV[0]) { + my @new_row=(1,@{$out[-1]}); + for (1..$#{$out[-1]}) { + $new_row[$_] = $out[-1][$_-1] + $out[-1][$_]; + } + push @out, \@new_row; +} + +# format data for output - obviously you'll run out of terminal at some point, +# so, this is just a pretty demo output +my $longest_length = length("@{$out[-1]}"); +for (0..$#out) { + $out[$_] = "@{$out[$_]}"; + my $this_length = length($out[$_]); + say ' 'x(($longest_length-$this_length)/2).$out[$_]; +} diff --git a/challenge-003/daniel-mantovani/perl5/ch-1.pl b/challenge-003/daniel-mantovani/perl5/ch-1.pl new file mode 100644 index 0000000000..fa23ce0075 --- /dev/null +++ b/challenge-003/daniel-mantovani/perl5/ch-1.pl @@ -0,0 +1,38 @@ +#!/usr/bin/perl +use strict; +use warnings; +use v5.10; + +my $q = $ARGV[0]; + +die + "Usage: perl $0 <n>\n\nwhere <n> is the (integer) quantity of hamming numbers you ask" + unless $q && $q =~ /^\d+$/; + +my @primes = (2, 3, 5); + +# for each prime, we will define one vector (array), whith just one element [1] +# note that [1] will be first hamming code inside each array! + +my %vec; +$vec{$_} = [1] for @primes; + +for my $i (1 .. $q) { + +# just look for the min value from the head of v2, v3 or v5 +# (without using List::Util, as the challenge encourages no to use external modules) + my $h; + map { $h = $vec{$_}[0] unless $h && $h < $vec{$_}[0] } @primes; + + # and we just got the next hamming number! + say "$i\t", $h; + + for my $f (@primes) { # now for every prime factor + # remove this value on each vector where it is present + shift @{$vec{$f}} if $vec{$f}[0] == $h; + + # and finally calculate and insert new values to each vector + # note that new pushed value is higer than any previous value in each vector + push @{$vec{$f}}, $h * $f; + } +} diff --git a/challenge-003/duncan-c-white/README b/challenge-003/duncan-c-white/README index 81f31dc7c0..d6a6cb2f04 100644 --- a/challenge-003/duncan-c-white/README +++ b/challenge-003/duncan-c-white/README @@ -1 +1,8 @@ -Solution by Duncan C. White +I have investigated Challenge 1 (the Regular numbers) reasonably thoroughly, +building and comparing 4 iterative solutions to generate them. I also +investigated a Lazy List version, storing the tail of the list as a promise - +a function to call when you wanted the next head item, and the returned tail +is another promise - to generate the next item later when needed. However, +I couldn't get that to work so abandoned it:-) + +Challenge 2 (Pascal's Triangle) by contrast is pretty basic and simple-minded. diff --git a/challenge-003/duncan-c-white/perl5/ch-1.pl b/challenge-003/duncan-c-white/perl5/ch-1.pl new file mode 100755 index 0000000000..3e6cf63ed4 --- /dev/null +++ b/challenge-003/duncan-c-white/perl5/ch-1.pl @@ -0,0 +1,220 @@ +#!/usr/bin/perl +# +# iterative generation of the first N regular numbers +# (numbers of the form 2^i.3^j.5^k) +# in ascending order, using 4 different algorithms. +# +# This program takes two optional arguments: +# ch-1-pl [benchmark [firstN]] +# where benchmark defaults to 0, and firstN to 1000. +# - If benchmark is 0, we run each algorithm in turn +# and minimally checks that each algorithm generates +# the same list. +# - If benchmark is any non-zero value (+ or -), each +# algorithm is Benchmarked with the $benchmark value +# as Benchmark::timethese's first parameter. Thus, +# a +ve value means "benchmark this many iterations", +# while a -ve value means "benchmark for abs(this) seconds". +# +# So, for example: +# ./ch-1.pl 0 +# runs all 4 algorithms, telling you whether each result is the same.. +# but: +# ./ch-1.pl -60 +# runs all 4 algorithms repeatedly under Benchmark, and reports: +# Benchmark: running v1, v2, v3, v4 for at least 60 CPU seconds... +# v1: 69 wallclock secs (68.38 usr + 0.00 sys = 68.38 CPU) @ 0.06/s (n=4) +# v2: 64 wallclock secs (63.17 usr + 0.00 sys = 63.17 CPU) @ 26.29/s (n=1661) +# v3: 62 wallclock secs (62.54 usr + 0.00 sys = 62.54 CPU) @ 45.24/s (n=2829) +# v4: 63 wallclock secs (63.38 usr + 0.00 sys = 63.38 CPU) @ 127.90/s (n=8106) + +use strict; +use warnings; + +use Benchmark qw(:all) ; + +# +# my @result = firstn_regnos_v1( $n ); +# Generate the first N regular numbers via basic GENERATE AND TEST: +# generate every integer, then check "is it 2^i.3^j.5^k for any i,j,k"? +# Returns a list of the first N regular numbers. +# +sub firstn_regnos_v1 +{ + my( $n ) = @_; + my @result = (1); + for( my $x=2; @result<$n; $x++ ) + { + my $y=$x; + $y/=5 while $y%5==0; + $y/=3 while $y%3==0; + $y/=2 while $y%2==0; + push @result, $x if $y==1; + } + return @result; +} + + +# +# my @result = firstn_regnos_v2( $n ); +# Generate the first N regular numbers via a todo set +# of higher 2^i.3^j.5^k values that we haven't yet +# checked, and sorting the todo list to pick the smallest +# every time. Returns a list of the first N regular numbers. +# +sub firstn_regnos_v2 +{ + my( $n ) = @_; + my @result = (1); + my %todo = map { $_ => 1 } qw(2 3 5); + for( my $i=1; $i<$n; $i++ ) + { + my @todo = sort { $a <=> $b } keys %todo; + my $next = shift @todo; + push @result, $next; + # update the todo set: next is done, so remove it + delete $todo{$next}; + # update the todo set: add 2*next, 3*nect and 5*next + $todo{2*$next}++; + $todo{3*$next}++; + $todo{5*$next}++; + } + return @result; +} + + +# +# my @result = firstn_regnos_v3( $n ); +# Generate the first N regular numbers via a todo set +# of higher 2^i.3^j.5^k values that we haven't yet +# checked. It's the same as _v2 EXCEPT that we pick the +# smallest element in a different and more efficient way - +# by searching linearly through the todo set - instead of +# forming a list and sorting it, every time. +# Returns a list of the first N regular numbers. +# +sub firstn_regnos_v3 +{ + my( $n ) = @_; + my @result = (1); + my %v = map { $_ => 1 } qw(2 3 5); + for( my $i=1; $i<$n; $i++ ) + { + my $min = each %v; + while( my $v = each %v ) + { + $min = $v if $min>$v; + } + my $next = $min; + push @result, $next; + delete $v{$next}; + $v{2*$next}++; + $v{3*$next}++; + $v{5*$next}++; + } + return @result; +} + + +# +# my @result = firstn_regnos_v4( $firstn ); +# Generate the first $firstn regular numbers via a more +# constrained "todo set" that never has both than +# one term i-j-X or i-X-k or X-j-k in. This is faster +# and uses less memory. +# Returns a list of the first $firstn regular numbers. +# +sub firstn_regnos_v4 +{ + my( $firstn ) = @_; + my @result = (); + my @next = ( [ 1,0,0,0 ] ); # list of N-i-j-k 4-tuples + my %haveij; # distinct "i-j" pairs in @next + my %haveik; # distinct "i-k" pairs + my %havejk; # distinct "j-k" pairs + for( my $a=1; $a<=$firstn; $a++ ) + { + my $tuple = shift @next; + my( $n, $i, $j, $k ) = @$tuple; + #print "debug: result n=$n, i=$i, j=$j, k=$k\n"; + push @result, $n; + # remove i-j-k from have* now that n-i-j-k is done + delete $haveij{"$i-$j"}; + delete $haveik{"$i-$k"}; + delete $havejk{"$j-$k"}; + # consider each of (i+1,j,k), (i,j+1,k) and (i,j,k+1) + foreach my $newt ( [$n*2, $i+1, $j, $k], + [$n*3, $i, $j+1, $k], + [$n*5, $i, $j, $k+1] ) + { + my( $newn, $x, $y, $z ) = @$newt; + #print "debug: found possible next n=$newn, x=$x, y=$y, z=$z\n"; + + # skip newn-x-y-z if we already known x-y-? or x-?-z or ?-x-z + # as this is a higher power combination that we will discover later + next if $haveij{"$x-$y"} || $haveik{"$x-$z"} || $havejk{"$y-$z"}; + + # ok, so newn-x-y-z is genuinely new: add it, first to have* + $haveij{"$x-$y"}++; + $haveik{"$x-$z"}++; + $havejk{"$y-$z"}++; + #print "debug: found actual next n=$newn, x=$x, y=$y, z=$z\n"; + + # second, insert $newn, $x, $y, $z into @next in sorted-by-N order + # - find first pos s.t. next[pos]>newn + my $pos; + for( $pos=0; $pos < @next && $next[$pos]->[0] < $newn; $pos ++ ) + { + } + # - add new item in at pos $pos: + splice( @next, $pos, 0, $newt ); + } + } + return @result; +} + + +# -------------------------------- Main program -------------------------------- + + +my $benchmark = shift // 0; +my $firstn = shift // 1000; + +if( $benchmark ) +{ + timethese( $benchmark, { + 'v1' => sub { firstn_regnos_v1( $firstn ) }, + 'v2' => sub { firstn_regnos_v2( $firstn ) }, + 'v3' => sub { firstn_regnos_v3( $firstn ) }, + 'v4' => sub { firstn_regnos_v4( $firstn ) }, + }); +} else +{ + print "calculating first $firstn regular numbers by v1 (generate and test):\n"; + my @result = firstn_regnos_v1( $firstn ); + my $s1 = join(',', @result); + print "they are: $s1\n"; + + print "calculating first $firstn regular numbers via v2 (todo+sort):\n"; + @result = firstn_regnos_v2( $firstn ); + my $s2 = join(',', @result); + #print "they are: $s2\n"; + + my $same = $s1 eq $s2 ? "same" : "different"; + print "v1 and v2: $same\n"; + + print "calculating first $firstn regular numbers via v3 (todo+find-smallest):\n"; + @result = firstn_regnos_v3( $firstn ); + my $s3 = join(',', @result); + + $same = $s1 eq $s3 ? "same" : "different"; + print "v1 and v3: $same\n"; + + print "calculating first $firstn regular numbers via v4 (clever):\n"; + @result = firstn_regnos_v4( $firstn ); + my $s4 = join(',', @result); + #print "v4: result $s4\n"; + + $same = $s1 eq $s4 ? "same" : "different"; + print "v1 and v4: $same\n"; +} diff --git a/challenge-003/duncan-c-white/perl5/ch-2.pl b/challenge-003/duncan-c-white/perl5/ch-2.pl new file mode 100755 index 0000000000..5109322548 --- /dev/null +++ b/challenge-003/duncan-c-white/perl5/ch-2.pl @@ -0,0 +1,26 @@ +#!/usr/bin/perl +# +# generate the first N rows of Pascal's triangle, no cleverness, just the +# basic formula. + + +use strict; +use warnings; + +use Function::Parameters; + +my $n = shift // 1000; + +my @curr = (1); # current row +my @next; # next row, one element longer than @curr + +for( my $row=1; $row<=$n; $row++ ) +{ + print "row $row: ".join(',',@curr)."\n"; + @next = ( ); + for( my $i=0; $i<=@curr; $i++ ) + { + $next[$i] = ($i==0?0:$curr[$i-1]) + ($i==@curr?0:$curr[$i]); + } + @curr = @next; +} |
