Unit 4 Session 2 Advanced (Click for link to problem statements)
Understand what the interviewer is asking for by using test cases and questions about the problem.
"scene"
and "theme"
key, and the values are strings.Plan the solution with appropriate visualizations and pseudocode.
General Idea: Use a dictionary to count the frequency of each theme. After counting, extract and return the themes that have a count greater than one.
1) Initialize an empty dictionary `theme_count` to track the frequency of each theme.
2) Iterate through the `scenes` list:
a) Extract the theme from each scene and update its count in `theme_count`.
3) Create a list `repeated_themes` containing themes that have a count greater than one.
4) Return the `repeated_themes` list.
**⚠️ Common Mistakes**
- Forgetting to correctly update the count for each theme, leading to incorrect identification of repeated themes.
- Assuming that all themes are unique without handling cases where themes appear more than once.
- Not considering that the input might contain no repeated themes, which should result in an empty list being returned.
def identify_repeated_themes(scenes):
theme_count = {}
# Count the occurrences of each theme
for scene in scenes:
theme = scene["theme"]
if theme in theme_count:
theme_count[theme] += 1
else:
theme_count[theme] = 1
# Extract themes that appear more than once
repeated_themes = [theme for theme, count in theme_count.items() if count > 1]
return repeated_themes
Example Usage:
scenes = [
{"scene": "The hero enters the dark forest.", "theme": "courage"},
{"scene": "A mysterious figure appears.", "theme": "mystery"},
{"scene": "The hero faces his fears.", "theme": "courage"},
{"scene": "An eerie silence fills the air.", "theme": "mystery"},
{"scene": "The hero finds a hidden treasure.", "theme": "discovery"}
]
repeated_themes = identify_repeated_themes(scenes)
print(repeated_themes)
# Output: ['courage', 'mystery']
scenes = [
{"scene": "The spaceship lands on an alien planet.", "theme": "exploration"},
{"scene": "A strange creature approaches.", "theme": "danger"},
{"scene": "The crew explores the new world.", "theme": "exploration"},
{"scene": "The crew encounters hostile forces.", "theme": "conflict"},
{"scene": "The crew makes a narrow escape.", "theme": "danger"}
]
repeated_themes = identify_repeated_themes(scenes)
print(repeated_themes)
# Output: ['exploration', 'danger']