-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathc1.py
41 lines (34 loc) · 768 Bytes
/
c1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import collections
def plus(t) :
result = 0
for a in t :
result += a
return result
def times(t) :
result = 1
for a in t :
result *= a
return result
def minus(t) :
if t == [] :
return 0
result = t[0]
for a in t[1:] :
result -= a
return result
def div(t) :
if len(t) != 2 :
return 0
return t[0] / t[1]
def calc(expr) :
if isinstance(expr, int) :
return expr
if callable(expr) :
return expr
if isinstance(expr, collections.Sequence) :
l = map(calc, expr)
return l[0](l[1:])
p1 = (plus, 1, (times, 2, 3))
p2 = [plus, 1, [times, 2, 3]]
# map(lambda x: x[1,2,3], [1,2,3])
# [[1,2,3],[1,2,3,1,2,3],[1,2,3,1,2,3,1,2,3]]