Last active
November 1, 2017 01:43
-
-
Save ShaneRich5/5e215249cba2551e6590e21cefcce948 to your computer and use it in GitHub Desktop.
Google's foo.bar challenges
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
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