Пример #1
0
from trace_recursion import trace


def str_len(n):
    if not n:
        return 0
    return 1 + str_len(n[1:])


str_len = trace(str_len)
print(str_len("Thomas"))
print(str_len("Ramu"))
print(str_len("ShivaRamaKrishnaIyer"))
Пример #2
0
from trace_recursion import trace


def mul(n, y):
    if n <= 0:
        return 0
    return y + mul(n - 1, y)


mul = trace(mul)
print(mul(5, -4))
print(mul(7, 3))
print(mul(2, 100))
Пример #3
0
from trace_recursion import trace


def GCD(x, y):
    if y == 0:
        # Base Case
        return x
    else:
        # Recursive Case
        return GCD(y, x % y)


GCD = trace(GCD)
print(GCD(72, 96))
print(GCD(3, 6))
print(GCD(73, 37))
Пример #4
0
from trace_recursion import trace


def qucikSort(arr):
    if len(arr) <= 1:
        return arr
    pivot = arr[len(arr) // 2]
    left = [x for x in arr if x < pivot]
    middle = [x for x in arr if x == pivot]
    right = [x for x in arr if x > pivot]
    return qucikSort(left) + middle + qucikSort(right)


qucikSort = trace(qucikSort)
print(qucikSort([1, 4, 6, 66, 76, 66, 5, 5, 5, 5, 90, 1]))
Пример #5
0
"""
Python Recursion Video Course
Robin Andrews - https://compucademy.net/
"""

import sys

sys.path.append("..")  # Adds higher directory to python modules path.
from trace_recursion import trace


def towers_of_hanoi(n, source, destination, auxiliary):
    if n == 1:
        print("Move disk 1 from source", source, "to destination", destination)
        return
    towers_of_hanoi(n - 1, source, auxiliary, destination)
    print("Move disk", n, "from source", source, "to destination", destination)
    towers_of_hanoi(n - 1, auxiliary, destination, source)


n = 3
towers_of_hanoi = trace(towers_of_hanoi)
# A, C, B are the names of the rods.
# They correspond to source, destination, auxiliary
towers_of_hanoi(n, 'A', 'C', 'B')
Пример #6
0
from trace_recursion import trace


def list_sum_iter(lt):
    s = 0
    for num in lt:
        s += num
    return s


def list_sum_recu(lt):
    if not len(lt):
        return 0
    return lt[0] + list_sum_recu(lt[1:])


# print("Iteration")
#print(list_sum_iter([3, 2, 1]))
# print("Recursion")
#print(list_sum_recu([3, 2, 1]))

assert list_sum_recu([1, 2, 4, 5]) == 12
assert list_sum_recu([3]) == 3
assert list_sum_recu([0, 10, -10, 12, -5, -1, -6]) == 0
assert list_sum_recu([14, -14, 14]) == 14
# assert factorial(1000) # Recursion Max depth Error

list_sum_recu = trace(list_sum_recu)
print(list_sum_recu([1, 2, 4, 5]))
Пример #7
0
"""
Python Recursion Video Course
Robin Andrews - https://compucademy.net/
"""
import sys
from trace_recursion import trace

sys.path.append("..")  # Adds higher directory to python modules path.


def gcd_recursive(a, b):
    pass


gcd_recursive = trace(gcd_recursive)
print(gcd_recursive(32, 12))  # From slides
print(gcd_recursive(50, 15))
print(gcd_recursive(42, 28))
print(gcd_recursive(28, 42))
print(gcd_recursive(345, 766))  # Co-prime
"""
Python Recursion Video Course
Robin Andrews - https://compucademy.net/
"""
import sys
from trace_recursion import trace

sys.path.append("..")  # Adds higher directory to python modules path.


def factorial(n):
    if n <= 1:
        # Base case
        return 1
    else:
        # Recursive case
        return n * factorial(n - 1)


factorial = trace(factorial)
print(factorial(5))
Пример #9
0
from trace_recursion import trace


def fibonacci_iter(n):
    a, b = 0, 1
    for i in range(n):
        a, b = b, a + b
    return a


def fibonacci_recu(n):
    if n < 2:
        return n
    return fibonacci_recu(n - 1) + fibonacci_recu(n - 2)


print(fibonacci_recu(10))

print(fibonacci_iter(10))

fibonacci_recu = trace(fibonacci_recu)
fibonacci_recu(5)