recursion, list comprehensions, object oriented programming, stack data structure
from recursion import *
x = range(20)
x
x = list(x)
x
rtotal(x)
rlength(x)
rmax(x)
tail recursive versions of the above
trtotal(x)
trlength(x)
trmax(x)
toord('abcdefg')
rtoord('abceefg')
rtochar(60,120)
tochar(60,80)
xx
maptree(lambda x: x+1, xx)
list comprehension version of maptree
lcmaptree(lambda x: x+1, xx)
mapmaptree(lambda x: x+1,xx)
rmaptree(lambda x: x+1,xx)
end of recursion examples. start of list comprension examples
from listcomp import *
a1
a2
Create a list with [...] syntax
a2 = [x for x in str(12345)]
a2
[int(x) for x in str(12345)]
[int(x) * int(x) for x in str(12345)]
[(n, n*n) for n in range(5)]
[(x1, x2) for (x1,x2) in enumerate('abcde')]
[x for x in range(10) if x % 2 == 0]
[x for x in range(10) if x > 4]
reduce(lambda x,y: x+y, range(101))
[(x,y,z) for x in range(1,30) for y in range(x,30) for z in range(y,30) if x**2 + y**2 == z**2]
[x if x % 2 else x + 1 for x in range(10)]
Create a set with {...} syntax
{x if x % 2 else x + 1 for x in range(10)}
s3 = {1,2,3,1,2,3}
s3
Create a generator object with (...) syntax.
gen = (x*x for x in range(200))
next(gen)
for n in range(5):
print(next(gen))
end of list comprehension. start of object oriented. Create course class. (pretty confusing, yes?)
from oop import *
m120
c100
course.all()
all is a class method, not an instance method.
course.all("Math")
test()
end of oop. start of stack.
from stack import *
note that stack has an __iter__ method for iteration, as in for or list
s
test(s)
revstr('hello world')
end of stack demo