From b2381734ce615aab75b36f9e426198fbfeffdba2 Mon Sep 17 00:00:00 2001 From: Abigail Date: Wed, 23 Sep 2020 00:46:07 +0200 Subject: Week 79, part 2, AWK solution. --- challenge-079/abigail/awk/ch-2.awk | 60 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 challenge-079/abigail/awk/ch-2.awk diff --git a/challenge-079/abigail/awk/ch-2.awk b/challenge-079/abigail/awk/ch-2.awk new file mode 100644 index 0000000000..164dc88b6f --- /dev/null +++ b/challenge-079/abigail/awk/ch-2.awk @@ -0,0 +1,60 @@ +# +# Challenge: +# +# You are given an array of positive numbers @N. +# Write a script to represent it as Histogram Chart and find out +# how much water it can trap. +# + +{ + # + # First, find the maximum value. + # + max = 0; + for (i = 1; i <= NF; i ++) { + if ($i > max) { + max = $i; + } + } + + # + # Given the value, we know how wide every column must be. + # + format = "%" length (max) "s"; + show = sprintf (" " format, "#"); + noshow = sprintf (" " format, " "); + + # + # Print the histogram + # + for (vol = max; vol; vol --) { + printf format, vol; + for (i = 1; i <= NF; i ++) { + if ($i >= vol) {printf show;} + else {printf noshow;} + } + printf "\n"; + } + + # + # Print the line with the bars. + # + bar = ""; + for (i = 0; i < length (max); i ++) { + bar = bar "_"; + } + printf bar; + for (i = 1; i <= NF; i ++) { + printf " " bar; + } + printf "\n"; + + # + # Print the line with the totals + # + printf format, " "; + for (i = 1; i <= NF; i ++) { + printf " " format, $i; + } + printf "\n"; +} -- cgit