CME193: IntroductiontoScientificPython Lecture7: Recursion,Exceptionhandling
CME 193: Introduction to Scientific Python Lecture 7: Recursion, Exception handling
Sven Schmit
stanford.edu/~schmit/cme193
7: Recursion, Exception handling
7-1
Contents
Recursion Exception handling Exercises
7: Recursion, Exception handling
7-2
Back to control flow
To execute repetitive code, we have relied on for and while loops. Furthermore, we used if statements to handle conditional statements. These statements are rather straightforward and easy to understand.
7: Recursion, Exception handling
7-3
Recursion
Recursive function solve problems by reducing them to smaller problems of the same form.
This allows recursive functions to call themselves...
New paradigm Powerful tool Divide-and-conquer Beautiful solutions
7: Recursion, Exception handling
7-4
Recursion
Recursive function solve problems by reducing them to smaller problems of the same form.
This allows recursive functions to call themselves...
New paradigm Powerful tool Divide-and-conquer Beautiful solutions
7: Recursion, Exception handling
7-5
First example
Let's consider a trivial problem: Suppose we want to add two positive numbers a and b, but we can only add/subtract one.
7: Recursion, Exception handling
7-6
First example
Non-recursive solution:
def add(a, b): while b > 0: a += 1 b -= 1 return a
7: Recursion, Exception handling
7-7
First example
Recursive solution: Simple case: b = 0, return a Else, we can return 1 + add(a, b-1)
def add(a, b): if b == 0: # base case return a # recursive step return add(a, b-1) + 1
7: Recursion, Exception handling
7-8
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related download
- release 0 9 0 yesudeep mangalapilly
- lecture 16 recursively defined sets structural induction
- python list all files in directory and subdirectories recursively
- recursive algorithms implemented in python
- generating permutations with recursion maths anew
- cs5001 cs5003 intensive foundations of computer science lecture 5
- data structure and algorithm in python tree
- how to iterate through all files in a directory using kotlin kotlin
- username book chapter 10 uc santa barbara
- python iterate dictionary recursively