Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Python 3.x This is chapter 15, Excersice 3 Question 1 (14 points): you wrote you

ID: 3731273 • Letter: P

Question

Python 3.x

This is chapter 15, Excersice 3

Question 1 (14 points): you wrote yourself Purpose: To practice testing functions Degree of Difficulty: Moderate A golfer's handicap differential is calculated by the following equation: Score - Course Rating)113/Slope Rating A golfer's handicap index is a numerical measure of a golfer's potential ability. It is calculated using the average of the best 10 handicap differentials of the golfer's past 20 total rounds, multiplied by 0.96. Write the following functions: handicap differential(score, course_rating, slope_rating) takes 3 numeric arguments and re- tums a calculated handicap differential. For example, given the inputs score = 80, course-rating 72. andslope_rating-133, the result is 6.8. score and course_rating must be> 17andslope_rating must be> 54 ad 156; otherwise, the function should return 36.4 (the maximum handicap index). handicap_index (differential_list) takes a list of handicap differentials and returns the calculated handicap index. For example, given the input [6.6,8.1,9.2,8.3,8.5,3.0,7.7,9.0,6.1,8.9,9.9,6.5,8.1,7.2,5.2,3.4,9.0,9.1,3.7,9.1] the result is 7.0. If 20 rounds are given as input, only the 20 most recent rounds should be used in the calculation. Assume that round at index [o] is the oldest round and index[-1] is the most recent To show that your functions are working correctly, implement a test driver which thoroughly tests both functions with a variety of white-box tests (at least six per function). You can use either plain if-statements (as demonstrated in readings 15.2.4) or a list-of-dictionaries to implement your test driver (as demonstrated in class during chapter 15 exercise 3)

Explanation / Answer

def handicap_differential(score, course_rating, slope_rating):
if ( course_rating > 17 and (slope_rating > 54 and slope_rating < 156):
# if condition not satisfied return maximum handicap index which is 36.4
return 36.4
else:
# calculate handicap differential
return (score - course_rating) * 113/slope_rating
  


def handicap_index(differential_list):
size = len( differential_list) #calculate size of list
if size < 20:
return 36.4
elif size == 20:
sorted_list = sorted(differential_list) # sort the list
sliced_list = sorted_list[ : 10 ] # select the first 10 numbers from the list which are samllest
average = sum(sliced_list)/len(sliced_list) # calculate average
return average * 0.96 # return average * 0.96
elif size > 20 :
selected_list = differential_list[ -20: ] # select 20 most recent elements
sorted_list = sorted(selected_list) # sort the list
sliced_list = sorted_list[ : 10 ] # select first 10 elements which will be smallest
average = sum(sliced_list)/len(sliced_list) # calculate average
return average * 0.96 # return average * 0.96