Count Unique Characters in a Script - 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 count the number of unique characters (keys) in a dictionary representing a script where each key is a character name and the value is a list of their dialogue lines.
  • Q: What are the inputs?
    • A: The input is a dictionary where the keys are strings representing character names and the values are lists of strings representing the dialogue lines.
  • Q: What are the outputs?
    • A: The output is an integer representing the number of unique characters in the script.
  • Q: How should the function handle cases where there are duplicate keys in the dictionary?
    • A: In Python, dictionaries cannot have duplicate keys; the last value will overwrite any previous values for the same key.
  • Q: Are there any assumptions about the input?
    • A: The input dictionary is well-formed with string keys and values being lists of strings.

P-lan

Plan the solution with appropriate visualizations and pseudocode.

General Idea: Use a set to track unique character names (keys). Since a set only keeps unique items, the number of unique characters will be the size of the set after iterating through the dictionary.

1) Initialize an empty set `unique_characters`.
2) Iterate over the `script` dictionary:
   a) For each key (character name), add it to the `unique_characters` set.
3) After iterating through all the keys, return the size of the `unique_characters` set, which represents the number of unique characters.

**⚠️ Common Mistakes**

- Assuming that dictionaries can have duplicate keys, which is not the case in Python.
- Not correctly understanding that each key in a dictionary is unique by definition.

I-mplement

def count_unique_characters(script):
    # Initialize a set to keep track of unique characters
    unique_characters = set()

    # Iterate over the dictionary and add each character to the set
    for character in script:
        unique_characters.add(character)

    # Return the count of unique characters
    return len(unique_characters)
Example Usage:

script = {
    "Alice": ["Hello there!", "How are you?"],
    "Bob": ["Hi Alice!", "I'm good, thanks!"],
    "Charlie": ["What's up?"]
}
print(count_unique_characters(script))  
# Output: 3

script_with_redundant_keys = {
    "Alice": ["Hello there!"],
    "Alice": ["How are you?"],
    "Bob": ["Hi Alice!"]
}
print(count_unique_characters(script_with_redundant_keys))  
# Output: 2