Skip to content

Commit 6231bf8

Browse files
authored
Merge pull request #81 from mikelane/beginner/#8/sum-earnings
8 Update filename
2 parents 39e1d95 + 4443746 commit 6231bf8

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

beginner/sum-earnings-mikelane.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import functools
2+
3+
4+
def sum_earnings(s: str) -> int:
5+
"""
6+
Challenge
7+
- Write a function that accepts a comma-separated string input of
8+
earning/spending activity and returns the sum of earnings as a
9+
single int value.
10+
11+
- If at any point the spending (negative) value is greater than the
12+
sum of earned (positive) values before it then the streak ends and
13+
the count should start over
14+
15+
We have a list in string type separated by commas that represented
16+
buy or sell activity. Positive value for selling and negative value
17+
for buying activity. For example, in the following string, this
18+
user sold something for $7 on the 2nd day, and something for $2 on
19+
the 4th day, and then bought something for $12 on the 5th day, and
20+
so on.
21+
22+
>>> sum_earnings('0,7,0,2,-12,3,0,2')
23+
5
24+
25+
This user's highest earnings streak is $5, which started on the 6th
26+
day and ended on the 8th day. The streak does not start before the
27+
6th day because the user spent $12 on the 5th and broke earlier
28+
streak on $9.
29+
30+
>>> sum_earnings('1,3,-2,1,2')
31+
5
32+
33+
Notes
34+
If the user did not do anything (i.e. 0,0,0,0,0) or only bought
35+
things without selling anything (i.e. -4,-3,-7,-1), then it should
36+
output with 0.
37+
38+
>>> sum_earnings('0,0,0,0,0')
39+
0
40+
>>> sum_earnings('-4,-3,-7,-1')
41+
0
42+
43+
Your program should be able to handle a comma-separated string
44+
consisting of any number of values. Your program should also be
45+
able to handle invalid input. If an invalid input is given, it
46+
should output 0.
47+
48+
some examples of invalid input:
49+
50+
>>> sum_earnings('qwerty')
51+
0
52+
>>> sum_earnings(',,3,,4')
53+
0
54+
>>> sum_earnings('1,2,v,b,3')
55+
0
56+
"""
57+
try:
58+
return functools.reduce(lambda x, y: max(0, x + y), map(int, s.split(',')))
59+
except ValueError:
60+
return 0

0 commit comments

Comments
 (0)