From 70639de743c7f71a72d88dae613a93b032327c06 Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Sat, 13 Feb 2021 15:52:05 +0000 Subject: Add Awk solution to challenge 001 --- challenge-001/paulo-custodio/awk/ch-1.awk | 31 +++++++++++++++++++++++++++++++ challenge-001/paulo-custodio/awk/ch-2.awk | 17 +++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 challenge-001/paulo-custodio/awk/ch-1.awk create mode 100644 challenge-001/paulo-custodio/awk/ch-2.awk 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..c954b16d73 --- /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) { + for(i in a); + return i +} + +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 +} -- cgit From 49857b9ef8e4b2981187a818f820a53c815350a4 Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Sun, 14 Feb 2021 00:32:22 +0000 Subject: Add Awk solution to challenge 002 --- challenge-001/paulo-custodio/t/test-1.yaml | 7 +--- challenge-001/paulo-custodio/test.pl | 14 ++++--- challenge-002/paulo-custodio/awk/ch-1.awk | 15 +++++++ challenge-002/paulo-custodio/awk/ch-2.awk | 63 ++++++++++++++++++++++++++++++ challenge-002/paulo-custodio/perl/ch-2.pl | 5 ++- challenge-002/paulo-custodio/t/test-1.yaml | 30 ++++++++++++++ challenge-002/paulo-custodio/t/test-2.yaml | 45 +++++++++++++++++++++ challenge-002/paulo-custodio/test.pl | 62 +---------------------------- 8 files changed, 167 insertions(+), 74 deletions(-) create mode 100644 challenge-002/paulo-custodio/awk/ch-1.awk create mode 100644 challenge-002/paulo-custodio/awk/ch-2.awk create mode 100644 challenge-002/paulo-custodio/t/test-1.yaml create mode 100644 challenge-002/paulo-custodio/t/test-2.yaml 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..e746826428 100644 --- a/challenge-001/paulo-custodio/test.pl +++ b/challenge-001/paulo-custodio/test.pl @@ -50,17 +50,18 @@ 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})) { path("out_exp.txt")->spew($spec->{output}); $cmd .= " > out.txt"; } @@ -69,13 +70,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 +104,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..c2af383106 --- /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'; -- cgit From e8a1190ebd8743bf718f9f83321acd9c83f14691 Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Sun, 14 Feb 2021 00:39:36 +0000 Subject: Remove tabs --- challenge-001/paulo-custodio/test.pl | 4 ++-- challenge-002/paulo-custodio/awk/ch-2.awk | 14 +++++++------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/challenge-001/paulo-custodio/test.pl b/challenge-001/paulo-custodio/test.pl index e746826428..2feb090a1e 100644 --- a/challenge-001/paulo-custodio/test.pl +++ b/challenge-001/paulo-custodio/test.pl @@ -50,7 +50,7 @@ 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} + ok eval($spec->{setup}), $spec->{setup} if defined($spec->{setup}); $@ and die $@; @@ -76,7 +76,7 @@ for my $lang (grep {-d} sort keys %LANG) { # run cleaup code if (Test::More->builder->is_passing) { - ok eval($spec->{cleanup}), $spec->{cleanup} + ok eval($spec->{cleanup}), $spec->{cleanup} if defined($spec->{cleanup}); $@ and die $@; unlink("in.txt", "out.txt", "out_exp.txt"); diff --git a/challenge-002/paulo-custodio/awk/ch-2.awk b/challenge-002/paulo-custodio/awk/ch-2.awk index c2af383106..67b75f5c8d 100644 --- a/challenge-002/paulo-custodio/awk/ch-2.awk +++ b/challenge-002/paulo-custodio/awk/ch-2.awk @@ -15,18 +15,18 @@ function format_number(n, base, negative, output, d) { 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 + + return output } function scan_number(str, base, n, negative, ch, d) { @@ -35,7 +35,7 @@ function scan_number(str, base, n, negative, ch, d) { negative = 1 else negative = 0 - + while (str != "") { ch = toupper(substr(str, 1, 1)) str = substr(str, 2) @@ -47,14 +47,14 @@ function scan_number(str, base, n, negative, ch, d) { n = base * n + d } if (negative) - n = -n + n = -n return n } BEGIN { BASE = 35 digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" - + if (ARGV[1] == "-r") print scan_number(ARGV[2], 35) else -- cgit From 2ac65e50b1f8c7ee19e2c6e9ce0ffd7d042bb1bd Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Sun, 14 Feb 2021 02:36:14 +0000 Subject: Add Awk solution to challenge 003 --- challenge-001/paulo-custodio/awk/ch-1.awk | 10 ++-- challenge-001/paulo-custodio/test.pl | 1 + challenge-003/paulo-custodio/awk/ch-1.awk | 65 ++++++++++++++++++++++++ challenge-003/paulo-custodio/awk/ch-2.awk | 65 ++++++++++++++++++++++++ challenge-003/paulo-custodio/t/test-1.yaml | 25 ++++++++++ challenge-003/paulo-custodio/t/test-2.yaml | 15 ++++++ challenge-003/paulo-custodio/test.pl | 80 +----------------------------- 7 files changed, 177 insertions(+), 84 deletions(-) create mode 100644 challenge-003/paulo-custodio/awk/ch-1.awk create mode 100644 challenge-003/paulo-custodio/awk/ch-2.awk create mode 100644 challenge-003/paulo-custodio/t/test-1.yaml create mode 100644 challenge-003/paulo-custodio/t/test-2.yaml 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 < <