Data Visualization: Place values on a log axis

Values that span many powers of ten need a logarithmic axis. Compute where each value goes, and draw the axis as text.

What to build

A program that places values on a log axis W characters wide.

Requirements

  • lo = 10^floor(log10 m) for the smallest positive value m, hi = 10^ceil(log10 M) for the largest value M; if lo equals hi, multiply hi by 10.
  • Print decades: and every power of ten from lo to hi, written with :g. Then, for each value in input order, V at P or, for a value that is zero or negative, V cannot be shown on a log scale (V written as in the input).
  • Finally print the axis: W characters of -, with | at each decade's position and * at each value's position (a * replaces a |).

Input

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

Output

The decades line, one line per value, and the axis line.

Constraints

5 ≤ W ≤ 80; at least one value is positive; up to 50 values.

Example

Input:

31
1 10 100 1000 50

Output:

decades: 1 10 100 1000
1 at 0
10 at 10
100 at 20
1000 at 30
50 at 17
*---------*------*--*---------*

Why it works

On a log scale equal distances are equal ratios, so the decades are evenly spaced and a value's position depends on its logarithm; zero and negative values have no logarithm and cannot appear.

Examples

Example 1 · Input
31
1 10 100 1000 50
Expected output
decades: 1 10 100 1000
1 at 0
10 at 10
100 at 20
1000 at 30
50 at 17
*---------*------*--*---------*
Example 2 · Input
21
3 -2 0 250
Expected output
decades: 1 10 100 1000
3 at 3
-2 cannot be shown on a log scale
0 cannot be shown on a log scale
250 at 16
|--*---|-----|--*---|

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

Hints

Hint 1

The axis runs from the power of ten at or below the smallest positive value to the power of ten at or above the largest.

Hint 2

The position of v is (log10 v − log10 lo) / (log10 hi − log10 lo) × (W − 1), rounded half up with int(x + 0.5).

Solution

Show one correct solution (try it yourself first)
import math

w = int(input())
texts = input().split()
values = [float(t) for t in texts]
positive = [v for v in values if v > 0]
lo = 10 ** math.floor(math.log10(min(positive)))
hi = 10 ** math.ceil(math.log10(max(positive)))
if lo == hi:
    hi *= 10
span = math.log10(hi) - math.log10(lo)


def position(v):
    return int((math.log10(v) - math.log10(lo)) / span * (w - 1) + 0.5)


decades = []
d = lo
while d <= hi * 1.0000001:
    decades.append(d)
    d *= 10
print("decades: " + " ".join(f"{d:g}" for d in decades))
axis = ["-"] * w
for d in decades:
    axis[position(d)] = "|"
for text, v in zip(texts, values):
    if v <= 0:
        print(f"{text} cannot be shown on a log scale")
    else:
        p = position(v)
        axis[p] = "*"
        print(f"{text} at {p}")
print("".join(axis))

Lesson: Scales, axes and nice ticks →