TIP102 Unit 5 Session 1 Standard (Click for link to problem statements)
TIP102 Unit 5 Session 1 Advanced (Click for link to problem statements)
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?
personality
matches the given personality_type
.What parameters does the function take?
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.
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:
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
personality
attribute of villagers.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
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
Villager
.personality_type
values.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.
O(N)
because we need to iterate through the entire list of villagers.O(N)
for the list that stores the names of villagers with the specified personality.