from typing import List
A: List[int] = [i for i in range(4, 0, -1)]
height: int = len(A) - 1
B: List[int] = []
C: List[int] = []
def move(n: int, source: List[int], target: List[int], auxiliary: List[int]):
if n > 0:
move(n - 1, source, auxiliary, target)
target.append(source.pop())
draw_disks(A, B, C, height)
print()
input()
move(n - 1, auxiliary, target, source)
def draw_disks(A: List[int], B: List[int], C: List[int], position: int, width: int = 2 * int(max(A))):
if position >= 0:
valueInA: str = " " if position >= len(A) else create_disk(A[position])
valueInB: str = " " if position >= len(B) else create_disk(B[position])
valueInC: str = " " if position >= len(C) else create_disk(C[position])
print("{0:^{width}}{1:^{width}}{2:^{width}}".format(valueInA, valueInB, valueInC, width=width))
draw_disks(A, B, C, position - 1, width)
else:
print("{0:^{width}}{1:^{width}}{2:^{width}}".format("A", "B", "C", width=width))
Important: The challenge of teaching recursion was attemped using recursion. Parallel "if I could use a fairy to solve a smaller problem, then by {idea}, I'd solve the original problem" to "if I could use a fairy to teach you how to teach recursion, then by {telling you I did so to teach you recursion}, I'd solve the original problem. This recursion was pretty small, with only two layers, but the number of layers is not a problem for us once we have a {base case} and an {idea}.
def create_disk(size: int) -> str:
if size == 1:
return "/\\"
else:
return "/" + create_disk(size - 1) + "\\"
move(len(A), A, C, B)
def fibonacci(n: int):
return fibonacci(n - 1) + fibonacci(n - 2) if n > 1 else 1
fibonacci(42)
def factorial(n: int):
return n * factorial(n - 1) if n > 1 else 1
factorial(270)
# import sys
# sys.getrecursionlimit()
# sys.setrecursionlimit(1)
factorial(3750)