Manage Character Arcs - 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 managing character arcs using a stack, ensuring that events are processed in the correct sequence.
  • Q: What are the inputs?
    • A: The input is a list of strings where each string represents an event in a character's development.
  • Q: What are the outputs?
    • A: The output is a list of processed events in the correct order as they should appear in the character arc.
  • Q: How should the events be managed using the stack?
    • A: Events should be pushed onto the stack as they occur, and then popped off to ensure they are processed in the reverse order they were added.
  • Q: Are there any assumptions about the input?
    • A: The list of events is well-formed and follows the intended sequence for the character's arc.

P-lan

Plan the solution with appropriate visualizations and pseudocode.

General Idea: Use a stack to manage the sequence of events. Push all events onto the stack, then pop them off to simulate processing the events in the correct order.

1) Initialize an empty stack.
2) Iterate over the `events` list in reverse order:
   a) Push each event onto the stack.
3) Initialize an empty list `processed_arc` to store the processed events.
4) While the stack is not empty:
   a) Pop an event from the stack and add it to `processed_arc`.
5) Return `processed_arc`, which now contains the events in the correct sequence.

**⚠️ Common Mistakes**

- Forgetting to reverse the order of events when pushing them onto the stack, leading to incorrect sequencing.
- Not correctly popping all events off the stack, which could leave the character arc incomplete.
- Assuming that the stack operations are unnecessary when they are essential to the problem's requirements.

I-mplement

def manage_character_arc(events):
    stack = []

    # Push events onto the stack in reverse order
    for event in reversed(events):
        stack.append(event)

    # Pop events from the stack to process them in the correct order
    processed_arc = []
    while stack:
        processed_arc.append(stack.pop())

    return processed_arc
Example Usage:

events = [
    "Character is introduced.",
    "Character faces a dilemma.",
    "Character makes a decision.",
    "Character grows stronger.",
    "Character achieves goal."
]

processed_arc = manage_character_arc(events)
print(processed_arc)  
# Output: ['Character is introduced.', 'Character faces a dilemma.', 'Character makes a decision.', 'Character grows stronger.', 'Character achieves goal.']

events = [
    "Character enters a new world.",
    "Character struggles to adapt.",
    "Character finds a mentor.",
    "Character gains new skills.",
    "Character faces a major setback.",
    "Character overcomes the setback."
]

processed_arc = manage_character_arc(events)
print(processed_arc)  
# Output: ['Character enters a new world.', 'Character struggles to adapt.', 'Character finds a mentor.', 'Character gains new skills.', 'Character faces a major setback.', 'Character overcomes the setback.']