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?
What additional method should be added to the Villager
class?
greet_player
method.What should the greet_player
method return?
"{self.name}: Hey there, {player_name}! How's it going, {self.catchphrase}?"
How do we create a new instance of the Villager
class?
Villager
constructor with the appropriate parameters for the new villager.HAPPY CASE
Input:
bones = Villager("Bones", "Dog", "yip yip")
print(bones.greet_player("Tram"))
Output:
"Bones: Hey there, Tram! How's it going, yip yip?"
Explanation:
The `Villager` object `bones` is instantiated correctly and the `greet_player` method returns the expected greeting.
EDGE CASE
Input:
villager = Villager(", ", ")
print(villager.greet_player("))
Output:
": Hey there, ! How's it going, ?"
Explanation:
Even with empty string attributes, the `Villager` object should handle the method call without errors, though the output will be empty as per the input values.
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: Define the greet_player
method in the Villager
class and create a new instance of Villager
.
1) Define the `greet_player` method in the `Villager` class.
2) Implement the method to return a formatted greeting string.
3) Create a new instance of the `Villager` class for `bones`.
4) Call the `greet_player` method with a specific player's name.
5) Print the result of the method call.
⚠️ Common Mistakes
Villager
object with the correct attributes.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")
# 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
.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 calling a method and returning a string are constant-time operations.O(1)
for each Villager
instance because it uses a fixed amount of memory for its attributes and methods.