马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 MIQIWEI 于 2020-8-16 17:14 编辑
get_pair_of_largest_numbers(numbers),该函数将数字列表作为参数,并返回具有最大和的相邻数字对作为元组。您可以假定列表不为空(至少两个或更多元素),并且每个元素都是有效数字。
def get_pair_of_largest_numbers(numbers):
result = [numbers[index] + numbers[index+1] for index in range(len(numbers)-1)]
max_value = result[0]
for index in range(len(result)):
if result[index] > max_value:
max_value = result[index]
return max_value
Test Result
print(get_pair_of_largest_numbers([10.5, -3, -1, 11]))
(-1, 11)
print(get_pair_of_largest_numbers([ 10, 20, 3, 31, 8, 42]))
(8, 42)
print(get_pair_of_largest_numbers([ 10.5, -3]))
(10.5, -3)
def get_pair_of_largest_numbers(numbers):
result = [numbers[index] + numbers[index+1] for index in range(len(numbers)-1)]
max_value = result.index(max(result))
return tuple(numbers[max_value:max_value+2])
print(get_pair_of_largest_numbers([10.5, -3, -1, 11]))
print(get_pair_of_largest_numbers([ 10, 20, 3, 31, 8, 42]))
print(get_pair_of_largest_numbers([ 10.5, -3]))
|