Unit 5 Session 1 (Click for link to problem statements)
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 should the set_catchphrase
method do?
new_catchphrase
and update the catchphrase
attribute if valid, otherwise print an error message.What constitutes a valid catchphrase?
HAPPY CASE
Input:
bones = Villager("Bones", "Dog", "Lazy", "yip yip")
bones.set_catchphrase("soar high")
print(bones.greet_player("Samia"))
Output:
Catchphrase updated
"Bones: Hey there, Samia! How's it going, soar high?"
Explanation:
The `catchphrase` attribute of `bones` is successfully updated to `"soar high"`, and the `greet_player` method returns the expected greeting.
EDGE CASE
Input:
bones = Villager("Bones", "Dog", "Lazy", "yip yip")
bones.set_catchphrase("#?!")
print(bones.greet_player("Samia"))
Output:
Invalid catchphrase
"Bones: Hey there, Samia! How's it going, yip yip?"
Explanation:
The `catchphrase` attribute of `bones` is not updated because the input is invalid, and the `greet_player` method reflects the original catchphrase.
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: Implement the set_catchphrase
method to validate and update the catchphrase
attribute.
1) Define the `set_catchphrase` method in the `Villager` class.
2) Implement validation to check if `new_catchphrase` is less than 20 characters long.
3) Check if all characters in `new_catchphrase` are alphabetic or whitespace.
4) If valid, update the `catchphrase` attribute and print "Catchphrase updated".
5) If invalid, print "Invalid catchphrase".
⚠️ Common Mistakes
new_catchphrase
.new_catchphrase
correctly.Implement the code to solve the algorithm.
class Villager:
def __init__(self, name, species, personality, catchphrase):
self.name = name
self.species = species
self.personality = personality
self.catchphrase = catchphrase
self.furniture = []
def set_catchphrase(self, new_catchphrase):
if len(new_catchphrase) < 20:
for c in new_catchphrase:
if not (c.isalpha() or c.isspace()):
print("Invalid catchphrase")
return
self.catchphrase = new_catchphrase
print("Catchphrase updated")
else:
print("Invalid catchphrase")
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 reflects the updated catchphrase if valid.Evaluate the performance of your algorithm and state any strong/weak or future potential work.
Assume N
represents the number of characters in the new_catchphrase
.
O(N)
because we need to check each character in the new_catchphrase
.O(1)
because we are not using any additional space that scales with input size.