Presentation is loading. Please wait.

Presentation is loading. Please wait.

Barb Ericson Georgia Tech

Similar presentations


Presentation on theme: "Barb Ericson Georgia Tech"— Presentation transcript:

1 Barb Ericson ericson@cc.gatech.edu Georgia Tech
Recursion Barb Ericson Georgia Tech

2 Recursion When a function calls itself def downUp(word ): print word
Used to break problems into smaller problems Used to solve recursion problems def downUp(word ): print word if len(word )==1: return downUp(word [1:]) >>> downUp("Hello")

3 Print filenames in a directory tree
import os import java.io.File as File def printAllFiles(directory ): files = os.listdir(directory) for file in files: fullname = directory+"/"+file if isDirectory(fullname ): printAllFiles(fullname) else: print fullname def isDirectory(filename ): filestatus = File(filename) return filestatus.isDirectory ()

4 Decrease Red Recursively
def decreaseRedR(aList ): if aList == []: # empty return setRed(aList [0], getRed(aList [0])*0.8) decreaseRedR(aList [1:])

5 What is the output? def test(num): Call Stack if num > 0:
test(0) return 0 test(1) return test(0) + 1 test(2) return test(1) + 2 test(3) return test(2) + 3 def test(num): if num > 0: return test(num -1) + num else: return 0 test(0) returns 0, test(1) returns 1, test(2) returns 3, test(3) returns 6

6 Stacks Add and remove from the top of the stack
Last in first out type of structure Like a stack of plates in a cafeteria Or a Pez container Or a childs toy

7 Queue Add to the end and remove from the front
First in and first out type of structure Like a line at a store checkout Or a printer queue


Download ppt "Barb Ericson Georgia Tech"

Similar presentations


Ads by Google