Codepath

Group by Personality

TIP102 Unit 5 Session 1 Standard (Click for link to problem statements)

TIP102 Unit 5 Session 1 Advanced (Click for link to problem statements)

Problem Highlights

  • 💡 Difficulty: Easy
  • Time to complete: 10-15 mins
  • 🛠️ Topics: Classes, Object-Oriented Programming, List Comprehension

1: U-nderstand

Understand what the interviewer is asking for by using test cases and questions about the problem.

  • Established a set (2-3) of test cases to verify their own solution later.
  • Established a set (1-2) of edge cases to verify their solution handles complexities.
  • Have fully understood the problem and have no clarifying questions.
  • Have you verified any Time/Space Constraints for this problem?
  • What should the of_personality_type function do?

    • It should return a list of names of villagers whose personality matches the given personality_type.
  • What parameters does the function take?

    • A list of Villager instances (townies) and a string (personality_type).
HAPPY CASE
Input: 
isabelle = Villager("Isabelle", "Dog", "Normal", "what's up?")
bob = Villager("Bob", "Cat", "Lazy", "pthhhpth")
stitches = Villager("Stitches", "Cub", "Lazy", "stuffin'")
townies = [isabelle, bob, stitches]
personality_type = "Lazy"
result = of_personality_type(townies, personality_type)

Output: 
["Bob", "Stitches"]

Explanation: 
The function iterates through the `townies` list and collects names of villagers with the "Lazy" personality.

EDGE CASE
Input: 
townies = [isabelle, bob, stitches]
personality_type = "Cranky"
result = of_personality_type(townies, personality_type)

Output: 
[]

Explanation: 
No villagers have the "Cranky" personality, so the function returns an empty list.

2: M-atch

Match what this problem looks like to known categories of problems, e.g. Linked List or Dynamic Programming, and strategies or patterns in those categories.

For List Comprehension problems, we want to consider the following approaches:

  • Iterate over the list to filter items based on a condition.
  • Use list comprehension for concise and readable code.

3: P-lan

Plan the solution with appropriate visualizations and pseudocode.

General Idea: Filter the townies list to get names of villagers with the specified personality_type.

1) Define the `of_personality_type` function.
2) Initialize an empty list to store names of villagers with the specified personality type.
3) Iterate through the `townies` list.
4) For each villager, check if their `personality` matches the `personality_type`.
5) If it matches, add the villager's `name` to the result list.
6) Return the result list.

⚠️ Common Mistakes

  • Forgetting to return the list of names.
  • Incorrectly referencing the personality attribute of villagers.

4: I-mplement

Implement the code to solve the algorithm.

class Villager:
    def __init__(self, name, species, personality, catchphrase):
        self.name = name
        self.species = species
        self.personality = personality
        self.catchphrase = catchphrase
        self.furniture = []

def of_personality_type(townies, personality_type):
    result = []
    for villager in townies:
        if villager.personality == personality_type:
            result.append(villager.name)
    return result

5: R-eview

Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.

  • Create instances of Villager.
  • Validate the function with different personality_type values.
  • Ensure the function correctly filters and returns the list of names.

6: E-valuate

Evaluate the performance of your algorithm and state any strong/weak or future potential work.

Assume N represents the number of villagers in the townies list.

  • Time Complexity: O(N) because we need to iterate through the entire list of villagers.
  • Space Complexity: O(N) for the list that stores the names of villagers with the specified personality.
Fork me on GitHub