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"
description and a "timestamp"
indicating when the event occurs in the narrative.True
if all scenes are in chronological order, and False
if any scene is out of order."timestamp"
keys that are integers.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 True
Example 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