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 add_item
method do?
item_name
and add it to the furniture
attribute if valid.What constitutes a valid item name?
"acoustic guitar"
, "ironwood kitchenette"
, "rattan armchair"
, "kotatsu"
, "cacao tree"
.HAPPY CASE
Input:
alice = Villager("Alice", "Koala", "Normal", "guvnor")
alice.add_item("acoustic guitar")
print(alice.furniture)
Output:
["acoustic guitar"]
Explanation:
The `item_name` is valid, so it is added to the `furniture` attribute of `alice`.
EDGE CASE
Input:
alice = Villager("Alice", "Koala", "Normal", "guvnor")
alice.add_item("nintendo switch")
print(alice.furniture)
Output:
[]
Invalid item
Explanation:
The `item_name` is invalid, so it is not added to the `furniture` attribute of `alice`, and an error message is printed.
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 Object-Oriented Programming problems, we want to consider the following approaches:
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Implement the add_item
method to validate and add items to the furniture
attribute.
1) Define the `add_item` method in the `Villager` class.
2) Implement validation to check if `item_name` is in the list of valid items.
3) If valid, add `item_name` to the `furniture` attribute.
4) If invalid, print "Invalid item".
⚠️ Common Mistakes
item_name
before adding it to the list.furniture
attribute.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 add_item(self, item_name):
valid_items = ["acoustic guitar", "ironwood kitchenette", "rattan armchair", "kotatsu", "cacao tree"]
if item_name in valid_items:
self.furniture.append(item_name)
else:
print("Invalid item")
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
Villager
object alice
.furniture
attribute.furniture
attribute and print an error message.Evaluate the performance of your algorithm and state any strong/weak or future potential work.
Assume N
represents the number of valid items.
O(1)
because checking membership in a small list and appending to a list are constant-time operations.O(1)
for each Villager
instance because it uses a fixed amount of memory for its attributes and methods.