aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAbigail <abigail@abigail.be>2020-09-23 00:46:07 +0200
committerAbigail <abigail@abigail.be>2020-09-23 00:46:07 +0200
commitb2381734ce615aab75b36f9e426198fbfeffdba2 (patch)
tree96c54f614c854a3a4685a1c27c0b8cbc8fadbfe5
parent1105830edcaaf9ed8924920a0da6128e7873b43f (diff)
downloadperlweeklychallenge-club-b2381734ce615aab75b36f9e426198fbfeffdba2.tar.gz
perlweeklychallenge-club-b2381734ce615aab75b36f9e426198fbfeffdba2.tar.bz2
perlweeklychallenge-club-b2381734ce615aab75b36f9e426198fbfeffdba2.zip
Week 79, part 2, AWK solution.
-rw-r--r--challenge-079/abigail/awk/ch-2.awk60
1 files changed, 60 insertions, 0 deletions
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";
+}