Unit 4 Session 1 Standard (Click for link to problem statements)
Understand what the interviewer is asking for by using test cases and questions about the problem.
Q: What is the structure of the input?
"creator"
and "text"
.Q: What is the output?
Q: What should the function return if the input list is empty?
Q: Are there any constraints on the input, such as the presence of the "creator" key in each dictionary?
"creator"
key with a corresponding value.Plan the solution with appropriate visualizations and pseudocode.
General Idea: Iterate over each dictionary in the input list, extract the value associated with the "creator"
key, and update a count in a dictionary to track the number of memes each creator has made.
1) Initialize an empty dictionary called `creator_count`.
2) For each dictionary (meme) in the input list:
a) Extract the value associated with the `"creator"` key.
b) If the creator is already in `creator_count`, increment their count by 1.
c) If the creator is not in `creator_count`, add them with a count of 1.
3) Return the `creator_count` dictionary as the output.
**⚠️ Common Mistakes**
- Forgetting to initialize the creator count correctly when encountering a creator for the first time.
- Assuming that the `"creator"` key will always be present in the dictionaries without verifying.
def count_meme_creators(memes):
creator_count = {}
for meme in memes:
creator = meme["creator"]
if creator in creator_count:
creator_count[creator] += 1
else:
creator_count[creator] = 1
return creator_count