PIXELBANKv8.2.1
Menu

Boolean Indexing

Problem Statement

Use boolean conditions to filter NumPy arrays.

Background

Boolean indexing lets you select 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:

  • "greater": Elements greater than threshold as list
  • "less_equal": Elements less than or equal to threshold as list
  • "count_greater": Count of elements greater than threshold

Output Format

Return a dictionary with exactly these three keys.

Example:

Input:
arr = [1, 5, 3, 8, 2, 9, 4, 7], threshold = 5
Output:
{'greater': [8, 9, 7], 'less_equal': [1, 5, 3, 2, 4], 'count_greater': 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

Test Results

0/0
Run code to see test results.