Skip to content

Instantly share code, notes, and snippets.

@sbrinkmeyer
Created March 2, 2018 20:25
Show Gist options
  • Save sbrinkmeyer/492dc76829adaa2b2d980059c017598b to your computer and use it in GitHub Desktop.
Save sbrinkmeyer/492dc76829adaa2b2d980059c017598b to your computer and use it in GitHub Desktop.
to see if an array has 2 numbers in it that add up to a search number. i was originally struggling with how to iterate through the array multiple times for the two numbers bit. then flying home i realized, hey i can cheat and iterate once through the array using a temp array to just subtract for finding the other number
numers = [ 2, 5, 8, 9, 13, 6, 22 ]
search_numer = 22
def funcyChicken(numberList=None,searchNumber=None):
retValue=False
temp_list = numberList[:]
for x in range(0,len(numberList)-1):
print numberList[x]
del temp_list[x]
other=search_numer-numberList[x]
if other in temp_list:
retValue=True
break
temp_list = numberList[:]
return retValue
return_value = funcyChicken(numers,search_numer)
print 'did we find the number where 2 numbers in the array added up to this? : {}'.format(return_value)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment