Unit 4 Session 2 Standard (Click for link to problem statements)
Understand what the interviewer is asking for by using test cases and questions about the problem.
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Iterate through the list of episode lengths and count how many fall into each of the three specified time ranges. Maintain three counters to track the number of episodes in each range.
1) Initialize three counters: `less_than_30`, `between_30_and_60`, and `more_than_60` to zero.
2) Iterate through the `episode_lengths` list:
a) If the episode length is less than 30 minutes, increment `less_than_30`.
b) If the episode length is between 30 and 60 minutes (inclusive), increment `between_30_and_60`.
c) If the episode length is greater than 60 minutes, increment `more_than_60`.
3) Return a tuple containing the three counters: `(less_than_30, between_30_and_60, more_than_60)`.
**⚠️ Common Mistakes**
- Misclassifying episodes that are exactly 30 or 60 minutes.
- Incorrectly updating the counters or initializing them improperly.
- Not considering edge cases, such as an empty list or episodes all falling into one category.
def track_episode_lengths(episode_lengths):
less_than_30 = 0
between_30_and_60 = 0
more_than_60 = 0
for length in episode_lengths:
if length < 30:
less_than_30 += 1
elif 30 <= length < 60:
between_30_and_60 += 1
else:
more_than_60 += 1
return (less_than_30, between_30_and_60, more_than_60)
Example Usage:
episode_lengths = [15, 45, 32, 67, 22, 59, 70]
print(track_episode_lengths(episode_lengths))
# Output: (2, 3, 2)
episode_lengths_2 = [10, 25, 30, 45, 55, 65, 80]
print(track_episode_lengths(episode_lengths_2))
# Output: (2, 3, 2)
episode_lengths_3 = [30, 30, 30, 30, 30]
print(track_episode_lengths(episode_lengths_3))
# Output: (0, 5, 0)