aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-02-14 04:12:31 +0000
committerGitHub <noreply@github.com>2021-02-14 04:12:31 +0000
commit7dd024035784f559242fa2005f7042eeb3b93b65 (patch)
tree59a0a1620f0eb4ce05f82559e98d7914d1e9a91b
parent20dd7156a87fc8cf3da0c10d8614fcaa389d8148 (diff)
parent2ac65e50b1f8c7ee19e2c6e9ce0ffd7d042bb1bd (diff)
downloadperlweeklychallenge-club-7dd024035784f559242fa2005f7042eeb3b93b65.tar.gz
perlweeklychallenge-club-7dd024035784f559242fa2005f7042eeb3b93b65.tar.bz2
perlweeklychallenge-club-7dd024035784f559242fa2005f7042eeb3b93b65.zip
Merge pull request #3512 from pauloscustodio/099
099
-rw-r--r--challenge-001/paulo-custodio/awk/ch-1.awk31
-rw-r--r--challenge-001/paulo-custodio/awk/ch-2.awk17
-rw-r--r--challenge-001/paulo-custodio/t/test-1.yaml7
-rw-r--r--challenge-001/paulo-custodio/test.pl15
-rw-r--r--challenge-002/paulo-custodio/awk/ch-1.awk15
-rw-r--r--challenge-002/paulo-custodio/awk/ch-2.awk63
-rw-r--r--challenge-002/paulo-custodio/perl/ch-2.pl5
-rw-r--r--challenge-002/paulo-custodio/t/test-1.yaml30
-rw-r--r--challenge-002/paulo-custodio/t/test-2.yaml45
-rw-r--r--challenge-002/paulo-custodio/test.pl62
-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
15 files changed, 387 insertions, 153 deletions
diff --git a/challenge-001/paulo-custodio/awk/ch-1.awk b/challenge-001/paulo-custodio/awk/ch-1.awk
new file mode 100644
index 0000000000..da8e38182a
--- /dev/null
+++ b/challenge-001/paulo-custodio/awk/ch-1.awk
@@ -0,0 +1,31 @@
+#!/usr/bin/gawk
+
+# Challenge 001
+#
+# Challenge #1
+# Write a script to replace the character ‘e’ with ‘E’ in the string
+# ‘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) {
+ 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 alen(a, i, k) {
+ k = 0
+ for(i in a) k++
+ return k
+}
+
+BEGIN {
+ text = join(ARGV, 1, alen(ARGV))
+ print gsub(/e/, "E", text) " " text
+ exit 0
+}
diff --git a/challenge-001/paulo-custodio/awk/ch-2.awk b/challenge-001/paulo-custodio/awk/ch-2.awk
new file mode 100644
index 0000000000..f4c9a9fafb
--- /dev/null
+++ b/challenge-001/paulo-custodio/awk/ch-2.awk
@@ -0,0 +1,17 @@
+#!/usr/bin/gawk
+
+# Challenge 001
+#
+# Challenge #2
+# Write a one-liner to solve the FizzBuzz problem and print the numbers 1
+# through 20. However, any number divisible by 3 should be replaced by the word
+# ‘fizz’ and any divisible by 5 by the word ‘buzz’. Those numbers that are both
+# divisible by 3 and 5 become ‘fizzbuzz’.
+
+BEGIN {
+ num = ARGV[1] ? ARGV[1] : 20
+ for (i=1; i<=num; i++) {
+ print (i%15)==0 ? "fizzbuzz" : (i%3)==0 ? "fizz" : (i%5)==0 ? "buzz" : i
+ }
+ exit 0
+}
diff --git a/challenge-001/paulo-custodio/t/test-1.yaml b/challenge-001/paulo-custodio/t/test-1.yaml
index 4e72c56368..c36262e3b7 100644
--- a/challenge-001/paulo-custodio/t/test-1.yaml
+++ b/challenge-001/paulo-custodio/t/test-1.yaml
@@ -2,12 +2,9 @@
cleanup:
args: Perl Weekly Challenge
input:
- output: |
- 5 PErl WEEkly ChallEngE
-
+ output: 5 PErl WEEkly ChallEngE
- setup:
cleanup:
args: Champion
input:
- output: |
- 0 Champion
+ output: 0 Champion
diff --git a/challenge-001/paulo-custodio/test.pl b/challenge-001/paulo-custodio/test.pl
index 6cc94723ba..41c54fdb5e 100644
--- a/challenge-001/paulo-custodio/test.pl
+++ b/challenge-001/paulo-custodio/test.pl
@@ -50,17 +50,19 @@ for my $lang (grep {-d} sort keys %LANG) {
for my $doc (@$yaml) {
for my $spec (@$doc) {
# run setup code
- ok eval($spec->{setup}), $spec->{setup} if $spec->{setup};
+ ok eval($spec->{setup}), $spec->{setup}
+ if defined($spec->{setup});
$@ and die $@;
# build test command line
my $cmd = "$exec ".($spec->{args} // "");
chomp($cmd);
- if($spec->{input}) {
+ if(defined($spec->{input})) {
path("in.txt")->spew($spec->{input});
$cmd .= " < in.txt";
}
- if ($spec->{output}) {
+ if (defined($spec->{output})) {
+ $spec->{output} =~ s/^\|//mg; # delete initial bar
path("out_exp.txt")->spew($spec->{output});
$cmd .= " > out.txt";
}
@@ -69,13 +71,14 @@ for my $lang (grep {-d} sort keys %LANG) {
run($cmd);
# compare output
- if ($spec->{output}) {
+ if (defined($spec->{output})) {
run("diff -w out_exp.txt out.txt");
}
# run cleaup code
if (Test::More->builder->is_passing) {
- ok eval($spec->{cleanup}), $spec->{cleanup} if $spec->{cleanup};
+ ok eval($spec->{cleanup}), $spec->{cleanup}
+ if defined($spec->{cleanup});
$@ and die $@;
unlink("in.txt", "out.txt", "out_exp.txt");
}
@@ -102,7 +105,7 @@ sub build {
return $exe;
}
if (/awk/) {
- return "gawk -f $prog";
+ return "gawk -f $prog --";
}
if (/basic/) {
run("fbc $prog -o $prog_wo_ext") if (!-f $exe || -M $exe > -M $prog);
diff --git a/challenge-002/paulo-custodio/awk/ch-1.awk b/challenge-002/paulo-custodio/awk/ch-1.awk
new file mode 100644
index 0000000000..c5adaa5282
--- /dev/null
+++ b/challenge-002/paulo-custodio/awk/ch-1.awk
@@ -0,0 +1,15 @@
+#!/usr/bin/gawk
+
+# Challenge 002
+#
+# Challenge #1
+# Write a script or one-liner to remove leading zeros from positive numbers.
+
+BEGIN {
+ num = ARGV[1]
+ if (match(num, /^0*([0-9]+)/, capture))
+ print capture[1]
+ else
+ print num
+ exit 0
+}
diff --git a/challenge-002/paulo-custodio/awk/ch-2.awk b/challenge-002/paulo-custodio/awk/ch-2.awk
new file mode 100644
index 0000000000..67b75f5c8d
--- /dev/null
+++ b/challenge-002/paulo-custodio/awk/ch-2.awk
@@ -0,0 +1,63 @@
+#!/usr/bin/gawk
+
+# Challenge 002
+#
+# Challenge #2
+# Write a script that can convert integers to and from a base35
+# representation, using the characters 0-9 and A-Y. Dave Jacoby came up
+# with nice description about base35, in case you needed some background.
+
+function format_number(n, base, negative, output, d) {
+ if (n < 0) {
+ negative = 1
+ n = -n
+ }
+ else {
+ negative = 0
+ }
+
+ output = ""
+ do {
+ d = n % base
+ n = int(n / base)
+ output = substr(digits, d+1, 1) output
+ } while (n > 0)
+
+ if (negative)
+ output = "-" output
+
+ return output
+}
+
+function scan_number(str, base, n, negative, ch, d) {
+ n = 0;
+ if (sub(/^-/, "", str))
+ negative = 1
+ else
+ negative = 0
+
+ while (str != "") {
+ ch = toupper(substr(str, 1, 1))
+ str = substr(str, 2)
+ d = index(digits, ch) - 1
+ if (d < 0 || d >= base) {
+ print "cannot parse '" str "'"
+ exit(1)
+ }
+ n = base * n + d
+ }
+ if (negative)
+ n = -n
+ return n
+}
+
+BEGIN {
+ BASE = 35
+ digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
+
+ if (ARGV[1] == "-r")
+ print scan_number(ARGV[2], 35)
+ else
+ print format_number(ARGV[1], 35)
+ exit 0
+}
diff --git a/challenge-002/paulo-custodio/perl/ch-2.pl b/challenge-002/paulo-custodio/perl/ch-2.pl
index e669e5212b..487857b096 100644
--- a/challenge-002/paulo-custodio/perl/ch-2.pl
+++ b/challenge-002/paulo-custodio/perl/ch-2.pl
@@ -11,16 +11,17 @@ use strict;
use warnings;
use 5.030;
+use constant { BASE => 35 };
my @digits = ('0'..'9','A'..'Z');
our $opt_r;
($opt_r = 1, shift) if @ARGV && $ARGV[0] eq '-r';
@ARGV==1 or die "Usage: ch-2.pl [-r] number\n";
if ($opt_r) {
- say scan_number($ARGV[0], 35);
+ say scan_number($ARGV[0], BASE);
}
else {
- say format_number($ARGV[0], 35);
+ say format_number($ARGV[0], BASE);
}
diff --git a/challenge-002/paulo-custodio/t/test-1.yaml b/challenge-002/paulo-custodio/t/test-1.yaml
new file mode 100644
index 0000000000..2c03c83caf
--- /dev/null
+++ b/challenge-002/paulo-custodio/t/test-1.yaml
@@ -0,0 +1,30 @@
+- setup:
+ cleanup:
+ args: 0
+ input:
+ output: 0
+- setup:
+ cleanup:
+ args: 000123
+ input:
+ output: 123
+- setup:
+ cleanup:
+ args: 000123
+ input:
+ output: 123
+- setup:
+ cleanup:
+ args: 123
+ input:
+ output: 123
+- setup:
+ cleanup:
+ args: -000123
+ input:
+ output: -000123
+- setup:
+ cleanup:
+ args: -123
+ input:
+ output: -123
diff --git a/challenge-002/paulo-custodio/t/test-2.yaml b/challenge-002/paulo-custodio/t/test-2.yaml
new file mode 100644
index 0000000000..8c287af768
--- /dev/null
+++ b/challenge-002/paulo-custodio/t/test-2.yaml
@@ -0,0 +1,45 @@
+- setup:
+ cleanup:
+ args: 0
+ input:
+ output: 0
+- setup:
+ cleanup:
+ args: 0
+ input:
+ output: 0
+- setup:
+ cleanup:
+ args: 1
+ input:
+ output: 1
+- setup:
+ cleanup:
+ args: 34
+ input:
+ output: Y
+- setup:
+ cleanup:
+ args: -35
+ input:
+ output: -10
+- setup:
+ cleanup:
+ args: -r 0
+ input:
+ output: 0
+- setup:
+ cleanup:
+ args: -r 1
+ input:
+ output: 1
+- setup:
+ cleanup:
+ args: -r Y
+ input:
+ output: 34
+- setup:
+ cleanup:
+ args: -r -10
+ input:
+ output: -35
diff --git a/challenge-002/paulo-custodio/test.pl b/challenge-002/paulo-custodio/test.pl
index f137453f23..a61c28ebb7 100644
--- a/challenge-002/paulo-custodio/test.pl
+++ b/challenge-002/paulo-custodio/test.pl
@@ -5,64 +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 ([ "0" => "0"],
- ["000123" => "123"],
- ["123" => "123"],
- ["-00123" => "-00123"],
- ["-123" => "-123"]) {
- my($in, $out) = @$_;
-
- is capture( "$LUA lua/ch-1.lua $in"), "$out\n";
- is capture( "perl perl/ch-1.pl $in"), "$out\n";
- is capture( "gforth forth/ch-1.fs $in"), "$out\n";
- is capture("python python/ch-1.py $in"), "$out\n";
- is capture( "c/ch-1 $in"), "$out\n";
- is capture( "cpp/ch-1 $in"), "$out\n";
- is capture( "basic/ch-1 $in"), "$out\n";
-}
-
-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 ([ "0" => "0"],
- [ "1" => "1"],
- [ "34" => "Y"],
- ["-35" => "-10"]) {
- my($in, $out) = @$_;
-
- is capture( "$LUA lua/ch-2.lua $in "), "$out\n";
- is capture( "$LUA lua/ch-2.lua -r $out"), "$in\n";
- is capture( "perl perl/ch-2.pl $in "), "$out\n";
- is capture( "perl perl/ch-2.pl -r $out"), "$in\n";
- is capture( "gforth forth/ch-2.fs $in "), "$out\n";
- is capture( "gforth forth/ch-2.fs -r $out"), "$in\n";
- is capture( "python python/ch-2.py $in "), "$out\n";
- is capture( "python python/ch-2.py -r $out"), "$in\n";
- is capture( "c/ch-2 $in "), "$out\n";
- is capture( "c/ch-2 -r $out"), "$in\n";
- is capture( "cpp/ch-2 $in "), "$out\n";
- is capture( "cpp/ch-2 -r $out"), "$in\n";
- is capture( "basic/ch-2 $in "), "$out\n";
- is capture( "basic/ch-2 -r $out"), "$in\n";
-}
-
-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';
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';