fork download
  1.  
  2. # from http://c...content-available-to-author-only...e.com/recipes/474088-tail-call-optimization-decorator/
  3. import sys
  4. sys.setrecursionlimit(10) # set low limit to demonstrate that there is no stack overflow (tail call version)
  5.  
  6. class TailRecurseException:
  7. def __init__(self, args, kwargs):
  8. self.args = args
  9. self.kwargs = kwargs
  10.  
  11. def tail_call_optimized(g):
  12. """
  13. This function decorates a function with tail call
  14. optimization. It does this by throwing an exception
  15. if it is it's own grandparent, and catching such
  16. exceptions to fake the tail call optimization.
  17.  
  18. This function fails if the decorated
  19. function recurses in a non-tail context.
  20. """
  21. def func(*args, **kwargs):
  22. f = sys._getframe()
  23. if f.f_back and f.f_back.f_back \
  24. and f.f_back.f_back.f_code == f.f_code:
  25. raise TailRecurseException(args, kwargs)
  26. else:
  27. while 1:
  28. try:
  29. return g(*args, **kwargs)
  30. except TailRecurseException, e:
  31. args = e.args
  32. kwargs = e.kwargs
  33. func.__doc__ = g.__doc__
  34. return func
  35.  
  36. @tail_call_optimized
  37. def countup(N, n=0):
  38. print(n)
  39. if n < N:
  40. countup(N, n + 1)
  41.  
  42. n = int(raw_input())
  43. countup(n)
  44.  
  45.  
  46. def countdown(n):
  47. if n != 0:
  48. countdown(n-1)
  49. print(n)
  50.  
  51. countdown(n)
Runtime error #stdin #stdout #stderr 0.08s 8888KB
stdin
100
stdout
0
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
stderr
Traceback (most recent call last):
  File "prog.py", line 51, in <module>
    countdown(n)
  File "prog.py", line 48, in countdown
    countdown(n-1)
  File "prog.py", line 48, in countdown
    countdown(n-1)
  File "prog.py", line 48, in countdown
    countdown(n-1)
  File "prog.py", line 48, in countdown
    countdown(n-1)
  File "prog.py", line 48, in countdown
    countdown(n-1)
  File "prog.py", line 48, in countdown
    countdown(n-1)
  File "prog.py", line 48, in countdown
    countdown(n-1)
  File "prog.py", line 48, in countdown
    countdown(n-1)
  File "prog.py", line 48, in countdown
    countdown(n-1)
RuntimeError: maximum recursion depth exceeded