Codepath

Meme Length Filter

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 strings, where each string represents a meme.
  • Q: What is the output?

    • A: The output is a list of strings containing only the memes whose lengths do not exceed the given maximum length.
  • Q: What should the function return if all memes are longer than the maximum length?

    • A: The function should return an empty list.
  • Q: Are there any constraints on the input, such as the presence of non-alphanumeric characters?

    • A: The problem does not specify, but the function should handle any type of string.

P-lan

Plan the solution with appropriate visualizations and pseudocode.

General Idea: Iterate through the list of memes and check each meme's length. If the length is within the acceptable limit, add it to the list of filtered memes.

1) Initialize an empty list called `filtered_memes`.
2) For each `meme` in `memes`:
   a) If the length of `meme` is less than or equal to `max_length`, append it to `filtered_memes`.
3) Return the `filtered_memes` list.

**⚠️ Common Mistakes**

- Forgetting to handle cases where the input list is empty.
- Incorrectly calculating the length of the memes, leading to filtering errors.

I-mplement

def filter_meme_lengths(memes, max_length):
    filtered_memes = []
    for meme in memes:
        if len(meme) <= max_length:
            filtered_memes.append(meme)
    return filtered_memes
Fork me on GitHub