Challenge - 5 Problems
Master of Searching and Counting
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ Predict Output
intermediate2:00remaining
Counting occurrences in a list
What is the output of this code that counts how many times the number 3 appears in the list?
Python
numbers = [1, 3, 5, 3, 7, 3, 9] count_3 = numbers.count(3) print(count_3)
Attempts:
2 left
๐ก Hint
Use the list method that counts how many times an item appears.
โ Incorrect
The list method count(x) returns how many times x appears in the list. Here, 3 appears three times.
โ Predict Output
intermediate2:00remaining
Finding the index of an element
What will be printed by this code that finds the first position of the string 'apple' in the list?
Python
fruits = ['banana', 'apple', 'orange', 'apple'] index = fruits.index('apple') print(index)
Attempts:
2 left
๐ก Hint
The index method returns the first position where the item is found.
โ Incorrect
The first 'apple' is at position 1 (counting from zero).
โ Predict Output
advanced2:00remaining
Counting elements with a condition
What is the output of this code that counts how many numbers in the list are greater than 5?
Python
nums = [2, 7, 4, 9, 5, 8] count = sum(1 for n in nums if n > 5) print(count)
Attempts:
2 left
๐ก Hint
Sum 1 for each number that meets the condition n > 5.
โ Incorrect
Numbers greater than 5 are 7, 9, and 8, so count is 3.
โ Predict Output
advanced2:00remaining
Finding the first matching element with next()
What will this code print? It finds the first even number in the list or returns -1 if none found.
Python
numbers = [1, 3, 5, 8, 10] first_even = next((x for x in numbers if x % 2 == 0), -1) print(first_even)
Attempts:
2 left
๐ก Hint
next() returns the first item from the generator that meets the condition.
โ Incorrect
The first even number is 8, so it prints 8.
โ Predict Output
expert2:00remaining
Counting unique elements with a condition
What is the output of this code that counts how many unique words in the list have length greater than 3?
Python
words = ['cat', 'dog', 'elephant', 'dog', 'bird', 'cat', 'elephant'] unique_words = set(words) count = sum(1 for w in unique_words if len(w) > 3) print(count)
Attempts:
2 left
๐ก Hint
Convert list to set to get unique words, then count those longer than 3 letters.
โ Incorrect
Unique words are {'cat', 'dog', 'elephant', 'bird'}. Only 'elephant' and 'bird' have length > 3, so count is 2.