Codepath

Top Meme Creators

Unit 4 Session 1 Standard (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 structure of the input?

    • A: The input is a list of dictionaries, where each dictionary contains a "creator" key and a "text" key representing a meme.
  • Q: What is the output?

    • A: The output is a dictionary where the keys are creators' names and the values are the number of memes they have created.
  • Q: What should the function return if there are no memes?

    • A: The function should return an empty dictionary.
  • Q: Are there any constraints on the input, such as the presence of the "creator" key in each dictionary?

    • A: It is assumed that each dictionary in the list will have a "creator" key with a corresponding value.

P-lan

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.

I-mplement

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
Fork me on GitHub