Update Catchphrase - codepath/compsci_guides GitHub Wiki

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

Problem Highlights

  • 💡 Difficulty: Easy
  • Time to complete: 5-10 mins
  • 🛠️ Topics: Classes, Object-Oriented Programming, Attribute Modification

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?
  • How do we update the catchphrase attribute of a Villager object?

    • By directly assigning a new value to the catchphrase attribute.
  • What should be the new value of the catchphrase attribute for bones?

    • The new value should be "ruff it up".
HAPPY CASE
Input: 
bones = Villager("Bones", "Dog", "yip yip")
bones.catchphrase = "ruff it up"
print(bones.greet_player("Samia"))

Output: 
"Bones: Hey there, Samia! How's it going, ruff it up?"

Explanation: 
The `catchphrase` attribute of `bones` is successfully updated to `"ruff it up"`, and the `greet_player` method returns the expected greeting.

EDGE CASE
Input: 
villager = Villager(", ", ")
villager.catchphrase = "hello"
print(villager.greet_player("))

Output: 
": Hey there, ! How's it going, hello?"

Explanation: 
Even with empty string attributes initially, the `catchphrase` attribute can be updated, and the method should handle the output accordingly.

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:

  • Directly modify object attributes as needed.
  • Ensure the methods correctly reflect updated attribute values.

3: P-lan

Plan the solution with appropriate visualizations and pseudocode.

General Idea: Update the catchphrase attribute of the Villager object and call the greet_player method.

1) Create an instance of the `Villager` class for `bones`.
2) Update the `catchphrase` attribute of the `bones` object.
3) Call the `greet_player` method with a specific player's name.
4) Print the result of the method call to verify the update.

⚠️ Common Mistakes

  • Forgetting to update the attribute before calling the method.
  • Incorrectly referencing the attribute to be updated.

4: I-mplement

Implement the code to solve the algorithm.

class Villager:
    def __init__(self, name, species, catchphrase):
        self.name = name
        self.species = species
        self.catchphrase = catchphrase
        self.furniture = []

    def greet_player(self, player_name):
        return f"{self.name}: Hey there, {player_name}! How's it going, {self.catchphrase}?"

# Create an instance of Villager for Bones
bones = Villager("Bones", "Dog", "yip yip")

# Update the catchphrase attribute
bones.catchphrase = "ruff it up"

# Call the greet_player method and print the result
player_name = "Tram" # Replace with your actual name
print(bones.greet_player(player_name))

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 bones.
  • Update the catchphrase attribute of bones.
  • Check the output of the greet_player method with the given player name.

6: E-valuate

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

Assume N represents the number of attributes in the Villager class.

  • Time Complexity: O(1) because updating an attribute and calling a method are constant-time operations.
  • Space Complexity: O(1) for each Villager instance because it uses a fixed amount of memory for its attributes and methods.