diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2021-02-14 16:32:59 +0000 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2021-02-14 16:32:59 +0000 |
| commit | ef14c20e2e2d61abe4cc73e5389733122df02d42 (patch) | |
| tree | d4346068d07aac04bae6094c595e1350fa6fe2c2 /challenge-002 | |
| parent | 40abdaf0147a405f36b5d43d7919eda15e131a3d (diff) | |
| download | perlweeklychallenge-club-ef14c20e2e2d61abe4cc73e5389733122df02d42.tar.gz perlweeklychallenge-club-ef14c20e2e2d61abe4cc73e5389733122df02d42.tar.bz2 perlweeklychallenge-club-ef14c20e2e2d61abe4cc73e5389733122df02d42.zip | |
Add Awk solution to challenge 004
Fix Awk syntax on previouws submissions - semicolon is needed at the end of statements
Diffstat (limited to 'challenge-002')
| -rw-r--r-- | challenge-002/paulo-custodio/awk/ch-1.awk | 8 | ||||
| -rw-r--r-- | challenge-002/paulo-custodio/awk/ch-2.awk | 42 |
2 files changed, 25 insertions, 25 deletions
diff --git a/challenge-002/paulo-custodio/awk/ch-1.awk b/challenge-002/paulo-custodio/awk/ch-1.awk index c5adaa5282..1db88fd0eb 100644 --- a/challenge-002/paulo-custodio/awk/ch-1.awk +++ b/challenge-002/paulo-custodio/awk/ch-1.awk @@ -6,10 +6,10 @@ # Write a script or one-liner to remove leading zeros from positive numbers. BEGIN { - num = ARGV[1] + num = ARGV[1]; if (match(num, /^0*([0-9]+)/, capture)) - print capture[1] + print capture[1]; else - print num - exit 0 + print num; + exit 0; } diff --git a/challenge-002/paulo-custodio/awk/ch-2.awk b/challenge-002/paulo-custodio/awk/ch-2.awk index 67b75f5c8d..752f93173b 100644 --- a/challenge-002/paulo-custodio/awk/ch-2.awk +++ b/challenge-002/paulo-custodio/awk/ch-2.awk @@ -9,55 +9,55 @@ function format_number(n, base, negative, output, d) { if (n < 0) { - negative = 1 + negative = 1; n = -n } else { - negative = 0 + negative = 0; } output = "" do { - d = n % base - n = int(n / base) - output = substr(digits, d+1, 1) output + d = n % base; + n = int(n / base); + output = substr(digits, d+1, 1) output; } while (n > 0) if (negative) - output = "-" output + output = "-" output; - return output + return output; } function scan_number(str, base, n, negative, ch, d) { n = 0; if (sub(/^-/, "", str)) - negative = 1 + negative = 1; else - negative = 0 + negative = 0; while (str != "") { - ch = toupper(substr(str, 1, 1)) - str = substr(str, 2) - d = index(digits, ch) - 1 + 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) + print "cannot parse '" str "'"; + exit(1); } - n = base * n + d + n = base * n + d; } if (negative) - n = -n - return n + n = -n; + return n; } BEGIN { - BASE = 35 - digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" + BASE = 35; + digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; if (ARGV[1] == "-r") - print scan_number(ARGV[2], 35) + print scan_number(ARGV[2], 35); else - print format_number(ARGV[1], 35) + print format_number(ARGV[1], 35); exit 0 } |
