Python Programming in Context Chapter 9. Objectives To introduce functional programming style To practice writing recursive functions To introduce grammars.

Slides:



Advertisements
Similar presentations
C++ Programming:. Program Design Including
Advertisements

What gets printed out? def f(n): if (n == 0): return (" ") else: print(“blug ") return f(n-1) f(3)
Lecture 12 Recursion part 1
ECE 103 Engineering Programming Chapter 54 Recursion Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103 material developed.
Grammar vs Recursive Descent Parser
40S Applied Math Mr. Knight – Killarney School Slide 1 Unit: Sequences Lesson: SEQ-L3 Drawing Fractal Patterns Drawing Fractal Patterns Learning Outcome.
New Mexico Computer Science For All
COMPSCI 105 S Principles of Computer Science
Chapter 16 Recursion: Another Control Mechanism. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. a.
Recursion Definition: A method that calls itself. Recursion can be direct or indirect. Indirect recursion involves a method call that eventually recalls.
1 CSE1301 Computer Programming Lecture 28 Recursion (Part 2)
Homework discussion Read pages 388 – 391 Page 400: 49 – 52, 72.
Recursion: Overview What is Recursion? Reasons to think recursively? An Example How does recursion work?
Linear Fractal Mountains 2 step recursive process: –Subdivide chain by creating edge midpoints –Randomly perturb midpoint positions Gen 0: Gen 1: Gen 2:
CHAPTER 10 Recursion. 2 Recursive Thinking Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
Taxicab Geometry. Uses a graph and the movement of a taxi Uses the legs of the resulting right triangle to find slope, distance, and midpoint.
Discrete Maths Objective to show the close connection between recursive definitions and recursive functions , Semester 2, Recursion.
Data Structures Using C++ 2E Chapter 6 Recursion.
1 Excursions in Modern Mathematics Sixth Edition Peter Tannenbaum.
© 2004 Pearson Addison-Wesley. All rights reserved October 27, 2006 Recursion (part 2) ComS 207: Programming I (in Java) Iowa State University, FALL 2006.
Log on and Download from website:
Data Structures Using C++ 2E Chapter 6 Recursion.
Department of Computer Science Data Structures Using C++ 2E Chapter 6: Recursion Learn about recursive Definitions Algorithms Functions Explore the base.
Chapter 15 Recursion.
1 CS 177 Week 16 Recitation Recursion. 2 Objective To understand and be able to program recursively by breaking down a problem into sub problems and joining.
Ch 9 Infinity page 1CSC 367 Fractals (9.2) Self similar curves appear identical at every level of detail often created by recursively drawing lines.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Chapter 14 Recursion. Chapter Objectives Learn about recursive definitions Explore the base case and the general case of a recursive definition Learn.
Reading – Chapter 10. Recursion The process of solving a problem by reducing it to smaller versions of itself Example: Sierpinski’s TriangleSierpinski’s.
Main Points: - Python Turtle - Fractals
Fractals Douglas reeves.
Fractals. Most people don’t think of mathematics as beautiful but when you show them pictures of fractals…
Python Programming in Context Chapter 4. Objectives To understand Python lists To use lists as a means of storing data To use dictionaries to store associative.
Fractals What do we mean by dimension? Consider what happens when you divide a line segment in two on a figure. How many smaller versions do you get? Consider.
EECS 110 Recitation #3 Ning Xia Northwestern University.
Data Structures Using Java1 Chapter 5 Recursion. Data Structures Using Java2 Chapter Objectives Learn about recursive definitions Explore the base case.
CS324e - Elements of Graphics and Visualization Fractals and 3D Landscapes.
Chapter 15: Recursion. Objectives In this chapter, you will: – Learn about recursive definitions – Explore the base case and the general case of a recursive.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 15 Recursion.
Ch 6.1 One Step Inequalities (Addition) Objective: To solve and graph simple inequalities involving addition/subtraction.
Programming Training kiddo Main Points: - Python Statements - Problems with selections.
MID-SEGMENT & TRIANGLE PROPORTIONALITY Day 8.  A midsegment of a triangle is a segment that connects the midpoints of two sides of a triangle. In the.
Review of Recursion  a recursive method calls itself  to prevent infinite recursion you need to ensure that: 1. the method reaches a base case 2. each.
Chapter 15: Recursion. Recursive Definitions Recursion: solving a problem by reducing it to smaller versions of itself – Provides a powerful way to solve.
Chapter 15: Recursion. Objectives In this chapter, you will: – Learn about recursive definitions – Explore the base case and the general case of a recursive.
Fractals Lesson 6-6.
12-CRS-0106 REVISED 8 FEB 2013 KUG1C3 Dasar Algoritma dan Pemrograman.
Chapter 9: Geometry. 9.1: Points, Lines, Planes and Angles 9.2: Curves, Polygons and Circles 9.3: Triangles (Pythagoras’ Theorem) 9.4: Perimeter, Area.
Data Structures.
CprE 185: Intro to Problem Solving (using C)
Main Points: - Python Turtle - Fractals
CS 1321.
We are removing the 4th part of hw2pr2 for now.
5-1 Midsegments of Triangles
Introduction to Computer Science - Alice
Data Structures Using Java
Recursion (part 2) October 26, 2007 ComS 207: Programming I (in Java)
Recursion Chapter 11.
Discrete Maths 11. Recursion Objective
Exercise (1) What does function chai draw? def chai(size):
Main Points: - Python Turtle - Fractals
Chapter 11 Recursion.
Chapter 12 Review Domain: Range: x y
Recursion Taken from notes by Dr. Neil Moore
Fractals What do we mean by dimension? Consider what happens when you divide a line segment in two on a figure. How many smaller versions do you get?
Recursion (part 2) March 22, 2006 ComS 207: Programming I (in Java)
Unit 9. Day 17..
CMPT 120 Lecture 16 – Unit 3 – Graphics and Animation
Chapter 5 Integration.
Presentation transcript:

Python Programming in Context Chapter 9

Objectives To introduce functional programming style To practice writing recursive functions To introduce grammars and production rules

Recursion A self referencing solution Base case is easily solved Recursive step uses solution to try to solve smaller version of the problem Sometimes easier to see with pictures

Figure 9.1

Figure 9.2

Infinite Recursion No base case In theory this will continue to call itself forever In practice, memory will eventually be completely consumed and process will stop

Listing 9.1 def hello(): print("Hello World") hello()

Nested Squares Base Case – Draw a simple square Recursive Step – Draw a picture of recursive squares starting with a smaller side length

Figure 9.3

Listing 9.2 def drawSquare(aTurtle,side): for i in range(4): aTurtle.forward(side) aTurtle.right(90) def nestedBox(aTurtle,side): if side >= 1: drawSquare(aTurtle,side) nestedBox(aTurtle,side-5)

Recursive Tree Draw trunk Recursively draw a tree on the right Recursively draw a tree on the left Stop when the tree is sufficiently small

Listing 9.3 def tree(t,trunkLength): if trunkLength < 5: return else: t.forward(trunkLength) t.right(30) tree(t, trunkLength-15) t.left(60) tree(t, trunkLength-15) t.right(30) t.backward(trunkLength)

Sierpinski Triangle Draw triangles from triangles by connecting the midpoints of the sides Continue to do this for each newly created triangle Stop at a sufficient number of levels

Figure 9.4

Figure 9.5

Figure 9.6

Listing 9.4 def drawTriangle(t,p1,p2,p3): t.up() t.goto(p1) t.down() t.goto(p2) t.goto(p3) t.goto(p1) def midPoint(p1,p2): return ((p1[0]+p2[0])/2.0,(p1[1]+p2[1])/2.0) def sierpinski(myTurtle,p1,p2,p3,depth): if depth > 0: sierpinski(myTurtle,p1,midPoint(p1,p2),midPoint(p1,p3),depth-1,) sierpinski(myTurtle,p2,midPoint(p2,p3),midPoint(p2,p1),depth-1,) sierpinski(myTurtle,p3,midPoint(p3,p1),midPoint(p3,p2),depth-1,) else: drawTriangle(myTurtle,p1,p2,p3)

Figure 9.7

Recursive Snowflakes Koch Curve L Systems Recursive Grammers Following a sequence of instructions that are recursively generated

Figure 9.8

Figure 9.9

Figure 9.10

L System Axiom (starting symbol) Rules (substitution) Axiom A Rules A → B B → AB

Example A Axiom B (apply rule 1 to A) AB (apply rule 2 to B) BAB (apply rule 1 to A, then apply rule 2 to B) ABBAB (applyrule 2 to B, then rule 1 to A, then rule 2 to B) BABABBAB (rules applied: 1, 2, 2, 1, 2)

Listing 9.5 def drawLS(aTurtle,instructions,angle,distance): for cmd in instructions: if cmd == 'F': aTurtle.forward(distance) elif cmd == 'B': aTurtle.backward(distance) elif cmd == '+': aTurtle.right(angle) elif cmd == '-': aTurtle.left(angle) else: print('Error: %s is an unknown command'%cmd)

Listing 9.6 def applyProduction(axiom,rules,n): for i in range(n): newString = "" for ch in axiom: newString = newString + rules.get(ch,ch) axiom = newString return axiom

Listing 9.7 def drawLS(aTurtle,instructions,angle,distance): stateSaver = [] for cmd in instructions: if cmd == 'F': aTurtle.forward(distance) elif cmd == 'B': aTurtle.backward(distance) elif cmd == '+': aTurtle.right(angle) elif cmd == '-': aTurtle.left(angle) elif cmd == '[': pos = aTurtle.position() head = aTurtle.heading() stateSaver.append((pos,head)) elif cmd == ']': pos,head = stateSaver.pop() aTurtle.up() aTurtle.setposition(pos) aTurtle.setheading(head) aTurtle.down()

Listing 9.8 def lsystem(axiom,rules,depth,initialPosition,heading,angle,length): aTurtle = turtle.Turtle() win = turtle.Screen() aTurtle.up() aTurtle.setposition(initialPosition) aTurtle.down() aTurtle.setheading(heading) newRules = applyProduction(axiom,rules,depth) drawLS(aTurtle,newRules,angle,length) win.exitonclick()

Figure 9.11