S3Q2 · Visualize Pattern Lock¶
⚡ Quick Reference
Type: Full I/O -grid visualization
Core idea: build a lookup label[dot_position] = visit_order, then render the 3×3 grid row by row.
seq = list(map(int, input().split()))
label = [''] * 10 # 1-indexed, label[i] = display for dot i
for order, dot in enumerate(seq, 1):
label[dot] = str(order)
def cell(dot):
return f"[{label[dot] if label[dot] else ' '}]"
for row in range(3):
dots = [row*3+1, row*3+2, row*3+3]
print(cell(dots[0]) + "---" + cell(dots[1]) + "---" + cell(dots[2]))
if row < 2:
print(" | | |")
Key rules:
- label[dot] = visit order if visited, else ' ' (space)
- Cell format: [n] for visited, [ ] for unvisited
- Row separator: " | | |" between rows (not after last)
- No trailing spaces
Problem Statement¶
Problem (I/O type)
Given a sequence of dot numbers visited in order, print a 3×3 pattern lock grid. Each visited dot shows its visit order; unvisited dots show [ ].
Example:
2 5 7 4 1
[5]---[1]---[ ]
| | |
[4]---[2]---[ ]
| | |
[3]---[ ]---[ ]
Understanding the structure¶
Dot numbering:
Input 2 5 7 4 1 means:
| Visit order | Dot visited |
|---|---|
| 1st | Dot 2 |
| 2nd | Dot 5 |
| 3rd | Dot 7 |
| 4th | Dot 4 |
| 5th | Dot 1 |
So the label at each dot position:
| Dot | Visited? | Label |
|---|---|---|
| 1 | Yes -5th | [5] |
| 2 | Yes -1st | [1] |
| 3 | No | [ ] |
| 4 | Yes -4th | [4] |
| 5 | Yes -2nd | [2] |
| 6 | No | [ ] |
| 7 | Yes -3rd | [3] |
| 8 | No | [ ] |
| 9 | No | [ ] |
Row 0: [5]---[1]---[ ]
Building the label lookup
enumerate(seq, 1) gives (1, dot2), (2, dot5), (3, dot7), ... -the visit order and the dot number simultaneously. Storing label[dot] = order gives O(1) lookup when rendering each cell.
Rendering the grid¶
Each content row: cell(d1) + "---" + cell(d2) + "---" + cell(d3)
Separator row: " | | |" -printed between content rows only (not after the last).
The separator spacing matches the 3-character cell width: [ ] is 3 chars, --- is 3 chars, | with spaces aligns under the cell centres.
Solution approaches¶
seq = list(map(int, input().split()))
label = [''] * 10 # 1-indexed; empty string = unvisited
for order, dot in enumerate(seq, 1):
label[dot] = str(order)
def cell(dot):
v = label[dot]
return f"[{v if v else ' '}]"
for row in range(3):
d1, d2, d3 = row*3+1, row*3+2, row*3+3
print(f"{cell(d1)}---{cell(d2)}---{cell(d3)}")
if row < 2:
print(" | | |")
seq = list(map(int, input().split()))
# Build label lookup: label[dot_number] = visit order string
label = {i: ' ' for i in range(1, 10)} # default: space (unvisited)
for order, dot in enumerate(seq, 1):
label[dot] = str(order)
# Render row by row
for row in range(3):
cells = []
for col in range(3):
dot = row * 3 + col + 1
cells.append(f"[{label[dot]}]")
print("---".join(cells))
if row < 2:
print(" | | |")
Uses a dict for the label lookup. "---".join(cells) assembles the three cells cleanly.
seq = list(map(int, input().split()))
label = {i: ' ' for i in range(1, 10)}
for order, dot in enumerate(seq, 1):
label[dot] = str(order)
make_row = lambda r: "---".join(
map(lambda c: f"[{label[r*3+c+1]}]", range(3))
)
rows = list(map(make_row, range(3)))
sep = " | | |"
print(("\n" + sep + "\n").join(rows))
make_row lambda builds one content row. ("\n" + sep + "\n").join(rows) weaves separators between the three rows in one expression.
Key takeaways¶
enumerate(seq, 1) for 1-based ordering
enumerate(seq, 1) gives (1, first_item), (2, second_item), .... Use the start argument to avoid adding 1 manually throughout the code.
dot = row*3 + col + 1
Maps a (row, col) pair to the dot number 1–9. Row 0 col 0 → dot 1; row 2 col 2 → dot 9. This avoids hardcoding the grid layout.
Separator only between rows
Print the separator " | | |" only when row < 2. A common mistake is printing it after every row including the last -use the condition to suppress the trailing separator.