Unit 5 Session 1 (Click for link to problem statements)
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?
add_item()
to add a valid item to the items
attribute of the Player
class.HAPPY CASE
Input: add_item("red shell")
Output: items = ["red shell"]
Explanation: The `item_name` "red shell" is valid, so it is added to the `items` list.
EDGE CASE
Input: add_item("super smash")
Output: items = ["red shell", "super star"]
Explanation: The `item_name` "super smash" is not valid, so it is not added to the `items` 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 method implementation with validation problems, we want to consider the following approaches:
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Implement a method that validates the input item and adds it to the player's items
list if it's valid.
1) Create a method `add_item(self, item_name)` in the `Player` class.
2) Define a list `valid_items` containing the allowed item names.
3) Check if `item_name` is in `valid_items`.
4) If `item_name` is valid, append it to `self.items`.
5) If `item_name` is not valid, do nothing.
⚠️ Common Mistakes
items
list directly without validation.Implement the code to solve the algorithm.
class Player():
def __init__(self, character, kart):
self.character = character
self.kart = kart
self.items = []
def add_item(self, item_name):
valid_items = ["banana", "green shell", "red shell", "bob-omb", "super star", "lightning", "bullet bill"]
if item_name in valid_items:
self.items.append(item_name)
# Example Usage:
player_one = Player("Yoshi", "Dolphin Dasher")
# items = []
player_one.add_item("red shell")
# items = ["red shell"]
player_one.add_item("super star")
# items = ["red shell", "super star"]
player_one.add_item("super smash")
# items = ["red shell", "super star"]
5: R-eview
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
Trace through your code with the following input:
player_one.add_item("red shell")
Verify that player_one.items now contains "red shell".
player_one.add_item("super smash")
Verify that player_one.items remains unchanged since "super smash" is not a valid item.
6: E-valuate
Evaluate the performance of your algorithm and state any strong/weak or future potential work.
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 the additional list of valid items, though it can be considered O(N) where N is the number of items in self.items.