Skip to content

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:

Input
3 5
7 2 8
4 5 3
6 0 7
Output
@ * @
* @ *
@ * @

Example 2:

Input
2 50
10 70
60 40
Output
* @
@ *

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

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))
n, t = map(int, input().split())
for _ in range(n):
    values  = list(map(int, input().split()))
    symbols = []
    for v in values:
        if v >= t:
            symbols.append("@")
        else:
            symbols.append("*")
    print(" ".join(symbols))
n, t = map(int, input().split())
for _ in range(n):
    row = [int(x) for x in input().split()]
    result = ["@" if v >= t else "*" for v in row]
    print(" ".join(result))
n, t = map(int, input().split())
threshold = lambda v: "@" if v >= t else "*"
for _ in range(n):
    row = map(int, input().split())
    print(" ".join(map(threshold, row)))

map(threshold, row) applies the lambda to every pixel. " ".join(...) formats the output. Clean one-liner per row.


Key takeaways

01

" ".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.

02

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.

03

≥ 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 → "@"