Count Elements Meeting a Condition Practice Problem
This data science coding problem helps you practice Control Flow & Loops, count elements meeting a condition, and implementation skills. Read the problem statement, write your solution, and strengthen your understanding of Control Flow & Loops.
- Problem ID: 75
- Problem key: 75-count-elements-meeting-a-condition
- URL: https://datacrack.app/solve/75-count-elements-meeting-a-condition
- Difficulty: easy
- Topic: Control Flow & Loops
- Module: Python Fundamentals
Problem Statement
# 🧩 Count Elements Meeting a Condition
---
### 🎯 Goal
Counting elements that meet a condition is a **core pattern** in data science. Before you learn `len(df[df['col'] > threshold])` in Pandas, you should be able to build it manually with a loop.
---
### 💻 Task
Implement `count_above_threshold(nums, threshold)` that counts elements strictly greater than the threshold **without** using built-in functions like `sum()` or `filter()`.
---
### 📥 Input
- `nums`: list of numbers (integers or floats)
- `threshold`: number — the comparison value
### 📤 Output
- `int` — count of elements in `nums` that are strictly greater than `threshold`
---
### 🧩 Starter Code
```python
def count_above_threshold(nums, threshold):
"""
Count how many elements in nums are strictly greater than threshold.
Args:
nums (list): List of numbers
threshold (float): The comparison value
Returns:
int: Count of elements greater than threshold
"""
# 🧠 TODO: Loop through nums, check each element, count the ones > threshold
pass
```
---
### 💡 Example
```python
count_above_threshold([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 5)
# Expected: 5 (elements 6, 7, 8, 9, 10)
count_above_threshold([5, 5, 5], 5)
# Expected: 0 (strictly greater than, not >=)
```
---
### 🔑 Key Concepts
- `>` (strictly greater than) vs `>=` (greater than or equal to)
- Empty list should return 0
- Counter pattern: initialize to 0, increment inside the loopStarter Code
def count_above_threshold(nums, threshold):
"""
Count how many elements in nums are strictly greater than threshold.
Args:
nums (list): List of numbers
threshold (float): The comparison value
Returns:
int: Count of elements greater than threshold
"""
# 🧠 TODO: Loop through nums, check each element, count the ones > threshold
pass