Created
May 9, 2020 18:33
-
-
Save xxbinxx/3b8923801b8039ab61af85d0acebddf6 to your computer and use it in GitHub Desktop.
Python program to return True if sum of any 3 numbers in array is equal to key else return false
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
""" | |
Created on Sat May 9 23:28:42 2020 | |
@author: bineet | |
""" | |
# given an array and if sum of any 3 numbers in array is equal to key | |
# then return true else false | |
arr = [-1, 0, 2, 4, 5, 3] | |
key = 3 | |
def check_sum(arr): | |
for i in range(0, len(arr)): | |
for j in range(i+1, len(arr)): | |
for k in range(j+1, len(arr)): | |
if arr[i] + arr[j] + arr[k] == key: | |
return True | |
return False | |
# test cases | |
assert check_sum([-1, 0, 2, 4, 5, 3]) == True | |
assert check_sum([-1, 0, 2, -4, 5, 3]) == True | |
assert check_sum([-1, 0, 2, 41, 51, 3]) == False | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment