-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This solution was very rough, I went a little willy-nilly with if statements near the end as I was trying to figure out why one of my cases wasn't passing.
- Loading branch information
1 parent
9703b00
commit 34b2d46
Showing
1 changed file
with
37 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
def answer(xs): | ||
total = 1 | ||
found = False | ||
zero = False | ||
negatives = [] | ||
if len(xs) == 1: | ||
return str(xs[0]) | ||
for num in xs: | ||
if num == 0: | ||
zero = True | ||
continue | ||
if num > 0: | ||
total = total * num | ||
found = True | ||
if num < 0: | ||
found = True | ||
negatives.append(num) | ||
|
||
if (len(negatives) % 2 == 0): | ||
for n in negatives: | ||
total *= n | ||
elif(len(negatives) == 1): | ||
if zero == True: | ||
return str(0) | ||
if total == 1: | ||
return str(negatives[0]) | ||
return str(total) | ||
else: | ||
negatives.sort() | ||
for n in negatives[:-1]: | ||
total *= n | ||
if total == 1 and found == False: | ||
return str(0) | ||
if total < 0 and zero == True: | ||
return str(0) | ||
return str(total) | ||
print str(answer([-43])) |