Add Furniture - codepath/compsci_guides GitHub Wiki
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, Methods, Data Validation
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
add_item
method do?- Validate the input
item_name
and add it to thefurniture
attribute if valid.
- Validate the input
-
What constitutes a valid item name?
- It must be one of the following:
"acoustic guitar"
,"ironwood kitchenette"
,"rattan armchair"
,"kotatsu"
,"cacao tree"
.
- It must be one of the following:
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.
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 Object-Oriented Programming problems, we want to consider the following approaches:
- Implement methods to encapsulate behavior.
- Ensure methods perform validation before updating attributes.
3: P-lan
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
- Forgetting to validate the
item_name
before adding it to the list. - Not correctly referencing or updating the
furniture
attribute.
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 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")
5: R-eview
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
- Instantiate the
Villager
objectalice
. - Validate different item names and check the
furniture
attribute. - Ensure invalid item names do not affect the
furniture
attribute and print an error message.
6: E-valuate
Evaluate the performance of your algorithm and state any strong/weak or future potential work.
Assume N
represents the number of valid items.
- Time Complexity:
O(1)
because checking membership in a small list and appending to a list are constant-time operations. - Space Complexity:
O(1)
for eachVillager
instance because it uses a fixed amount of memory for its attributes and methods.