Get Player - codepath/compsci_guides GitHub Wiki
TIP102 Unit 5 Session 1 Standard (Click for link to problem statements)
- 💡 Difficulty: Easy
- ⏰ Time to complete: 5-10 mins
- 🛠️ Topics: Classes, Methods
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?
- Add a method
get_player()
to thePlayer
class and use it to print a formatted string with details about twoPlayer
objects.
- Add a method
HAPPY CASE
Input: Create Player with "Yoshi" and "Super Blooper", another with "Bowser" and "Pirahna Prowler"
Output: "Match: Yoshi driving the Super Blooper vs Bowser driving the Pirahna Prowler"
Explanation: The `get_player()` method correctly formats the details of each player, and these details are used in the final print statement.
EDGE CASE
Input: Create Player with empty strings for character and kart
Output: "Match: driving the vs driving the "
Explanation: The `get_player()` method handles cases with empty strings gracefully, producing an empty output for character and kart.
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 method problems, we want to consider the following approaches:
- Properly formatting and returning strings using class attributes.
- Utilizing f-strings for concise and readable string formatting.
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Add a method to the Player
class to return a formatted string with the player's character and kart. Create a new player and use this method to generate a specific output.
1) Define the `get_player()` method within the `Player` class.
2) Inside `get_player()`, use an f-string to format the output as "<character> driving the <kart>".
3) Create a new instance `player_two` with "Bowser" as the character and "Pirahna Prowler" as the kart.
4) Use the `get_player()` method for both `player_one` and `player_two` to print the match statement.
- Forgetting to use
self
to access class attributes within the method. - Incorrectly formatting the output string.
Implement the code to solve the algorithm.
class Player():
def __init__(self, character, kart):
self.character = character
self.kart = kart
self.items = []
def get_player(self):
return f"{self.character} driving the {self.kart}"
player_two = Player("Bowser", "Pirahna Prowler")
print(f"Match: {player_one.get_player()} vs {player_two.get_player()}")
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")
- player_two = Player("Bowser", "Pirahna Prowler")
- Verify that the output is "Match: Yoshi driving the Super Blooper vs Bowser driving the Pirahna Prowler".
Evaluate the performance of your algorithm and state any strong/weak or future potential work.
- Time Complexity: O(1) because the method runs in constant time.
- Space Complexity: O(1) as we are only storing and returning a formatted string.