📘
Boolean Masking
EasyNumPy Boolean
Problem Statement
Use boolean conditions to filter NumPy arrays.
Background
Boolean indexing selects elements based on conditions:
arr[arr > 5] # All elements greater than 5
arr[arr % 2 == 0] # All even elements
Your Task
Write a function filter_array(arr, threshold) that returns a dictionary with:
- "above": Elements strictly greater than threshold (as list)
- "below_eq": Elements less than or equal to threshold (as list)
- "count_above": Count of elements above threshold
Example:
Input:
arr = [1, 5, 3, 8, 2, 9, 4, 7], threshold = 5
Output:
{'above': [8, 9, 7], 'below_eq': [1, 5, 3, 2, 4], 'count_above': 3}Reasoning:
arr[arr > 5] gives [8, 9, 7], arr[arr <= 5] gives [1, 5, 3, 2, 4]
Constraints:
- Use boolean indexing arr[condition]
- threshold will be a number
Editor
Python 3.13.1
Test Results
0/0Run code to see test results.