Giter VIP home page Giter VIP logo

Comments (18)

xxleyi avatar xxleyi commented on May 27, 2024

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.

xxleyi avatar xxleyi commented on May 27, 2024

image

from learning_list.

xxleyi avatar xxleyi commented on May 27, 2024

image

from learning_list.

xxleyi avatar xxleyi commented on May 27, 2024

The key of recursion is calling self, directly or indirectly
image

from learning_list.

xxleyi avatar xxleyi commented on May 27, 2024

read:

from learning_list.

xxleyi avatar xxleyi commented on May 27, 2024

image

from learning_list.

xxleyi avatar xxleyi commented on May 27, 2024

image

from learning_list.

xxleyi avatar xxleyi commented on May 27, 2024

cascade:

def cascade(n):
    if n < 10:
        print(n)
    else:
        print(n)
        cascade(n // 10)
        print(n)
    
cascade(1234)

Python Tutor cascade 演示

image

from learning_list.

xxleyi avatar xxleyi commented on May 27, 2024

image

def fib(n):
    if n == 0:
        return 0
    elif n == 1:
        return 1
    else:
        return fib(n - 1) + fib(n - 2)
        
print(fib(6))

Python Tutor fib recursive 演示

image

from learning_list.

xxleyi avatar xxleyi commented on May 27, 2024

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))

Python Tutor cache version 演示

from learning_list.

xxleyi avatar xxleyi commented on May 27, 2024

image

from learning_list.

xxleyi avatar xxleyi commented on May 27, 2024

image

def fib(n):
    curr, next = 0, 1
    for _ in range(n):
        curr, next = next, curr + next
    return curr
        
        
print(fib(300))

Python Tutor iterative fib 演示

from learning_list.

xxleyi avatar xxleyi commented on May 27, 2024

image

from learning_list.

xxleyi avatar xxleyi commented on May 27, 2024

count partitions

image
image
image

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.

xxleyi avatar xxleyi commented on May 27, 2024

image

from learning_list.

xxleyi avatar xxleyi commented on May 27, 2024

09-Recursion.pptx

from learning_list.

xxleyi avatar xxleyi commented on May 27, 2024

10-Tree-Recursion.pptx

from learning_list.

xxleyi avatar xxleyi commented on May 27, 2024

递归貌似简明

不得不说,树形递归符合人的思维,比起迭代更容易

from learning_list.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.