Track Scene Transitions - codepath/compsci_guides GitHub Wiki

Unit 4 Session 2 Advanced (Click for link to problem statements)

U-nderstand

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 simulate and print out the transitions between scenes in the order they appear in a list.
  • Q: What are the inputs?
    • A: The input is a list of strings where each string represents a scene in a story.
  • Q: What are the outputs?
    • A: The output is a series of printed statements showing the transitions from one scene to the next.
  • Q: How should the transitions be handled?
    • A: Each scene transitions to the next scene in the list, and this transition should be printed.
  • Q: Are there any assumptions about the input?
    • A: The list of scenes contains at least two scenes to simulate transitions.

P-lan

Plan the solution with appropriate visualizations and pseudocode.

General Idea: Use a queue to process each scene in the order they appear. As each scene is dequeued, print the transition from the current scene to the next scene.

1) Initialize a queue using the `deque` class and add all scenes to the queue.
2) While the queue has more than one scene:
   a) Dequeue the first scene (current_scene).
   b) Peek at the next scene in the queue (next_scene).
   c) Print the transition from `current_scene` to `next_scene`.
3) Continue until all transitions have been printed.

**⚠️ Common Mistakes**

- Forgetting to handle cases where there are fewer than two scenes, which would result in no transitions to print.
- Not correctly dequeuing or peeking the next scene, which could lead to incorrect transitions.

I-mplement

from collections import deque

def track_scene_transitions(scenes):
    # Initialize a queue with the scenes
    queue = deque(scenes)

    # Process the scenes to track transitions
    while len(queue) > 1:
        current_scene = queue.popleft()
        next_scene = queue[0]
        print(f"Transition from {current_scene} to {next_scene}")
Example Usage:

scenes = ["Opening", "Rising Action", "Climax", "Falling Action", "Resolution"]
track_scene_transitions(scenes)
# Output:
# Transition from Opening to Rising Action
# Transition from Rising Action to Climax
# Transition from Climax to Falling Action
# Transition from Falling Action to Resolution

scenes = ["Introduction", "Conflict", "Climax", "Denouement"]
track_scene_transitions(scenes)
# Output:
# Transition from Introduction to Conflict
# Transition from Conflict to Climax
# Transition from Climax to Denouement