forked from smaranjitghose/Foobar_Challenge
-
Notifications
You must be signed in to change notification settings - Fork 2
/
power_hungry.py
42 lines (32 loc) · 1.11 KB
/
power_hungry.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
def solution(xs):
# handle simple edge cases
if len(xs) == 0:
return str(0)
if len(xs) == 1:
return str(xs[0])
# split input into positive/negative lists
positive_numbers = []
negative_numbers = []
for n in xs:
if n > 0: positive_numbers.append(n)
elif n < 0: negative_numbers.append(n)
# cache list counts
positive_count = len(positive_numbers)
negative_count = len(negative_numbers)
# handle single negative panel edge case
if negative_count == 1 and positive_count == 0:
return str(0)
# handle all zeros edge case
if negative_count == 0 and positive_count == 0:
return str(0)
# calculate positive power output
power_output = 1L
for n in positive_numbers:
power_output *= n
# remove "largest" negative panel in odd arrangements
if negative_count % 2 == 1:
negative_numbers.remove(max(negative_numbers))
# calculate negative power output
for n in negative_numbers:
power_output *= n
return str(power_output)