From 30e89ee2fa17fbdda162b4087d83fc1463e8fabf Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Sun, 27 Dec 2020 22:22:17 +0000 Subject: Add Perl solution to challenge 003 --- challenge-003/paulo-custodio/README | 1 + challenge-003/paulo-custodio/perl/ch-1.pl | 37 ++++++++++++++++++++++ challenge-003/paulo-custodio/perl/ch-2.pl | 34 ++++++++++++++++++++ challenge-003/paulo-custodio/test.pl | 52 +++++++++++++++++++++++++++++++ 4 files changed, 124 insertions(+) create mode 100644 challenge-003/paulo-custodio/README create mode 100644 challenge-003/paulo-custodio/perl/ch-1.pl create mode 100644 challenge-003/paulo-custodio/perl/ch-2.pl create mode 100644 challenge-003/paulo-custodio/test.pl diff --git a/challenge-003/paulo-custodio/README b/challenge-003/paulo-custodio/README new file mode 100644 index 0000000000..87dc0b2fbd --- /dev/null +++ b/challenge-003/paulo-custodio/README @@ -0,0 +1 @@ +Solution by Paulo Custodio diff --git a/challenge-003/paulo-custodio/perl/ch-1.pl b/challenge-003/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..d51b7115af --- /dev/null +++ b/challenge-003/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,37 @@ +#!/usr/bin/perl + +# 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. + +use strict; +use warnings; +use 5.030; +use List::Util 'min'; + +# return an iterator to generate the sequence +# the sequence is a merge of all multiples of 2, 3 and 5 +sub hamming_gen { + # sequences of hamming numbers 2*n, 3*n, 5*n + my @seq = ([1], [1], [1]); + my @base = (2, 3, 5); + + return sub { + # get the smallest of the multiples + my $n = min($seq[0][0], $seq[1][0], $seq[2][0]); + for my $i (0..2) { + # shift used multiples + shift @{$seq[$i]} if $seq[$i][0] == $n; + # push next multiple + push @{$seq[$i]}, $n*$base[$i]; + } + return $n; + } +} + +my($n) = @ARGV; +my $hamming = hamming_gen(); +for (1 .. $n) { + say $hamming->(); +} diff --git a/challenge-003/paulo-custodio/perl/ch-2.pl b/challenge-003/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..132a230e03 --- /dev/null +++ b/challenge-003/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,34 @@ +#!/usr/bin/perl + +# 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. + +use strict; +use warnings; +use 5.030; + +my($rows) = @ARGV; + +draw_pascal($rows); + +sub draw_pascal { + my($rows) = @_; + my @row = (1); + say " " x $rows, "@row"; + for my $row (1 .. $rows-1) { + # compute next row + my @next = (1); + for my $i (0 .. $#row-1) { + push @next, $row[$i]+$row[$i+1]; + } + push @next, 1; + @row = @next; + + say " " x ($rows-$row), "@row"; + } +} + + + \ No newline at end of file diff --git a/challenge-003/paulo-custodio/test.pl b/challenge-003/paulo-custodio/test.pl new file mode 100644 index 0000000000..8c00c3b3c1 --- /dev/null +++ b/challenge-003/paulo-custodio/test.pl @@ -0,0 +1,52 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use Test::More; +use 5.030; + +is capture("perl perl/ch-1.pl 20"), <