Unit 5 Session 1 (Click for link to problem statements)
TIP102 Unit 5 Session 1 Standard (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 print_inventory
method do?
furniture
list.furniture
list is empty, print "Inventory empty"
.How should the items and their quantities be printed?
"item: quantity"
.HAPPY CASE
Input:
alice = Villager("Alice", "Koala", "Normal", "guvnor")
alice.print_inventory()
Output:
Inventory empty
Input:
alice = Villager("Alice", "Koala", "Normal", "guvnor")
alice.furniture = ["acoustic guitar", "ironwood kitchenette", "kotatsu", "kotatsu"]
alice.print_inventory()
Output:
acoustic guitar: 1, ironwood kitchenette: 1, kotatsu: 2
Explanation:
The `furniture` list is processed to count each item, and the counts are printed correctly. If the list is empty, it prints `"Inventory empty"`.
EDGE CASE
Input:
alice = Villager("Alice", "Koala", "Normal", "guvnor")
alice.furniture = []
alice.print_inventory()
Output:
Inventory empty
Explanation:
The method handles an empty `furniture` list by printing `"Inventory empty"`.
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 print_inventory
method to count and print the items in the furniture
list.
1) Define the `print_inventory` method in the `Villager` class.
2) Check if the `furniture` list is empty.
3) If empty, print "Inventory empty".
4) If not empty, create a dictionary to count each item in the `furniture` list.
5) Iterate over the `furniture` list to populate the dictionary with item counts.
6) Create a formatted string to print each item and its quantity.
7) Print the formatted string.
⚠️ Common Mistakes
furniture
list is empty.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 print_inventory(self):
if not self.furniture:
print("Inventory empty")
else:
item_counts = {}
for item in self.furniture:
if item in item_counts:
item_counts[item] += 1
else:
item_counts[item] = 1
inventory_output = ", ".join([f"{item}: {count}" for item, count in item_counts.items()])
print(inventory_output)
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
Villager
object alice
.furniture
lists and check the printed output.Evaluate the performance of your algorithm and state any strong/weak or future potential work.
Assume N
represents the number of items in the furniture
list.
O(N)
because counting the items and creating the output string scale with the number of items.O(N)
for the dictionary used to count items.