Quick actions

cmd+k|ctrl+k

Navigation

Languages

vinhquang4.5

Snippet info

Language

Python

Visibility

public

Author

vinhquang-pyt

Created

2016-09-26T09:54:44Z

Updated

2016-10-12T04:57:59Z

# Bài 4.5
# -------

# Given an integer list from -10 to 10 except 0, write a function:
# - calculate its sum without using function ``sum``.
# - calculate its product

# Return a tuple (sum, product).

# Input::

#   li = range(-10, 11)
#   li = list(li)
#   li.remove(0)

# Compare output with this::

#   from functools import reduce
#   assert sum_and_product(li) == (sum(li), reduce(lambda x,y: x*y, li))


lst = list(range(-10, 11))
lst.remove(0)
sum_lst = 0
for num in lst:
    sum_lst = sum_lst + int(num)
assert sum(lst) == sum_lst, 'You sum this list incorrect'
print(sum_lst)
INFO