Skip to content

Add Special Item

Raymond Chen edited this page Aug 9, 2024 · 7 revisions

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-Medium
  • Time to complete: 10-15 mins
  • 🛠️ Topics: Classes, Methods, 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 is the task?
    • Implement a setter method set_character() to update the character attribute with validation.
HAPPY CASE
Input: set_player(""Peach"") 
Output: ""Character updated""
Explanation: ""Peach"" is a valid character, so the `character` attribute is updated.

EDGE CASE
Input: set_player(""Link"") 
Output: ""Invalid character""
Explanation: ""Link"" is not a valid character, so the `character` attribute is not updated.

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 setter method problems, we want to consider the following approaches:

  • Validate the input against a set of allowed values.
  • Update the attribute if validation passes, otherwise, return an error message.

3: P-lan

Plan the solution with appropriate visualizations and pseudocode.

General Idea: Implement a method that validates the input character name and updates the character attribute if the name is valid.

1) Create a method `set_player(self, name)` in the `Player` class.
2) Define a list `valid_characters` containing the allowed character names.
3) Check if `name` is in `valid_characters`.
4) If `name` is valid, update `self.character` to `name` and print ""Character updated"".
5) If `name` is not valid, print ""Invalid character"".

⚠️ Common Mistakes

  • Not properly checking if the input name is in the list of valid characters.
  • Forgetting to use self to update the character attribute.

4: I-mplement

Implement the code to solve the algorithm.

class Player():
    def __init__(self, character, kart):
        self.character = character
        self.kart = kart
        self.items = []

    def set_player(self, name):
        valid_characters = [""Mario"", ""Luigi"", ""Peach"", ""Yoshi"", ""Toad"", ""Wario"", ""Donkey Kong"", ""Bowser""]
        if name in valid_characters:
            self.character = name
            print(""Character updated"")
        else:
            print(""Invalid character"")

# Example 1:
player_one.set_player(""Peach"")  # Output: ""Character updated""

# Example 2:
player_one.set_player(""Link"")  # Output: ""Invalid character""

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.set_player(""Peach"")
  • Verify that player_one.character is now ""Peach"" and the output is ""Character updated"".
  • player_one.set_player(""Link"")
  • Verify that player_one.character remains unchanged and the output is ""Invalid character"".

6: E-valuate

Evaluate the performance of your algorithm and state any strong/weak or future potential work.

  • Time Complexity: O(1) because the operations involved (list check and assignment) are constant time.
  • Space Complexity: O(1) as we only store a small list of valid characters."
Clone this wiki locally