-
Notifications
You must be signed in to change notification settings - Fork 266
Analyze Storyline Continuity
kyra-ptn edited this page Sep 3, 2024
·
2 revisions
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.
- Q: What is the goal of the problem?
- A: The goal is to determine if a list of scenes in a storyline are in chronological order based on their timestamps.
- Q: What are the inputs?
- A: The input is a list of dictionaries, where each dictionary represents a scene and includes a
"scene"description and a"timestamp"indicating when the event occurs in the narrative.
- A: The input is a list of dictionaries, where each dictionary represents a scene and includes a
- Q: What are the outputs?
- A: The output is a boolean value:
Trueif all scenes are in chronological order, andFalseif any scene is out of order.
- A: The output is a boolean value:
- Q: How should the function handle timestamps?
- A: The function should iterate through the scenes and check if each scene's timestamp is greater than or equal to the previous scene's timestamp.
- Q: Are there any assumptions about the input?
- A: The input list contains well-formed dictionaries with valid
"timestamp"keys that are integers.
- A: The input list contains well-formed dictionaries with valid
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Iterate through the list of scenes, comparing each scene's timestamp with the previous one. If any timestamp is found to be out of order, return False. If all timestamps are in order, return True.
1) Iterate through the `scenes` list starting from the second scene (index 1).
2) For each scene, compare its `timestamp` with the `timestamp` of the previous scene.
a) If the current scene's `timestamp` is less than the previous scene's `timestamp`, return `False`.
3) If the loop completes without finding any out-of-order timestamps, return `True`.
**⚠️ Common Mistakes**
- Not correctly comparing consecutive timestamps, leading to incorrect identification of continuity gaps.
- Assuming that timestamps are always in order without checking, which could result in incorrect output.
- Forgetting to handle edge cases such as a single scene or an empty list, which are trivially continuous.def analyze_storyline_continuity(scenes):
for i in range(1, len(scenes)):
if scenes[i]['timestamp'] < scenes[i-1]['timestamp']:
return False
return TrueExample Usage:
scenes = [
{"scene": "The hero enters the dark forest.", "timestamp": 1},
{"scene": "A mysterious figure appears.", "timestamp": 2},
{"scene": "The hero faces his fears.", "timestamp": 3},
{"scene": "The hero finds a hidden treasure.", "timestamp": 4},
{"scene": "An eerie silence fills the air.", "timestamp": 5}
]
continuity = analyze_storyline_continuity(scenes)
print(continuity)
# Output: True
scenes = [
{"scene": "The spaceship lands on an alien planet.", "timestamp": 3},
{"scene": "A strange creature approaches.", "timestamp": 2},
{"scene": "The crew explores the new world.", "timestamp": 4},
{"scene": "The crew encounters hostile forces.", "timestamp": 5},
{"scene": "The crew makes a narrow escape.", "timestamp": 6}
]
continuity = analyze_storyline_continuity(scenes)
print(continuity)
# Output: False