Skip to content

Instantly share code, notes, and snippets.

@giwa
Last active March 11, 2016 19:24
Show Gist options
  • Save giwa/b6bf2a7e703cb423f182 to your computer and use it in GitHub Desktop.
Save giwa/b6bf2a7e703cb423f182 to your computer and use it in GitHub Desktop.
>>> sum(range(11))
55
>>> sum([i for i in range(1,11)])
55
>>> sum(i for i in range(1,11))
55
>>> import functools
>>> from functools import reduce
>>> reduce(lambda x,y: x + y, range(1,11))
55
>>> from operator import add
>>> reduce(add, range(1,11))
55
>>> def add_r(n):
if n == 0:
return 0
return n + add_r(n-1)
>>> add_r(10)
55
>>> def add_tr(n, r=0):
if n == 0:
return r
r += n
return add_tr(n-1, r)
>>> add_tr(10)
55
>>> s = 0
>>> for i in range(1, 11):
s += i
>>> s
55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment