Write a program which allows the user to enter a list of numbers.Write a program which allows the user to enter a list of numbers,then works out whether there are any two numbers in the list which sum to 100.Hint:you could loop through the numbers in
来源:学生作业帮助网 编辑:六六作业网 时间:2024/11/27 18:12:41
Write a program which allows the user to enter a list of numbers.Write a program which allows the user to enter a list of numbers,then works out whether there are any two numbers in the list which sum to 100.Hint:you could loop through the numbers in
Write a program which allows the user to enter a list of numbers.
Write a program which allows the user to enter a list of numbers,then works out whether there are any two numbers in the list which sum to 100.Hint:you could loop through the numbers in the list,and for each number,loop through the list again looking for a number that adds to it to make 100.However,there are other ways to do this too.
Write a program which allows the user to enter a list of numbers.Write a program which allows the user to enter a list of numbers,then works out whether there are any two numbers in the list which sum to 100.Hint:you could loop through the numbers in
def checkio(data, target):
data.sort()
i = 0
j = len(data)-1
while(i < j):
if data[i] + data[j] > target:
j -= 1
elif data[i] + data[j] < target:
i += 1
else:
return str(data[i]) + ' + ' + str(data[j]) + ' = ' + str(target)
return 'No solution!'
if __name__ == '__main__':
data = [10,2,3,87,11,1,2,41,34,56,44,93,5,16,74,23,22,19]
target = 100;
print data
print checkio(data,target)