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"
key and a "text"
key representing a meme.Q: What is the output?
Q: What should the function return if there are no memes?
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 through the list of memes, count how many memes each creator has, and store this information in a dictionary.
1) Initialize an empty dictionary called `creator_count`.
2) For each `meme` in `memes`:
a) Extract the `creator` from the `meme`.
b) If the `creator` is already in `creator_count`, increment the count.
c) If the `creator` is not in `creator_count`, add them with a count of 1.
3) Return the `creator_count` dictionary.
**⚠️ 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