递归的三定律
递归算法必须具有基本情况。
递归算法必须改变其状态并向基本情况靠近。
递归算法必须以递归方式调用自身。
整数转换为任意进制字符串
def toStr(n,base): convertString = "0123456789ABCDEF" if n < base: return convertString[n] else: return toStr(n//base,base) + convertString[n%base] print(toStr(1453,16))
栈帧:实现递归字符串转换器
from pythonds.basic.stack import Stack rStack = Stack() def toStr(n,base): convertString = "0123456789ABCDEF" while n > 0: if n < base: rStack.push(convertString[n]) else: rStack.push(convertString[n % base]) n = n // base res = "" while not rStack.isEmpty(): res = res + str(rStack.pop()) return res print(toStr(1453,16))
可视化递归
import turtle myTurtle = turtle.Turtle() myWin = turtle.Screen() def drawSpiral(myTurtle, lineLen): if lineLen > 0: myTurtle.forward(lineLen) myTurtle.right(90) drawSpiral(myTurtle,lineLen-5) drawSpiral(myTurtle,100) myWin.exitonclick()
import turtle def tree(branchLen,t): if branchLen > 5: t.forward(branchLen) t.right(20) tree(branchLen-15,t) t.left(40) tree(branchLen-15,t) t.right(20) t.backward(branchLen) def main(): t = turtle.Turtle() myWin = turtle.Screen() t.left(90) t.up() t.backward(100) t.down() t.color("green") tree(75,t) myWin.exitonclick() main()
谢尔宾斯基三角形
import turtle def drawTriangle(points,color,myTurtle): myTurtle.fillcolor(color) myTurtle.up() myTurtle.goto(points[0][0],points[0][1]) myTurtle.down() myTurtle.begin_fill() myTurtle.goto(points[1][0],points[1][1]) myTurtle.goto(points[2][0],points[2][1]) myTurtle.goto(points[0][0],points[0][1]) myTurtle.end_fill() def getMid(p1,p2): return ( (p1[0]+p2[0]) / 2, (p1[1] + p2[1]) / 2) def sierpinski(points,degree,myTurtle): colormap = ['blue','red','green','white','yellow', 'violet','orange'] drawTriangle(points,colormap[degree],myTurtle) if degree > 0: sierpinski([points[0], getMid(points[0], points[1]), getMid(points[0], points[2])], degree-1, myTurtle) sierpinski([points[1], getMid(points[0], points[1]), getMid(points[1], points[2])], degree-1, myTurtle) sierpinski([points[2], getMid(points[2], points[1]), getMid(points[0], points[2])], degree-1, myTurtle) def main(): myTurtle = turtle.Turtle() myWin = turtle.Screen() myPoints = [[-100,-50],[0,100],[100,-50]] sierpinski(myPoints,3,myTurtle) myWin.exitonclick() main()
汉诺塔游戏
def hanoi(n,x,y,z): if n==1: print(x,'-->',z) else: hanoi(n-1,x,z,y)#将前n-1个盘子从x移动到y上 hanoi(1,x,y,z)#将最底下的最后一个盘子从x移动到z上 hanoi(n-1,y,x,z)#将y上的n-1个盘子移动到z上 n=int(input('请输入汉诺塔的层数:')) hanoi(n,'x','y','z')
探索迷宫问题
def searchFrom(maze, startRow, startColumn): maze.updatePosition(startRow, startColumn) # Check for base cases: # 1. We have run into an obstacle, return false if maze[startRow][startColumn] == OBSTACLE : return False # 2. We have found a square that has already been explored if maze[startRow][startColumn] == TRIED: return False # 3. Success, an outside edge not occupied by an obstacle if maze.isExit(startRow,startColumn): maze.updatePosition(startRow, startColumn, PART_OF_PATH) return True maze.updatePosition(startRow, startColumn, TRIED) # Otherwise, use logical short circuiting to try each # direction in turn (if needed) found = searchFrom(maze, startRow-1, startColumn) or searchFrom(maze, startRow+1, startColumn) or searchFrom(maze, startRow, startColumn-1) or searchFrom(maze, startRow, startColumn+1) if found: maze.updatePosition(startRow, startColumn, PART_OF_PATH) else: maze.updatePosition(startRow, startColumn, DEAD_END) return found
参考:https://facert.gitbooks.io/python-data-structure-cn/4.%E9%80%92%E5%BD%92/4.11.%E6%8E%A2%E7%B4%A2%E8%BF%B7%E5%AE%AB/
最新评论