Data Visualization: A line chart on a grid

Draw a time series on a character grid, the way a plotting library places points.

What to build

A program that draws a series as a text line chart.

Requirements

  • Each value's row is int((v − lo) / (hi − lo) × (H − 1) + 0.5), or 0 if all values are equal.
  • Print H rows from the top: the label (the largest value on the top row, the smallest on the bottom row, nothing on other rows) right-aligned to the wider of the two labels, then |, then one character per value: * if the value is on that row, . otherwise.
  • Then print the axis: the label width in spaces, +, and one - per value.

Input

Line 1: H. Line 2: the values separated by spaces.

Output

H + 1 lines.

Constraints

2 ≤ H ≤ 20; 1 to 60 values.

Example

Input:

4
1 3 2 4

Output:

4 |...*
  |.*..
  |..*.
1 |*...
  +----

Why it works

Mapping between the smallest and largest value uses every row of the grid, and printing the top row first matches how charts are read, with larger values higher up.

Examples

Example 1 · Input
4
1 3 2 4
Expected output
4 |...*
  |.*..
  |..*.
1 |*...
  +----
Example 2 · Input
3
5 5
Expected output
5 |..
  |..
5 |**
  +--

Prompts inside input() are ignored when checking, so input("n: ") is fine.

Hints

Hint 1

Compute each value's row once; then print the rows from height − 1 down to 0.

Hint 2

The label column is as wide as the longer of the two labels, written with :g.

Solution

Show one correct solution (try it yourself first)
h = int(input())
values = [float(v) for v in input().split()]
lo, hi = min(values), max(values)
rows = [0 if hi == lo else int((v - lo) / (hi - lo) * (h - 1) + 0.5) for v in values]
top, bottom = f"{hi:g}", f"{lo:g}"
w = max(len(top), len(bottom))
for level in range(h - 1, -1, -1):
    label = top if level == h - 1 else bottom if level == 0 else ""
    marks = "".join("*" if r == level else "." for r in rows)
    print(f"{label:>{w}} |{marks}")
print(" " * w + " +" + "-" * len(values))

Lesson: Line charts and sparklines →