diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2021-02-13 15:52:05 +0000 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2021-02-13 15:52:05 +0000 |
| commit | 70639de743c7f71a72d88dae613a93b032327c06 (patch) | |
| tree | 780265de95d2454c8a97e04a7e8b31b4be150af6 | |
| parent | b3daee64ed733af78f2141d074fcc8ace8071ea2 (diff) | |
| download | perlweeklychallenge-club-70639de743c7f71a72d88dae613a93b032327c06.tar.gz perlweeklychallenge-club-70639de743c7f71a72d88dae613a93b032327c06.tar.bz2 perlweeklychallenge-club-70639de743c7f71a72d88dae613a93b032327c06.zip | |
Add Awk solution to challenge 001
| -rw-r--r-- | challenge-001/paulo-custodio/awk/ch-1.awk | 31 | ||||
| -rw-r--r-- | challenge-001/paulo-custodio/awk/ch-2.awk | 17 |
2 files changed, 48 insertions, 0 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..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 +} |
