lecture19

# lecture19 - Lecture 19 Advanced topics part 1 Caltech CS 1...

This preview shows pages 1–13. Sign up to view the full content.

Caltech CS 1: Fall 2010 Lecture 19 : November 29, 2010 Advanced topics, part 1

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Caltech CS 1: Fall 2010 Regular expressions
Caltech CS 1: Fall 2010 Advanced topics, lecture 1 recursion first-class functions lambda expressions higher-order functions map , filter , reduce

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Caltech CS 1: Fall 2010 This is the last week of classes! Assignment 8 is due Saturday at 6 PM last assignment! Make sure you get caught up on all old assignments/redos Change of pass grade: was 40, is now 37 due to one less assignment than planned Will have one final ombuds section on Thursday, recitations on Friday Friday recitations: final review
Caltech CS 1: Fall 2010 A function is known as recursive when it calls itself Python allows you to define recursive functions Recursion has a reputation for difficulty, but it's really no big deal Let's look at a simple example

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Caltech CS 1: Fall 2010 How would you compute the sum of all the numbers from 0 to N (where N >= 0)? In Python, could just use the built-in sum function or a for or while loop Mathematically, we can define a solution like this: sum_to_N(0) = 0 sum_to_N(n) = n + sum_to_N(n-1)
Caltech CS 1: Fall 2010 sum_to_N definition: sum_to_N(0) = 0 sum_to_N(n) = n + sum_to_N(n-1) We state that sum_to_N(0) is 0 by definition This is called a base case it can be solved immediately (no recursion needed) To compute sum_to_N(n) for n > 0 : first compute sum_to_N(n-1) then add n to it This must be the correct answer!

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Caltech CS 1: Fall 2010 sum_to_N definition: sum_to_N(0) = 0 sum_to_N(n) = n + sum_to_N(n-1) Another way to look at this: If sum_to_N(n-1) gives the correct sum from 0 to n-1 then sum_to_N(n) will be the correct sum from 0 to n , because we've just added n to sum_to_N(n-1)
Caltech CS 1: Fall 2010 sum_to_N definition: sum_to_N(0) = 0 sum_to_N(n) = n + sum_to_N(n-1) The hard part: We assume that sum_to_N works correctly for all numbers < n when we define sum_to_N for the argument n How can we do this?

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Caltech CS 1: Fall 2010 sum_to_N definition: sum_to_N(0) = 0 sum_to_N(n) = n + sum_to_N(n-1) This is an example of mathematical induction : if sum_to_N works for some base case ( 0 ) and we know that if sum_to_N works for some value n-1 , then it will also work for the value n then we conclude that sum_to_N works for all n from 0 to infinity! works for 0, 1, 2, 3, . .. as far up as we want to go
Caltech CS 1: Fall 2010 def sum_to_N (n): if n == 0: return 0 else: return (n + sum_to_N(n – 1) ) recursive call to sum_to_N

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Caltech CS 1: Fall 2010 Question 1 : How is it possible that you can call a function while defining it? Answer
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 02/22/2011 for the course CS 1 taught by Professor Pinkston,d during the Fall '08 term at Caltech.

### Page1 / 46

lecture19 - Lecture 19 Advanced topics part 1 Caltech CS 1...

This preview shows document pages 1 - 13. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online