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