diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2021-02-14 00:32:22 +0000 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2021-02-14 00:32:22 +0000 |
| commit | 49857b9ef8e4b2981187a818f820a53c815350a4 (patch) | |
| tree | f3b7b636b8d4aa07217ea631915b4b255e9218e0 | |
| parent | 70639de743c7f71a72d88dae613a93b032327c06 (diff) | |
| download | perlweeklychallenge-club-49857b9ef8e4b2981187a818f820a53c815350a4.tar.gz perlweeklychallenge-club-49857b9ef8e4b2981187a818f820a53c815350a4.tar.bz2 perlweeklychallenge-club-49857b9ef8e4b2981187a818f820a53c815350a4.zip | |
Add Awk solution to challenge 002
| -rw-r--r-- | challenge-001/paulo-custodio/t/test-1.yaml | 7 | ||||
| -rw-r--r-- | challenge-001/paulo-custodio/test.pl | 14 | ||||
| -rw-r--r-- | challenge-002/paulo-custodio/awk/ch-1.awk | 15 | ||||
| -rw-r--r-- | challenge-002/paulo-custodio/awk/ch-2.awk | 63 | ||||
| -rw-r--r-- | challenge-002/paulo-custodio/perl/ch-2.pl | 5 | ||||
| -rw-r--r-- | challenge-002/paulo-custodio/t/test-1.yaml | 30 | ||||
| -rw-r--r-- | challenge-002/paulo-custodio/t/test-2.yaml | 45 | ||||
| -rw-r--r-- | challenge-002/paulo-custodio/test.pl | 62 |
8 files changed, 167 insertions, 74 deletions
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'; |
