Skip to content

Instantly share code, notes, and snippets.

@ShaneRich5
Last active November 1, 2017 01:43
Show Gist options
  • Save ShaneRich5/5e215249cba2551e6590e21cefcce948 to your computer and use it in GitHub Desktop.
Save ShaneRich5/5e215249cba2551e6590e21cefcce948 to your computer and use it in GitHub Desktop.
Google's foo.bar challenges
def answer(xs):
negatives = []
result = 0
if len(xs) == 1:
return xs[0]
for element in xs:
if element > 0:
if result == 0:
result = element
else:
result *= element
elif element < 0:
negatives.append(element)
if negatives == [] or (len(negatives) == 1 and result == 0):
return result
if result == 0:
result = 1
result = reduce(lambda x, y: x * y, negatives) * result
if len(negatives) % 2 == 0:
return result
else:
return result / max(negatives)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment