To be done in python 3.0. Please comment your code thoroughly, I don\'t want to
ID: 3735298 • Letter: T
Question
To be done in python 3.0. Please comment your code thoroughly, I don't want to copy it, I need to understand it. Thank you!
Question 1 (14 points): Purpose: To practice testing functions you wrote yourself. 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 measure of a golfer's potential ability. It is calculated using the average of the best (smallesti 10 handicap differentials of the golfer's past 20 total rounds, multiplied by 0.96. Write the following functions: e handicap differential (score, course_rating, slope_rating) takes 3 numeric arguments and re- turns a calculated handicap differential For example, given the inputs: score 80, course rating-72 and slope_rating-133, the result is 6.8. score and course_rating mustbe > 17 and slope_rating must be> 54 andExplanation / Answer
#Let us first implement the function that calculates handicap differential
def handicap_differential (score, course_rating, slope_rating):
""" This function calculates and returns the handicap differential
from score, course ratin and score rating """
#If the parameters are within the limits specified by the question
if score > 17 & course_rating > 17 & slope_rating > 54 & slope_rating < 156:
return (score-course_rating)*113/slope_rating
else:
#The maximum handicap index value
return 36.4
#The function to calculate the differential index
def handicap_index (differential_list):
""" This function takes a list as input and returns 0.96*average of the last 10 best rounds"""
l = len(differential_list)
#If the list is less than 20 elements
if l < 20:
return 36.4
#If we have 20 elements to work with
else:
#In case the list has more than 20 elements, we are taking the first 20
#If there are exactly 20 elements doing this won't change anything
#Note: The splice operator(:20) here means 'the first twenty elements'
differential_list = differential_list[:20]
#Sorting the list in ascending order using an inbuilt python function
differential_list.sort()
#After sorting, the first 10 elements will be the 10 lowest values
#Hence storing them in a new list
lowest_vals = differential_list[:10]
#Average = sum/number of elements
#We already know there are exactly 10 elements by this point
#We need the exact answer in a decimal form
#Hence we are taking 10 as a float value so that it doesn't get implicitly converted to integer
#This isn't really necessary in python 3, but just making sure :D
avg = sum(lowest_vals)/float(10)
#Return average
return avg*0.96
#White Box Tests
#In white box tests one has to try and account for every possible test case
#Testing handicap_differential
handicap_differential(15, 35, 100)
handicap_differential(18, 15, 101)
handicap_differential(18, 27, 35)
handicap_differential(18, 27, 240)
handicap_differential(10. 12, 150)
handicap_differential(9, 16, 11)
handicap_differential(10, 11, 256)
handicap_differential(24, 56, 127)
#Testing handicap_index
handicap_index([23, 10, 29, 11, 5.6, 36.4, 12, 11, 10, 6.6, 4.4, 23.9, 10, 6, 5.5, 18.9, 36.3, 20.4, 23. 3])
handicap_index([23, 10, 29, 11, 5.6, 36.4, 12, 11, 10)
handicap_index([23,4, 0.5, 12.4, 23, 4, 13, 15, 5.6, 6.7, 8.9, 12.3, 14.5, 20]
handicap_index[1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9, 10, 11, 12, 13, 14, 15, 16, 17,18,19,2.2]
handicap_index[1,1,1,1,1,1,1,1,1 5, 4, 5, 6, 8, 9, 3.4, 2]
handicap_index[1,6,8,2,5.5,6.8,2.6,2.98,1,1,2,3,7,8,3,5.6, 10.7]
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.