-
Notifications
You must be signed in to change notification settings - Fork 244
Instantiate an Instance of Player
TIP102 Unit 5 Session 1 Standard (Click for link to problem statements)
- 💡 Difficulty: Easy
- ⏰ Time to complete: 5-10 mins
- 🛠️ Topics: Classes, Constructors
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 is the task?
- Instantiate an instance of the
Player
class and correctly initialize the attributes.
- Instantiate an instance of the
HAPPY CASE
Input: Instantiate Player with "Yoshi" as character and "Super Blooper" as kart
Output: Player's character is "Yoshi", kart is "Super Blooper", and items list is empty.
Explanation: We correctly initialize the Player object with the provided values and an empty items list.
EDGE CASE
Input: Instantiate Player with an empty string for character and kart
Output: Player's character is ", kart is ", and items list is empty.
Explanation: We check if the object initializes correctly with empty strings.
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 instantiation problems, we want to consider the following approaches:
- Use of constructors to initialize object attributes.
- Handling default values or optional parameters if applicable.
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Create an instance of the Player
class and initialize its attributes based on the input parameters.
1) Define a class `Player` with a constructor that takes `character` and `kart` as parameters.
2) Inside the constructor, set `self.character` to the character parameter.
3) Set `self.kart` to the kart parameter.
4) Initialize `self.items` as an empty list.
5) Instantiate `player_one` with "Yoshi" as the character and "Super Blooper" as the kart.
- Forgetting to initialize
self.items
as an empty list. - Confusing attribute names or missing the
self
keyword.
Implement the code to solve the algorithm.
class Player():
def __init__(self, character, kart):
self.character = character
self.kart = kart
self.items = []
player_one = Player("Yoshi", "Super Blooper")
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
- Trace through your code with the following input:
- player_one = Player("Yoshi", "Super Blooper")
- Verify that player_one.character is "Yoshi", player_one.kart is "Super Blooper", and player_one.items is an empty list.
Evaluate the performance of your algorithm and state any strong/weak or future potential work.
- Time Complexity: O(1) because the operations inside the constructor run in constant time.
- Space Complexity: O(1) as we are only storing a few attributes for a single object.