aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-003/paulo-custodio/README1
-rw-r--r--challenge-003/paulo-custodio/perl/ch-1.pl37
-rw-r--r--challenge-003/paulo-custodio/perl/ch-2.pl34
-rw-r--r--challenge-003/paulo-custodio/test.pl52
4 files changed, 124 insertions, 0 deletions
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"), <<END;
+1
+2
+3
+4
+5
+6
+8
+9
+10
+12
+15
+16
+18
+20
+24
+25
+27
+30
+32
+36
+END
+
+is capture("perl perl/ch-2.pl 10"), <<END;
+ 1
+ 1 1
+ 1 2 1
+ 1 3 3 1
+ 1 4 6 4 1
+ 1 5 10 10 5 1
+ 1 6 15 20 15 6 1
+ 1 7 21 35 35 21 7 1
+ 1 8 28 56 70 56 28 8 1
+ 1 9 36 84 126 126 84 36 9 1
+END
+
+
+done_testing;
+
+sub capture {
+ my($cmd) = @_;
+ my $out = `$cmd`;
+ $out =~ s/[ \t\v\f\r]*\n/\n/g;
+ return $out;
+}