<jupyter_start><jupyter_text>绘制汉诺塔<jupyter_code>import turtle

class Stack:
    """栈的实现"""
    def __init__(self):
        self.items = []
    
    def is_empty(self):
        return self.size() == 0
    
    def push(self,item):
        self.items.append(item)
        
    def pop(self):
        return self.items.pop()
    
    def peek(self):
        if not self.is_empty():
            return self.items[-1]
    
    def size(self):
        return len(self.items)
    
def draw_poles():
    """
    画出汉诺塔的三个柱子
    :return: 
    """
    t = turtle.Turtle()
    t.hideturtle()
    for i in range(3):
        t.up()
        t.pensize(10)
        t.speed(100)
        t.goto(200*(i-1), 100)
        t.down()
        t.goto(200*(i-1), -100)
        t.goto(200*(i-1)-20, -100)
        t.goto(200*(i-1)+20, -100)
        
def create_plates(n):
    """
    制造盘子
    :param n: 制造盘子个数
    :return: 盘子存储的列表
    """
    plates = [turtle.Turtle() for i in range(n)]
    for i in range(n):
        plates[i].color("brown")
        plates[i].pencolor("black")
        plates[i].up()
        plates[i].hideturtle()
        plates[i].shape("square")
        plates[i].shapesize(1,8-i)
        plates[i].goto(-200,-90+20*i)
        plates[i].showturtle()
    return plates

def pole_stack():
    """
    制造poles的栈
    :return: 三个柱子的栈
    """
    poles = [Stack() for i in range(3)]
    return poles

def move_disk(plates,poles,fp,tp):
    """
    移动起始柱子的最顶端的盘子到目的柱子
    :param plates: 盘子列表
    :param poles: 柱子的栈列表
    :param fp: 起始柱子的索引
    :param tp: 移动到目的柱子的索引
    :return: 
    """
    mov = poles[fp].peek()
    plates[mov].goto((fp-1)*200, 150)
    plates[mov].goto((tp-1)*200, 150)
    
    # 确定移动到底部的高度，确保恰好放在原来最上面的盘子上面
    l = poles[tp].size()
    plates[mov].goto((tp-1)*200, -90+20*l)
    
def move_tower(plates,poles,n,from_pole,to_pole,with_pole):
    """
    递归放盘子
    :param plates: 盘子列表
    :param poles: 柱子列表
    :param n: 盘子个数
    :param from_pole: 起始柱子索引
    :param to_pole: 目的柱子索引
    :param with_pole: 中间柱子索引
    :return: 
    """
    if n >= 1:
        # 从起始柱子移动顶部n-1个盘子到中间柱子
        move_tower(plates, poles,n-1,from_pole,with_pole,to_pole)
        
        # 将最底下的那个盘子从起始柱子移动到目的柱子
        move_disk(plates,poles,from_pole,to_pole)
        poles[to_pole].push(poles[from_pole].pop())
        
        #从中间柱子移动顶部n-1个盘子到目的柱子,完成移动
        move_tower(plates,poles,n-1,with_pole,to_pole,from_pole)
    
if __name__ == '__main__':
    screen = turtle.Screen()
    n = 8   # 创建盘子个数
    draw_poles()
    plates = create_plates(n)
    poles = pole_stack()
    
    # 将盘子放在起始柱子
    for i in range(n):
        poles[0].push(i)

    move_tower(plates,poles,n,0,2,1)
    
    screen.exitonclick()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1483657730444363, 'fraction_numerical': 0.030113845023870732, 'mean_word_length': 2.174825174825175, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '664704', 'license': 'MIT', 'n_tokens_mistral': 1325, 'n_tokens_neox': 1268, 'n_words': 176, 'path': 'DataStructure/recursion_part5.ipynb', 'repo_name': 'lb971216008/Use-Python-to-Achieve', 'size': '2723'}