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?
How do we update the catchphrase
attribute of a Villager
object?
catchphrase
attribute.What should be the new value of the catchphrase
attribute for bones
?
"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.
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: 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
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))
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
Villager
object bones
.catchphrase
attribute of bones
.greet_player
method with the given player name.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.
O(1)
because updating an attribute and calling a method are constant-time operations.O(1)
for each Villager
instance because it uses a fixed amount of memory for its attributes and methods.