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.
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.
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