S3Q2 · Draw Arrow Trail from Movement Deltas¶
⚡ Quick Reference
Type: Full I/O -positional arrow drawing
Core idea: track current position; for each delta draw the arrow with leading spaces based on its start column.
deltas = list(map(int, input().split()))
pos = 0
for d in deltas:
if d > 0:
print(" " * pos + "=" * d + ">")
elif d < 0:
v = -d
print(" " * (pos - v) + "<" + "=" * (v - 1))
else:
print(" " * pos + "=")
pos += d
Key rules:
- Right (d > 0): arrow starts at pos, "=" * d + ">", leading spaces = pos
- Left (d < 0): arrow ends at pos, "<" + "=" * (|d|-1), leading spaces = pos - |d|
- Zero: "=" at pos, leading spaces = pos
- Update pos += d after printing each arrow
Problem Statement¶
Problem (I/O type)
Read space-separated movement deltas. For each delta, draw an arrow aligned to its position. Right arrows start at the current position; left arrows end at it.
Example:
1 4 -2 5 0 -3 0 -3 1 4
=>
====>
<==
=====>
=
<===
=
<===
=>
====>
Understanding the arrow rules¶
Right arrow (d > 0):
- Starts at column pos
- Length d equals signs followed by >
- Content: "=" * d + ">"
- Leading spaces: pos
Left arrow (d < 0, length v = -d):
- Ends at column pos (current position before moving)
- Starts at column pos - v
- Content: "<" + "=" * (v - 1) -total length v
- Leading spaces: pos - v
Zero (d == 0):
- Single = at column pos
- Leading spaces: pos
Left arrow alignment
The left arrow ends at the current position, so it starts at pos - v. The < is at the leftmost end, followed by v-1 equals signs. For d=-2 at pos=5: starts at column 3, content <==, leading spaces 5-2=3.
Tracing the example¶
| Delta | pos before |
Arrow content | Leading spaces | pos after |
|---|---|---|---|---|
| 1 | 0 | => |
0 | 1 |
| 4 | 1 | ====> |
1 | 5 |
| -2 | 5 | <== |
3 | 3 |
| 5 | 3 | =====> |
3 | 8 |
| 0 | 8 | = |
8 | 8 |
| -3 | 8 | <=== |
5 | 5 |
| 0 | 5 | = |
5 | 5 |
| -3 | 5 | <=== |
2 | 2 |
| 1 | 2 | => |
2 | 3 |
| 4 | 3 | ====> |
3 | 7 |
Solution approaches¶
deltas = list(map(int, input().split()))
pos = 0
for d in deltas:
if d > 0:
padding = " " * pos
arrow = "=" * d + ">"
elif d < 0:
v = abs(d)
padding = " " * (pos - v)
arrow = "<" + "=" * (v - 1)
else:
padding = " " * pos
arrow = "="
print(padding + arrow)
pos += d
Every component named -easy to trace and debug.
make_arrow = lambda d, pos: (
" " * pos + "=" * d + ">" if d > 0 else
" " * (pos + d) + "<" + "=" * (-d - 1) if d < 0 else
" " * pos + "="
)
deltas = list(map(int, input().split()))
pos = 0
for d in deltas:
print(make_arrow(d, pos))
pos += d
Note: for negative d, pos + d = pos - |d| -the start column of the left arrow.
Key takeaways¶
Right arrow starts at pos, left arrow ends at pos
The current position is always the anchor -right arrows grow rightward from it, left arrows grow leftward ending at it. Getting this direction right is the key insight.
Left arrow leading spaces = pos - |d|
The left arrow starts |d| columns to the left of the current position. Leading spaces = pos - |d|. Always guaranteed non-negative since the problem states position is always ≥ 0.
Update pos after printing
Print with the current pos, then update with pos += d. Updating before printing would shift every arrow by one step -a common off-by-one mistake in positional problems.