diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2019-04-13 23:04:03 +0100 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2019-04-13 23:04:03 +0100 |
| commit | e6b27fc5d6ac336141c6060d890ed304f6ede228 (patch) | |
| tree | 1cda5b114a90a2eeee418f1356ee3817155c200c | |
| parent | 7f605d60af5251ecca9222686d9fa6cb94c43979 (diff) | |
| download | perlweeklychallenge-club-e6b27fc5d6ac336141c6060d890ed304f6ede228.tar.gz perlweeklychallenge-club-e6b27fc5d6ac336141c6060d890ed304f6ede228.tar.bz2 perlweeklychallenge-club-e6b27fc5d6ac336141c6060d890ed304f6ede228.zip | |
- Added solution by Athanasius.
| -rw-r--r-- | challenge-003/athanasius/perl5/ch-1.pl | 42 | ||||
| -rw-r--r-- | challenge-003/athanasius/perl5/ch-2.pl | 26 | ||||
| -rw-r--r-- | challenge-003/athanasius/perl5/one-more-ch-1.pl | 46 |
3 files changed, 114 insertions, 0 deletions
diff --git a/challenge-003/athanasius/perl5/ch-1.pl b/challenge-003/athanasius/perl5/ch-1.pl new file mode 100644 index 0000000000..2ba3cb495f --- /dev/null +++ b/challenge-003/athanasius/perl5/ch-1.pl @@ -0,0 +1,42 @@ +use strict; +use warnings; +use Const::Fast; +use HOP::Stream qw( head merge node tail transform ); + +const my $N => $ARGV[0] // 29; + +$| = 1; + +my $hs = hamming(); +print "\n", head($hs) if $N >= 1; + +for (2 .. $N) +{ + $hs = tail($hs); + print ' ' . head($hs); +} + +print "\n"; + +sub hamming +{ + my $hamming; + + return $hamming = + node + ( + 1, + sub + { + merge + ( + (transform { $_[0] * 2 } $hamming), + merge + ( + (transform { $_[0] * 3 } $hamming), + (transform { $_[0] * 5 } $hamming), + ) + ) + } + ); +} diff --git a/challenge-003/athanasius/perl5/ch-2.pl b/challenge-003/athanasius/perl5/ch-2.pl new file mode 100644 index 0000000000..2ea13d050b --- /dev/null +++ b/challenge-003/athanasius/perl5/ch-2.pl @@ -0,0 +1,26 @@ +use strict; +use warnings; +use Const::Fast; + +const my $MIN_ROWS => 3; +const my $PRINT_WIDTH => 4; + +my $arg = $ARGV[0]; +const my $ROWS => defined($arg) && ($arg >= $MIN_ROWS) ? $arg : $MIN_ROWS; + +display(my $row = [ 1 ]); # First row + +for my $i (2 .. $ROWS) +{ + my $next = [ 1 ]; + push @$next, $row->[$_ - 1] + $row->[$_] for 1 .. $#$row; + push @$next, 1; + display($row = $next); +} + +sub display +{ + my ($row) = @_; + printf '%*d ', $PRINT_WIDTH, $row->[$_] for 0 .. $#$row; + print "\n"; +} diff --git a/challenge-003/athanasius/perl5/one-more-ch-1.pl b/challenge-003/athanasius/perl5/one-more-ch-1.pl new file mode 100644 index 0000000000..bf8a879697 --- /dev/null +++ b/challenge-003/athanasius/perl5/one-more-ch-1.pl @@ -0,0 +1,46 @@ +use strict; +use warnings; +use Const::Fast; + +const my $N => $ARGV[0] // 29; + +$| = 1; + +my @hamming = (1); +my $s2_idx = 0; +my $s3_idx = 0; +my $s5_idx = 0; + +print "\n", $hamming[0] if $N >= 1; + +for (2 .. $N) +{ + my $next_2 = $hamming[$s2_idx] * 2; + my $next_3 = $hamming[$s3_idx] * 3; + my $next_5 = $hamming[$s5_idx] * 5; + + if ($next_2 <= $next_3 && $next_2 <= $next_5) + { + push @hamming, $next_2; + ++$s2_idx; + ++$s3_idx if $next_3 == $next_2; + ++$s5_idx if $next_5 == $next_2; + } + elsif ($next_3 <= $next_2 && $next_3 <= $next_5) + { + push @hamming, $next_3; + ++$s3_idx; + ++$s2_idx if $next_2 == $next_3; + ++$s5_idx if $next_5 == $next_3; + } + else + { + push @hamming, $next_5; + ++$s5_idx; + ++$s2_idx if $next_2 == $next_5; + ++$s3_idx if $next_3 == $next_5; + } + print ' ', $hamming[-1]; +} + +print "\n"; |
