Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Questions 1. Provide code to answer the questions below. You should also provide a transcript of a Python inter- active session that shows the result
Questions 1. Provide code to answer the questions below. You should also provide a transcript of a Python inter- active session that shows the result of executing the test case given for each question. All parts of this question use the Point-a-rally scoring (PARS) system. (a) Define a function game(ra, rb) that implements the algorithm below to simulate a single game of squash between players with abilities ra and ). The function should return the final score as a tuple. Start Generale random number r between 0 and 1 is rcp? Yes Increase a's score No Calculate probability P that a wins a point No Increase b's score Initialise variables to store scores Game over? Yes Return scores Stop If the random seed is set to 57, then calling the function with arguments 70 and 30 should return (11, 5). [Functionality: 7 marks] (b) Define a function winProbability(ra, rb, n) that simulates n games in order to estimate the probability that a player with ability ra will win a game against a player of ability rs. Given sufficient simulations, calling the function with abilities 70 and 30 should give the answer 0.98 when printed to 2 decimal places. [Functionality: 4 marks) (c) Assume players abilities are given in a csv file with the format: player a ability, player b ability, 60, 20, 100, 55, 50, 40, 20, 70, 95, 85, Write a function that reads in a csv file of this format and returns a list of tuples containing each pair of abilities. 3 Questions 1. Provide code to answer the questions below. You should also provide a transcript of a Python inter- active session that shows the result of executing the test case given for each question. All parts of this question use the Point-a-rally scoring (PARS) system. (a) Define a function game(ra, rb) that implements the algorithm below to simulate a single game of squash between players with abilities ra and ). The function should return the final score as a tuple. Start Generale random number r between 0 and 1 is rcp? Yes Increase a's score No Calculate probability P that a wins a point No Increase b's score Initialise variables to store scores Game over? Yes Return scores Stop If the random seed is set to 57, then calling the function with arguments 70 and 30 should return (11, 5). [Functionality: 7 marks] (b) Define a function winProbability(ra, rb, n) that simulates n games in order to estimate the probability that a player with ability ra will win a game against a player of ability rs. Given sufficient simulations, calling the function with abilities 70 and 30 should give the answer 0.98 when printed to 2 decimal places. [Functionality: 4 marks) (c) Assume players abilities are given in a csv file with the format: player a ability, player b ability, 60, 20, 100, 55, 50, 40, 20, 70, 95, 85, Write a function that reads in a csv file of this format and returns a list of tuples containing each pair of abilities. 3
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started