Codepath

Poohsticks

TIP102 Unit 1 Session 1 Standard (Click for link to problem statements)

Problem Highlights

  • 💡 Difficulty: Easy
  • Time to complete: 5 mins
  • 🛠️ Topics: List Iteration, Conditionals

U-nderstand

Understand what the interviewer is asking for by using test cases and questions about the problem.

  • Q: What should the function count_less_than() do?

    • A: The function should accept a list of integers race_times and an integer threshold. It should return the number of elements in race_times that are less than threshold.
  • Q: What happens if all race times are greater than or equal to the threshold?

    • A: The function should return 0 since there are no times less than the threshold.
  • Q: Should the function handle negative numbers in the race_times list?

    • A: Yes, the function should work with any integer values in the race_times list, including negative numbers.
  • The function count_less_than() should take a list of integers race_times and an integer threshold and return the count of elements in race_times that are strictly less than threshold.

HAPPY CASE
Input: race_times = [1, 2, 3, 4, 5, 6], threshold = 4
Expected Output: 3

Input: race_times = [5, 6, 7, 8], threshold = 7
Expected Output: 2

EDGE CASE
Input: race_times = [], threshold = 4
Expected Output: 0

P-lan

Plan the solution with appropriate visualizations and pseudocode.

General Idea: Define a function that iterates through the list, counts how many elements are less than the threshold, and returns the count.

1. Define the function `count_less_than(race_times, threshold)`.
2. Initialize a variable `count` to 0.
3. Iterate through each element in `race_times`.
4. If an element is less than `threshold`, increment `count`.
5. Return `count`

⚠️ Common Mistakes

  • Forgetting to initialize the count variable.
  • Incorrectly comparing elements to the threshold.

I-mplement

Implement the code to solve the algorithm.

def count_less_than(race_times, threshold):
    # Initialize the count variable to 0
    count = 0
    
    # Iterate through each time in the race_times list
    for time in race_times:
        # If the time is less than the threshold, increment the count
        if time < threshold:
            count += 1
    
    # Return the count of race times less than the threshold
    return count
Fork me on GitHub