Ticket Sales - codepath/compsci_guides GitHub Wiki

Unit 2 Session 1 (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 problem asking for?

    • A: The problem asks to return the total number of tickets sold from a dictionary that maps ticket types to the number of tickets sold.
  • Q: What are the inputs?

    • A: A dictionary ticket_sales where keys are ticket types and values are the number of tickets sold.
  • Q: What are the outputs?

    • A: An integer representing the total number of tickets sold.
  • Q: Are there any constraints on the values?

    • A: The values should be non-negative integers.

P-lan

Plan the solution with appropriate visualizations and pseudocode.

General Idea: Sum up all the values in the ticket_sales dictionary to get the total number of tickets sold.

1) Initialize a variable `total` to 0.
2) Iterate through all the values in the `ticket_sales` dictionary.
   - For each value, add it to `total`.
3) Return the value of `total`.

⚠️ Common Mistakes

  • Ensure that all values in the dictionary are summed correctly.
  • Handle cases where the dictionary might be empty, resulting in a total of 0.

I-mplement

def total_sales(ticket_sales):
    total = 0
    for tickets in ticket_sales.values():
        total += tickets
    return total