TIP102 Unit 5 Session 1 Advanced (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?
name
, species
, and catchphrase
.Villager
have?
name
, species
, catchphrase
, and furniture
.HAPPY CASE
Input: ("Apollo", "Eagle", "pah")
Output:
Apollo
Eagle
pah
[]
Explanation: The constructor correctly initializes the properties.
EDGE CASE
Input: (", ", ")
Output:
(empty string)
(empty string)
(empty string)
[]
Explanation: The constructor handles empty strings correctly.
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 Class Constructor problems, we want to consider the following approaches:
Plan the solution with appropriate visualizations and pseudocode.
General Idea: We need to define the __init__()
constructor to initialize the properties of the Villager
class.
1) Define the class `Villager`.
2) Define the `__init__()` constructor with parameters `name`, `species`, and `catchphrase`.
3) Initialize the `name` property with the `name` parameter.
4) Initialize the `species` property with the `species` parameter.
5) Initialize the `catchphrase` property with the `catchphrase` parameter.
6) Initialize the `furniture` property as an empty list.
⚠️ 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 = []
# Example Usage:
apollo = Villager("Apollo", "Eagle", "pah")
print(apollo.name) # Output: Apollo
print(apollo.species) # Output: Eagle
print(apollo.catchphrase) # Output: pah
print(apollo.furniture) # Output: []
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
Initialized properties: name, species, catchphrase, and furniture Verify the example usage produces the correct output.
Evaluate the performance of your algorithm and state any strong/weak or future potential work.
Assume N represents the number of instances created.