aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-001/paulo-custodio/awk/ch-1.awk10
-rw-r--r--challenge-001/paulo-custodio/test.pl1
-rw-r--r--challenge-003/paulo-custodio/awk/ch-1.awk65
-rw-r--r--challenge-003/paulo-custodio/awk/ch-2.awk65
-rw-r--r--challenge-003/paulo-custodio/t/test-1.yaml25
-rw-r--r--challenge-003/paulo-custodio/t/test-2.yaml15
-rw-r--r--challenge-003/paulo-custodio/test.pl80
7 files changed, 177 insertions, 84 deletions
diff --git a/challenge-001/paulo-custodio/awk/ch-1.awk b/challenge-001/paulo-custodio/awk/ch-1.awk
index c954b16d73..da8e38182a 100644
--- a/challenge-001/paulo-custodio/awk/ch-1.awk
+++ b/challenge-001/paulo-custodio/awk/ch-1.awk
@@ -7,8 +7,7 @@
# ‘Perl Weekly Challenge’. Also print the number of times the character ‘e’
# is found in the string.
-function join(array, start, end, sep, result, i)
-{
+function join(array, start, end, sep, result, i) {
if (sep == "")
sep = " "
else if (sep == SUBSEP) # magic value
@@ -19,9 +18,10 @@ function join(array, start, end, sep, result, i)
return result
}
-function alen(a, i) {
- for(i in a);
- return i
+function alen(a, i, k) {
+ k = 0
+ for(i in a) k++
+ return k
}
BEGIN {
diff --git a/challenge-001/paulo-custodio/test.pl b/challenge-001/paulo-custodio/test.pl
index 2feb090a1e..41c54fdb5e 100644
--- a/challenge-001/paulo-custodio/test.pl
+++ b/challenge-001/paulo-custodio/test.pl
@@ -62,6 +62,7 @@ for my $lang (grep {-d} sort keys %LANG) {
$cmd .= " < in.txt";
}
if (defined($spec->{output})) {
+ $spec->{output} =~ s/^\|//mg; # delete initial bar
path("out_exp.txt")->spew($spec->{output});
$cmd .= " > out.txt";
}
diff --git a/challenge-003/paulo-custodio/awk/ch-1.awk b/challenge-003/paulo-custodio/awk/ch-1.awk
new file mode 100644
index 0000000000..12c7cea74a
--- /dev/null
+++ b/challenge-003/paulo-custodio/awk/ch-1.awk
@@ -0,0 +1,65 @@
+#!/usr/bin/gawk
+
+# 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.
+
+function min(a, b) {
+ return (a<b) ? a : b;
+}
+
+function min3(a, b, c) {
+ return min(a, min(b, c))
+}
+
+function alen(a, i, k) {
+ k = 0
+ for(i in a) k++
+ return k
+}
+
+function push(a, v, n) {
+ n = alen(a)
+ while (n in a) n++
+ a[n] = v
+}
+
+function shift(a, i, v) {
+ v = a[0]
+ for (i=1; i<alen(a); i++)
+ a[i-1] = a[i]
+ delete a[alen(a)-1]
+ return v
+}
+
+function hamming( n) {
+ # get the smallest of the multiples
+ n = min3(seq2[0], seq3[0], seq5[0])
+
+ # shift used multiples
+ if (seq2[0] == n) shift(seq2)
+ if (seq3[0] == n) shift(seq3)
+ if (seq5[0] == n) shift(seq5)
+
+ # push next multiple
+ push(seq2, n*2)
+ push(seq3, n*3)
+ push(seq5, n*5)
+
+ return n
+}
+
+BEGIN {
+ # sequences of hamming numbers 2*n, 3*n, 5*n
+ seq2[0] = 1
+ seq3[0] = 1
+ seq5[0] = 1
+
+ n = ARGV[1] ? ARGV[1] : 5
+ for (i=0; i<n; i++)
+ print hamming()
+ exit(0)
+}
diff --git a/challenge-003/paulo-custodio/awk/ch-2.awk b/challenge-003/paulo-custodio/awk/ch-2.awk
new file mode 100644
index 0000000000..ab7703737c
--- /dev/null
+++ b/challenge-003/paulo-custodio/awk/ch-2.awk
@@ -0,0 +1,65 @@
+#!/usr/bin/gawk
+
+# 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.
+
+function alen(a, i, k) {
+ k = 0
+ for(i in a) k++
+ return k
+}
+
+function push(a, v, n) {
+ n = alen(a)
+ while (n in a) n++
+ a[n] = v
+}
+
+function spaces(n, i, output) {
+ output = ""
+ for (i = 0; i < n; i++)
+ output = output " "
+ return output
+}
+
+function join(array, start, end, sep, result, i) {
+ if (sep == "")
+ sep = " "
+ else if (sep == SUBSEP) # magic value
+ sep = ""
+ result = array[start]
+ for (i = start + 1; i <= end; i++)
+ result = result sep array[i]
+ return result
+}
+
+function draw_pascal(rows, row, r, i, nrow) {
+ row[0] = 1
+ print spaces(rows-1) join(row, 0, alen(row)-1, " ")
+ for (r = 1; r < rows; r++) {
+ # compute next row
+ for (i in nrow) delete nrow[i]
+ nrow[0] = 1
+ for (i = 0; i < alen(row)-1; i++)
+ push(nrow, row[i]+row[i+1])
+ push(nrow, 1)
+
+ # swap row
+ for (i in nrow)
+ row[i] = nrow[i]
+
+ # print
+ print spaces(rows-r-1) join(row, 0, alen(row)-1, " ")
+ }
+}
+
+BEGIN {
+ n = ARGV[1] ? ARGV[1] : 5
+ draw_pascal(n)
+ exit(0)
+
+}
diff --git a/challenge-003/paulo-custodio/t/test-1.yaml b/challenge-003/paulo-custodio/t/test-1.yaml
new file mode 100644
index 0000000000..1bef1a5dee
--- /dev/null
+++ b/challenge-003/paulo-custodio/t/test-1.yaml
@@ -0,0 +1,25 @@
+- setup:
+ cleanup:
+ args: 20
+ input:
+ output: |
+ 1
+ 2
+ 3
+ 4
+ 5
+ 6
+ 8
+ 9
+ 10
+ 12
+ 15
+ 16
+ 18
+ 20
+ 24
+ 25
+ 27
+ 30
+ 32
+ 36
diff --git a/challenge-003/paulo-custodio/t/test-2.yaml b/challenge-003/paulo-custodio/t/test-2.yaml
new file mode 100644
index 0000000000..4f3bc74165
--- /dev/null
+++ b/challenge-003/paulo-custodio/t/test-2.yaml
@@ -0,0 +1,15 @@
+- setup:
+ cleanup:
+ args: 10
+ input:
+ output: |
+ | 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
diff --git a/challenge-003/paulo-custodio/test.pl b/challenge-003/paulo-custodio/test.pl
index 7884fcb0e4..a61c28ebb7 100644
--- a/challenge-003/paulo-custodio/test.pl
+++ b/challenge-003/paulo-custodio/test.pl
@@ -5,82 +5,4 @@ use warnings;
use Test::More;
use 5.030;
-my $LUA = ($^O eq 'msys') ? "lua.exe" : "lua";
-
-run("gcc c/ch-1.c -o c/ch-1");
-run("g++ cpp/ch-1.cpp -o cpp/ch-1");
-run("fbc basic/ch-1.bas -o basic/ch-1");
-
-for ([20 => <<END]) {
-1
-2
-3
-4
-5
-6
-8
-9
-10
-12
-15
-16
-18
-20
-24
-25
-27
-30
-32
-36
-END
- my($in, $out) = @$_;
-
- is capture( "$LUA lua/ch-1.lua $in"), $out;
- is capture( "perl perl/ch-1.pl $in"), $out;
- is capture( "gforth forth/ch-1.fs $in"), $out;
- is capture("python python/ch-1.py $in"), $out;
- is capture( "c/ch-1 $in"), $out;
- is capture( "cpp/ch-1 $in"), $out;
- is capture( "basic/ch-1 $in"), $out;
-}
-
-run("gcc c/ch-2.c -o c/ch-2");
-run("g++ cpp/ch-2.cpp -o cpp/ch-2");
-run("fbc basic/ch-2.bas -o basic/ch-2");
-
-for ([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
- my($in, $out) = @$_;
-
- is capture( "$LUA lua/ch-2.lua $in "), $out;
- is capture( "perl perl/ch-2.pl $in "), $out;
- is capture( "gforth forth/ch-2.fs $in "), $out;
- is capture( "python python/ch-2.py $in "), $out;
- is capture( "c/ch-2 $in "), $out;
- is capture( "cpp/ch-2 $in "), $out;
- is capture( "basic/ch-2 $in "), $out;
-}
-
-done_testing;
-
-sub capture {
- my($cmd) = @_;
- my $out = `$cmd`;
- $out =~ s/[ \t\v\f\r]*\n/\n/g;
- return $out;
-}
-
-sub run {
- my($cmd) = @_;
- ok 0==system($cmd), $cmd;
-}
+require '../../challenge-001/paulo-custodio/test.pl';