A line chart in SVG is a polyline through scaled points. Build its points attribute.
What to build
A program that writes an SVG polyline for a series of values.
Requirements
- Point i (from 0) has x = padding + i × (W − 2 × padding) ÷ (n − 1).
- Its y is H − padding − (v − min) ÷ (max − min) × (H − 2 × padding), using the series' own minimum and maximum; if they are equal, y = H ÷ 2.
- Print
<polyline points="x1,y1 x2,y2 ..."/>, with every number in:gformat.
Input
Line 1: W, H and padding. Line 2: the values.
Output
One line.
Constraints
2 to 200 values; 2 × padding is less than both W and H.
Example
Input:
100 50 0
0 10 5
Output:
<polyline points="0,50 50,0 100,25"/>
Why it works
Spreading points evenly across the width and scaling values into the padded height places the whole series inside the drawing, and the flip turns rising data into a line that rises on screen.
Examples
100 50 0 0 10 5
<polyline points="0,50 50,0 100,25"/>
20 20 2 1 2
<polyline points="2,18 18,2"/>
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
x goes from padding to W − padding in n − 1 equal steps.
Hint 2
Scale y between the data's own minimum and maximum, flipped, inside the padding.
Hint 3
If all values are equal, put every point at y = H ÷ 2.
Solution
Show one correct solution (try it yourself first)
w, h, pad = map(float, input().split())
values = [float(v) for v in input().split()]
n = len(values)
lo, hi = min(values), max(values)
pts = []
for i, v in enumerate(values):
x = pad + i * (w - 2 * pad) / (n - 1)
if hi == lo:
y = h / 2
else:
y = h - pad - (v - lo) / (hi - lo) * (h - 2 * pad)
pts.append(f"{x:g},{y:g}")
print(f'<polyline points="{" ".join(pts)}"/>')