Compare two time series on one shared scale, mark where they overlap, and count how often the lead changes.
What to build
A program that draws two series on one grid and counts their crossings.
Requirements
- lo and hi are taken over both series; each value's row is int((v − lo) / (hi − lo) × (H − 1) + 0.5), or 0 if lo equals hi.
- Print H rows from the top as in a single-series grid (label of hi on the top row, lo on the bottom row, right-aligned to the wider label, then
|), withawhere only the first series is,bwhere only the second is,#where both are, and.elsewhere; then the axis line (spaces,+, one-per point). - Then print
a = NAME1, b = NAME2, # = bothandcrossings: K, where K counts the times the leading series changes: at each point the leader is the larger series (ties have no leader), and a crossing is a leader different from the previous point that had a leader.
Input
Line 1: H. Line 2: NAME1 and its values. Line 3: NAME2 and the same number of values.
Output
H + 3 lines.
Constraints
2 ≤ H ≤ 20; 1 to 60 values per series.
Example
Input:
3
rent 1 2 3 4
food 4 3 2 1
Output:
4 |b..a
|.##.
1 |a..b
+----
a = rent, b = food, # = both
crossings: 1
Why it works
A shared scale is the only way the two lines can be compared, and ignoring ties when tracking the leader counts real overtakes rather than moments of equality.
Examples
Example 1 · Input
3 rent 1 2 3 4 food 4 3 2 1
Expected output
4 |b..a |.##. 1 |a..b +---- a = rent, b = food, # = both crossings: 1
Example 2 · Input
2 x 1 1 y 1 1
Expected output
1 |.. 1 |## +-- a = x, b = y, # = both crossings: 0
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
Compute lo and hi over both series together, so they share one scale.
Hint 2
For crossings, remember the last leader that was not a tie and count each change.
Solution
Show one correct solution (try it yourself first)
h = int(input())
first = input().split()
second = input().split()
a = [float(v) for v in first[1:]]
b = [float(v) for v in second[1:]]
lo, hi = min(a + b), max(a + b)
def row(v):
return 0 if hi == lo else int((v - lo) / (hi - lo) * (h - 1) + 0.5)
ra, rb = [row(v) for v in a], [row(v) for v in b]
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 = ""
for x, y in zip(ra, rb):
marks += "#" if x == level and y == level else "a" if x == level else "b" if y == level else "."
print(f"{label:>{w}} |{marks}")
print(" " * w + " +" + "-" * len(a))
crossings = 0
leader = None
for x, y in zip(a, b):
now = "a" if x > y else "b" if y > x else None
if now is not None:
if leader is not None and now != leader:
crossings += 1
leader = now
print(f"a = {first[0]}, b = {second[0]}, # = both")
print(f"crossings: {crossings}")