Skip to content

Calculate Tournament Placement

kyra-ptn edited this page Aug 25, 2024 · 4 revisions

Unit 5 Session 2 (Click for link to problem statements)

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

Problem Highlights

  • 💡 Difficulty: Medium
  • Time to complete: 15-20 mins
  • 🛠️ Topics: Object-Oriented Programming, Methods, List Manipulation

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 happens if there are no opponents?
    • If there are no opponents, the player automatically gets 1st place.
HAPPY CASE
Input: player1 = Player("Mario", "Standard", [1, 2, 1, 1, 3]), opponents = [Player("Luigi", "Standard", [2, 1, 3, 2, 2]), Player("Peach", "Standard", [3, 3, 2, 3, 1])]
Output: 1
Explanation: Mario's average race outcome is 1.6, which is lower than Luigi's 2.0 and Peach's 2.4, so Mario gets 1st place.

EDGE CASE
Input: player1 = Player("Mario", "Standard", [2, 2, 2, 2, 2]), opponents = [Player("Luigi", "Standard", [2, 2, 2, 2, 2])]
Output: 1
Explanation: Both players have the same average race outcome, so Mario gets 1st place because the function counts the player itself as 1st.

EDGE CASE
Input: player1 = Player("Mario", "Standard", [1, 1, 1, 1, 1]), opponents = []
Output: 1
Explanation: Without any opponents, Mario automatically gets 1st place.

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:

  • Calculation and comparison of averages
  • Iteration over a list of objects to compare attributes

3: P-lan

Plan the solution with appropriate visualizations and pseudocode.

General Idea: Calculate the average race outcome for the player and each opponent. Count how many opponents have a better (lower) average race outcome than the player to determine their rank.

1) Calculate the average race outcome for the player.
2) Initialize the rank to 1.
3) Iterate over each opponent:
    a) Calculate the average race outcome for the opponent.
    b) If the opponent's average is better (lower), increase the player's rank.
4) Return the player's rank.

⚠️ Common Mistakes

  • Forgetting to handle the case where the list of race outcomes is empty.
  • Not correctly handling ties in the average outcomes, leading to incorrect rankings.

4: I-mplement

Implement the code to solve the algorithm.

class Player:
    def __init__(self, character, kart, outcomes):
        self.character = character
        self.kart = kart
        self.items = []
        self.race_outcomes = outcomes

    def get_tournament_place(self, opponents):
        # Calculate own average race outcome
        own_average = sum(self.race_outcomes) / len(self.race_outcomes) if self.race_outcomes else float('inf')
        
        # Start counting at 1 for the player themselves
        rank = 1
        
        # Calculate and compare with each opponent's average race outcome
        for opponent in opponents:
            opponent_average = sum(opponent.race_outcomes) / len(opponent.race_outcomes)
            if opponent_average < own_average:
                rank += 1 # The opponent did better (lower) than we did!
        
        return rank

5: R-eview

Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.

  • Check the calculated averages and the resulting rank to ensure accuracy.

Example:

player1 = Player("Mario", "Standard", [1, 2, 1, 1, 3])
player2 = Player("Luigi", "Standard", [2, 1, 3, 2, 2])
player3 = Player("Peach", "Standard", [3, 3, 2, 3, 1])

opponents = [player2, player3]
print(f"{player1.character} was number {player1.get_tournament_place(opponents)}")
# Expected Output: "Mario was number 1"

6: E-valuate

Evaluate the performance of your algorithm and state any strong/weak or future potential work.

  • Time Complexity: O(N * M) where N is the number of opponents and M is the number of races, as we need to calculate the average race outcome for each player.
  • Space Complexity: O(1) because we are only using a constant amount of extra space to store the rank and averages.
Clone this wiki locally