|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
问题:
Write a function defined as:
def linear_interpolate_index (slist, match_value, low_index, high_index):
that takes an ordered list of ascending numbers and a desired 'match' value to search for in the list.
The partition of interest in the list is defined by the index of the low and high values in the list. The function uses linear interpolation between slist[high] and slist[low] to return the index to try interpolation searching for the match value or None if the desired match falls outside the range slist[low] to slist[high-1)].
For example, a call to linear_interpolate_index(list(range(0, 20, 2)), 6, 0, 9) would return an index value of 3.
我的答案:
def linear_interpolate_index (slist, match_value, low_index, high_index):
# YOUR CODE HERE
low_value = slist[low_index]
high_value = slist[high_index]
if match_value < low_value or match_value > high_value:
return None
if low_value == high_value:
return low_index
interpolated_index = low_index + ((match_value - low_value) / (high_value - low_value)) * (high_index - low_index)
return int(interpolated_index)
slist = list(range(0, 20, 2))
result = linear_interpolate_index(slist, 6, 0, 9)
print(result)
但是系统提醒我list index out of range
求大神帮我瞅瞅 |
|