Build a boolean mask from a comparison, print it the way NumPy does, and use it to filter and count.
What to build
A program that applies a threshold to an array.
Requirements
- For the comparison
values > T, print the mask in NumPy's format: square brackets, each True written asTrueand each False asFalse, separated by single spaces. - Then print the selected values as a Python list, then
count: K.
Input
Line 1: whole numbers. Line 2: T.
Output
Three lines.
Constraints
1 to 30 numbers; |x|, |T| ≤ 10^6.
Example
Input:
4 9 1 7
5
Output:
[False True False True]
[9, 7]
count: 2
Why it works
NumPy pads True to the width of False so that masks line up in columns, and summing a mask counts its True values because True is 1.
Examples
Example 1 · Input
4 9 1 7 5
Expected output
[False True False True] [9, 7] count: 2
Example 2 · Input
1 2 0
Expected output
[ True True] [1, 2] count: 2
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
NumPy prints True as " True" (with a space) so that it lines up with "False".
Hint 2
Count the matches by summing the booleans.
Solution
Show one correct solution (try it yourself first)
values = [int(x) for x in input().split()]
t = int(input())
mask = [v > t for v in values]
print("[" + " ".join(" True" if m else "False" for m in mask) + "]")
print([v for v, m in zip(values, mask) if m])
print("count:", sum(mask))