Comments (18)
Mutual Recursion
# too big n will generate: [RecursionError: maximum recursion depth exceeded in comparison]
# not a valid algorythom
def is_even(n):
if n == 0:
return True
return is_odd(n - 1)
def is_odd(n):
if n == 0:
return False
return is_even(n - 1)
print(is_even(8))
def is_even(n):
if n == 0:
yield True
yield is_odd(n - 1)
def is_odd(n):
if n == 0:
yield False
yield is_even(n - 1)
print(is_even(8))
# 伪尾递归优化,性能太差,没法用
import types
def tramp(gen, *args, **kwargs):
g = gen(*args, **kwargs)
while isinstance(g, types.GeneratorType):
g=next(g)
return g
tramp(is_even, 8)
Python Tutor mutual recursion 演示
from learning_list.
from learning_list.
from learning_list.
The key of recursion is calling self, directly or indirectly
from learning_list.
read:
- print or return
- Environment Diagrams: Python3 Tutor fact 演示
- recursive tree
write: - leap of faith
write:
from learning_list.
from learning_list.
from learning_list.
cascade:
def cascade(n):
if n < 10:
print(n)
else:
print(n)
cascade(n // 10)
print(n)
cascade(1234)
from learning_list.
def fib(n):
if n == 0:
return 0
elif n == 1:
return 1
else:
return fib(n - 1) + fib(n - 2)
print(fib(6))
from learning_list.
cache version
def fib(n, cache_dict={}):
if n == 0:
return 0
elif n == 1:
return 1
else:
if n in cache_dict:
return cache_dict[n]
cache_n = fib(n - 1) + fib(n - 2)
cache_dict[n] = cache_n
return cache_n
print(fib(6))
from learning_list.
from learning_list.
def fib(n):
curr, next = 0, 1
for _ in range(n):
curr, next = next, curr + next
return curr
print(fib(300))
from learning_list.
from learning_list.
count partitions
def count_part(n, m):
if n < 0:
return 0
if n == 0 and m > 0:
return 1
if m <= 0:
return 0
with_m = count_part(n - m, m)
without_m = count_part(n, m - 1)
return with_m + without_m
print(count_part(6, 3))
Python Tutor Count Partitions 演示
from learning_list.
from learning_list.
from learning_list.
from learning_list.
递归貌似简明
不得不说,树形递归符合人的思维,比起迭代更容易
from learning_list.
Related Issues (20)
- 给函数式程序员的 Hack Pipe:我如何学着停止担忧并爱上那顶小帽子 HOT 1
- 一文说透 TypeScript 的增删查改
- 重温 React hook 基本原理
- rust 中消失的三元去了哪里?
- 如何判断类型中是否有必填字段
- automate chrome by script kit
- applescript 踩坑经验
- hammerspoon 添加自定义菜单,执行 shell 脚本
- karabiner multitouch_extension
- 我的 Mac 使用实践总结
- 活久见之 JS 新见闻:return 写不写还是有关键区别的。
- Rust trait vs TypeScript interface
- React Final Form useFormValuesWatch
- data fetch in react
- type safe useParams
- finally, learn how generate struct with builtin function in Zig
- zig code: auto git push, find suitable target branch and open merge url in browser
- bun preloader for test
- depth protection and loose union
- 初步建立起 RSC 的直观体验 HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from learning_list.