Skip to content

Instantly share code, notes, and snippets.

@oliland
Last active December 14, 2015 01:19
Show Gist options
  • Save oliland/5005004 to your computer and use it in GitHub Desktop.
Save oliland/5005004 to your computer and use it in GitHub Desktop.
Solution for Array Addition II on coderbyte - allowing negative numbers.
def ArrayAddition(arr):
arr = sorted(arr)
# preprocess array to remove negative numbers
difference = 0
for x in arr:
if x < 0:
x = abs(x)
difference = difference + x
largest = arr.pop() + difference
while (True):
result = largest
for x in reversed(arr):
result = result - x
if result == 0:
return "true"
elif result < 0:
# We may end up below 0 because of negative numbers!
# how do we know when to skip?
# we don't - so we're going to have to do something more clever
# how about "preprocessing" the array to shift negative numbers into positive?
result = result + x
# If we each the end of the list and haven't returned, pop the largest off
try:
arr.pop()
except:
return "false"
# keep this function call here
# to see how to enter arguments in Python scroll down
print ArrayAddition(raw_input())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment