Race Results - codepath/compsci_guides GitHub Wiki

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

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

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

Problem Highlights

  • 💡 Difficulty: Easy
  • Time to complete: 5-10 mins
  • 🛠️ Topics: Classes, Lists, Enumeration

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 is the task?
    • Implement a function print_results() to display the race results in order, with each player's position in the race.
HAPPY CASE
Input: race_results = [Player("Peach", "Daytripper"), Player("Mario", "Standard Kart M"), Player("Luigi", "Super Blooper")]
Output: 
1. Peach
2. Mario
3. Luigi
Explanation: The function correctly enumerates through the list and prints each player's character in order of their race position.

EDGE CASE
Input: race_results = []
Output: 
Explanation: The function should handle an empty list without errors and not print any results.

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 list enumeration problems, we want to consider the following approaches:

  • Use enumerate() to loop through the list with an index.
  • Print formatted strings that include both the index (place in the race) and the player's character.

3: P-lan

Plan the solution with appropriate visualizations and pseudocode.

General Idea: Loop through the race_results list and print each player's position along with their character name.

1) Create a function `print_results(race_results)`.
2) Loop through `race_results` using `enumerate()` to get both the index (starting at 1) and the `Player` object.
3) Print each player's position in the format "<place>. <character>".

⚠️ Common Mistakes

  • Forgetting to start the enumerate() index at 1.
  • Not handling an empty list gracefully.

4: I-mplement

Implement the code to solve the algorithm.

def print_results(race_results):
    for place, racer in enumerate(race_results, start=1):
        print(f"{place}. {racer.character}")

5: R-eview

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:
  • race_results = [peach, mario, luigi]
  • Verify that the output is "1. Peach", "2. Mario", "3. Luigi".

6: E-valuate

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

  • Time Complexity: O(N) where N is the number of players in the race_results list.
  • Space Complexity: O(1) since the function uses a constant amount of extra space regardless of input size."
⚠️ **GitHub.com Fallback** ⚠️