Given a list of numbers and a number k, write a Python program to check whether the sum of any two numbers from the list is equal to k or not.
For example, given [1, 5, 11, 5] and k = 16, return true since 11 + 5 is 16.
def check_sum(nums, k): for i in range(len(nums)): for j in range(i+1, len(nums)): if nums[i] + nums[j] == k: return True return False print(check_sum([12, 5, 0, 5], 10)) print(check_sum([20, 20, 4, 5], 40)) print(check_sum([1, -1], 0)) print(check_sum([1, 1, 0], 0))
True True True False
Given a list of numbers and a number k, write a Python program to check whether the sum of any two numbers from the list is equal to k or not
Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.