Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
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
Zach-Houston authored Jun 26, 2017
1 parent 9703b00 commit 34b2d46
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions largest_product.py
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]))

0 comments on commit 34b2d46

Please sign in to comment.