S2Q2 · Thresholding a 2D Array with * and @¶
⚡ Quick Reference
Type: Full I/O problem
Core idea: read each row, map each value to "@" or "*" based on the threshold, print space-separated.
n, t = map(int, input().split())
for _ in range(n):
row = map(int, input().split())
print(" ".join("@" if v >= t else "*" for v in row))
Key rules:
- value >= t → "@" | value < t → "*"
- Elements separated by spaces, no leading/trailing space
- " ".join(...) handles spacing cleanly
Problem Statement¶
Problem (I/O type)
Read an n×n grid and threshold value t. Replace each pixel with @ if ≥ t, else *. Print the result.
Example 1:
3 5
7 2 8
4 5 3
6 0 7
@ * @
* @ *
@ * @
Example 2:
2 50
10 70
60 40
* @
@ *
Tracing Example 1 (n=3, t=5)¶
| Row | Values | ≥ 5? | Output |
|---|---|---|---|
| 1 | 7, 2, 8 | ✅, ❌, ✅ | @ * @ |
| 2 | 4, 5, 3 | ❌, ✅, ❌ | * @ * |
| 3 | 6, 0, 7 | ✅, ❌, ✅ | @ * @ |
Solution approaches¶
Key takeaways¶
" ".join() - no leading or trailing space
" ".join(symbols) places exactly one space between elements and nothing at the ends. Using print(*symbols) also works - it joins with a space by default. Both avoid the trailing space issue.
Process row by row - no need to store the full grid
Read one row, convert, print - then move to the next. The grid never needs to be stored in memory all at once, making this efficient for large inputs.
≥ t maps to "@" - inclusive threshold
Values exactly equal to t map to "@" (not "*"). The condition is v >= t, not v > t. From example 1: value 5 with threshold 5 → "@" ✓