Your task is to write a function find_best_game , which takes a string argument stats . The string will contain multiple lines in a format shown in the example below:player1_stats = """Catan: 10 15 30 50 Incan Gold : 30 70 40 140 70 Coup : 50 70 90"""result = find_best_game(player1_stats)print(result) # ['Incan Gold', 70.0]As shown above, each line contains statistics for a single game in form of <name_of_game>: <points> (colon : is the separator between name and points). <points> will be a space-separated sequence of integers, representing the points scored by the player in multiple instances of that game. The function find_best_game returns a list with two items — the first item must be name of the game with highest average points, and the second item must be the average points for that game. In the example shown above, average points for "Catan", "Incan Gold" and "Coup" are 26.25, 70.0 and 70.0, respectively. In case of a tie, just choose the game that appears first.Important notes/assumptions:There will be no blank lines and there will be at least one line in the argument string stats <name_of_game> can contain spaces in middle and those spaces must be preserved. Spaces at beginning and end of the name must be removed.There will be zero or more spaces:at beginning and end of each linebefore and after colon : There will be one or more spaces between integers in <points> There will be at least one integer in <points>
Question
Your task is to write a function find_best_game , which takes a string argument stats . The string will contain multiple lines in a format shown in the example below:player1_stats = """Catan: 10 15 30 50 Incan Gold : 30 70 40 140 70 Coup : 50 70 90"""result = find_best_game(player1_stats)print(result) # ['Incan Gold', 70.0]As shown above, each line contains statistics for a single game in form of <name_of_game>: <points> (colon : is the separator between name and points). <points> will be a space-separated sequence of integers, representing the points scored by the player in multiple instances of that game. The function find_best_game returns a list with two items — the first item must be name of the game with highest average points, and the second item must be the average points for that game. In the example shown above, average points for "Catan", "Incan Gold" and "Coup" are 26.25, 70.0 and 70.0, respectively. In case of a tie, just choose the game that appears first.Important notes/assumptions:There will be no blank lines and there will be at least one line in the argument string stats <name_of_game> can contain spaces in middle and those spaces must be preserved. Spaces at beginning and end of the name must be removed.There will be zero or more spaces:at beginning and end of each linebefore and after colon : There will be one or more spaces between integers in <points> There will be at least one integer in <points>
Solution
Here is a Python function that implements the requirements:
def find_best_game(stats):
# Split the stats string into lines
lines = stats.split('\n')
# Initialize variables to keep track of the best game and its average score
best_game = None
best_avg = 0
# Iterate over each line
for line in lines:
# Split the line into game name and points
game, points = line.split(':')
# Remove leading and trailing spaces from game name
game = game.strip()
# Convert points string into a list of integers
points = list(map(int, points.split()))
# Calculate the average score for this game
avg = sum(points) / len(points)
# If this game's average score is better than the current best, update the best game and average
if avg > best_avg:
best_game = game
best_avg = avg
# Return the best game and its average score
return [best_game, best_avg]
This function works by first splitting the input string into lines, then processing each line individually. For each line, it splits the line into the game name and the points, then calculates the average score for that game. If the average score is higher than the current best, it updates the best game and average score. Finally, it returns the best game and its average score.
Similar Questions
Find all the statistics of the highest valued (Value) player.Your output must be a dict whose keys are all the column names in the dataset, and the corresponding values are the values under that column for the player with the highest Value.You do not have to worry about any ties. There is a unique player in the dataset with the highest Value.
Given the participants' score sheet for your University Sports Day, you are required to find the runner-up score. You are given scores. Store them in a list and find the score of the runner-up.Input FormatThe first line contains . The second line contains an array of integers each separated by a space.
Choose the correct answerWhat is the output of the following statement?print("Sachin scored " + 40 + " runs")OptionsSachinscored40runsSachin scored 40 runsType ErrorSyntax Error
A user that inputs 2 and 1 into the following code will result in what output? board = [ ["-", "-", "-"], ["-", "-", "-"], ["-", "-", "-"] ] col = int(input("X player, select a column: ")) row = int(input("X player, select a row: ")) board[row][col] = "X" print(board[0]) print(board[1]) print(board[2])
You are given a string s. The score of a string is defined as the sum of the absolute difference between the ASCII values of adjacent characters.Return the score of s. Example 1:Input: s = "hello"Output: 13Explanation:The ASCII values of the characters in s are: 'h' = 104, 'e' = 101, 'l' = 108, 'o' = 111. So, the score of s would be |104 - 101| + |101 - 108| + |108 - 108| + |108 - 111| = 3 + 7 + 0 + 3 = 13.Example 2:Input: s = "zaz"Output: 50Explanation:The ASCII values of the characters in s are: 'z' = 122, 'a' = 97. So, the score of s would be |122 - 97| + |97 - 122| = 25 + 25 = 50. Constraints:2 <= s.length <= 100s consists only of lowercase English letters.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.