agenda
play

Agenda Announcements Snarf code CompSci101Recursion 1/14/2013 - PowerPoint PPT Presentation

Agenda Announcements Snarf code CompSci101Recursion 1/14/2013 CompSci101 Peter Lorensen 1 Recusion Recusion: Se recusion. 1/14/2013 CompSci101 Peter Lorensen 2 Recursion Functions can call other functions, but they


  1. Agenda • Announcements • Snarf code “CompSci101Recursion” 1/14/2013 CompSci101 Peter Lorensen 1

  2. Recusion • Recusion: Se recusion. 1/14/2013 CompSci101 Peter Lorensen 2

  3. Recursion • Functions can call other functions, but they can also call themselves – that is reursion! 1/14/2013 CompSci101 Peter Lorensen 3

  4. Recursion • It is not a function or library • It is a new way of structuring your code to solve problems. • Recursion is based on the divide an conquer way of thinking: -Solve 1 small piece of the problem and call your self with the rest of it. 1/14/2013 CompSci101 Peter Lorensen 4

  5. Recursion example # 1 • A simple recursive function that counts down to “Blastoff” def countdown(n): if n == 0: print 'Blastoff!' else: print n countdown(n - 1) countdown(5) 1/14/2013 CompSci101 Peter Lorensen 5

  6. Try example 1/14/2013 CompSci101 Peter Lorensen 6

  7. Recursion example # 2 • How to count files and folders? 1/14/2013 CompSci101 Peter Lorensen 7

  8. Recursion example # 2 • Solve a big problem by – Dividing it into a smaller problem that can be solved. Identical in structure. 1/14/2013 CompSci101 Peter Lorensen 8

  9. Recursion on a structure • Do something (like counting) on the place were you are… • …then call your self on a new element. 1/14/2013 CompSci101 Peter Lorensen 9

  10. Recursion on a collection • If else structure: – If the problem is small enough to be solved, then do it, otherwise divide it up and call yourself. • Do something to a small part of the problem, then divide and call your self on the rest def countdown(n): if n == 0: print 'Blastoff!' else: print n countdown(n - 1) countdown(5) 1/14/2013 CompSci101 Peter Lorensen 10

  11. Question • What is printed? def vatCalculator( meatLst ): vat = 7.25 A) 100.0 7.25 , 1000.0 72.5 , if len( meatLst ) == 0: print "Done" 10.0 0.725 , else: Done pay = meatLst[0] tax = (pay * vat) / 100.0 B) 100.0 72.5 , print pay, tax, “," 1000.0 725.0 , vatCalculator(meatLst) 10.0 7.25 , Done lionFood = [10.0, 1000.0, 100.0 ] C) Error vatCalculator( lionFood ) 1/14/2013 CompSci101 Peter Lorensen 11

  12. Recursion in sorting • Quicksort can be solved via recursion 1/14/2013 CompSci101 Peter Lorensen 12

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend