aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-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";
+}