Print Inventory - codepath/compsci_guides GitHub Wiki

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

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

Problem Highlights

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

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 print_inventory method do?

    • Print the name and quantity of each item in the furniture list.
    • If the furniture list is empty, print "Inventory empty".
  • How should the items and their quantities be printed?

    • Each item and its quantity should be printed in the format "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"`.

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.
  • Use data structures to manage and count inventory items.

3: P-lan

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

  • Forgetting to handle the case when the furniture list is empty.
  • Incorrectly counting or printing the items and their quantities.

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 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)

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 object alice.
  • Validate different furniture lists and check the printed output.
  • Ensure the method handles empty lists and prints the items and their quantities correctly.

6: E-valuate

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.

  • Time Complexity: O(N) because counting the items and creating the output string scale with the number of items.
  • Space Complexity: O(N) for the dictionary used to count items.