New Horizons - codepath/compsci_guides GitHub Wiki

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

Problem Highlights

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

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?
  • What should be the attributes of the Villager class?

    • The Villager class should have name, species, catchphrase, and furniture attributes.
  • How do we instantiate a new Villager object?

    • By calling the Villager constructor with the appropriate parameters.
  • What should the default value of the furniture attribute be?

    • The furniture attribute should be an empty list.
HAPPY CASE
Input: 
apollo = Villager("Apollo", "Eagle", "pah")
print(apollo.name) 
print(apollo.species) 
print(apollo.catchphrase) 
print(apollo.furniture) 

Output: 
"Apollo"
"Eagle"
"pah"
[]

Explanation: 
The `Villager` object is instantiated correctly with the given attributes, and the default furniture list is empty.

EDGE CASE
Input: 
villager = Villager(", ", ")
print(villager.name) 
print(villager.species) 
print(villager.catchphrase) 
print(villager.furniture) 

Output: 
"
"
"
[]

Explanation: 
Even with empty string attributes, the `Villager` object should be instantiated correctly, and the default furniture list should still be empty.

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:

  • Define classes with appropriate attributes and methods.
  • Use constructors to initialize object instances.
  • Ensure default values are set correctly.

3: P-lan

Plan the solution with appropriate visualizations and pseudocode.

General Idea: Define the Villager class with the necessary attributes and create an instance of this class.

1) Define the `Villager` class.
2) Implement the constructor to initialize `name`, `species`, `catchphrase`, and `furniture`.
3) Instantiate a `Villager` object with the given attributes.
4) Verify the attributes by printing them out.

⚠️ Common Mistakes

  • Forgetting to initialize all the required attributes.
  • Not setting default values correctly.
  • Incorrect indentation in the constructor.

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 = []

# Instantiate the Villager class
apollo = Villager("Apollo", "Eagle", "pah")

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 apollo.
  • Check the values of apollo.name, apollo.species, apollo.catchphrase, and apollo.furniture.

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 initializing an object and setting attributes are constant-time operations.
  • Space Complexity: O(1) for each Villager instance because it uses a fixed amount of memory for its attributes.