Skip to content

Commit 215ed06

Browse files
committed
sum.py file added
1 parent 9b0e9b9 commit 215ed06

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

sum.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
def mysum(l, start=0):
2+
'''
3+
sum function. Python example/exercise.
4+
5+
l -> list.
6+
start -> start point, that added to sum.
7+
8+
function can also sum list with strings. In this case it converts
9+
all list items to str. In this case start point is - empty string
10+
(c) Alexander Kilinkarov
11+
'''
12+
13+
# check if there is string in list. And if there is, so convert
14+
# all list items to str
15+
for i in l:
16+
if isinstance(i, str):
17+
l = map(str, l)
18+
if start==0:
19+
start=''
20+
else:
21+
start = str(start)
22+
break
23+
24+
# s is total sum
25+
s = start
26+
for i in l:
27+
s+=i
28+
return s
29+
30+
print(mysum([1, 2, 3], 6))
31+
print(mysum(['1', '2', '3'], 'asdsa'))

0 commit comments

Comments
 (0)