forked from codepath/compsci_guides
-
Notifications
You must be signed in to change notification settings - Fork 0
Track Podcast Episodes by Length
Jessica Sang edited this page Sep 14, 2024
·
1 revision
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.
- Q: What is the goal of the problem?
- A: The goal is to count the number of podcast episodes that fall into three specific time ranges: less than 30 minutes, 30 to 60 minutes, and more than 60 minutes.
- Q: What are the inputs?
- A: The input is a list of integers where each integer represents the duration of a podcast episode in minutes.
- Q: What are the outputs?
- A: The output is a tuple containing three integers representing the number of episodes in each of the three time ranges.
- Q: How should episodes that are exactly 30 or 60 minutes be classified?
- A: Episodes that are exactly 30 minutes should be included in the 30 to 60 minute range, and those that are exactly 60 minutes should also be included in the 30 to 60 minute range.
- Q: Are there any assumptions about the input?
- A: The list of episode lengths is non-negative and contains valid durations.
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)