lecture19

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

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

View Full Document Right Arrow Icon
Caltech CS 1: Fall 2010 Lecture 19 : November 29, 2010 Advanced topics, part 1
Background image of page 1

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

View Full DocumentRight Arrow Icon
Caltech CS 1: Fall 2010 Regular expressions
Background image of page 2
Caltech CS 1: Fall 2010 Advanced topics, lecture 1 recursion first-class functions lambda expressions higher-order functions map , filter , reduce
Background image of page 3

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

View Full DocumentRight Arrow Icon
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
Background image of page 4
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
Background image of page 5

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

View Full DocumentRight Arrow Icon
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)
Background image of page 6
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!
Background image of page 7

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

View Full DocumentRight Arrow Icon
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)
Background image of page 8
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?
Background image of page 9

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

View Full DocumentRight Arrow Icon
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
Background image of page 10
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
Background image of page 11

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

View Full DocumentRight Arrow Icon
Caltech CS 1: Fall 2010 Question 1 : How is it possible that you can call a function while defining it? Answer
Background image of page 12
Image of page 13
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 Right Arrow Icon
Ask a homework question - tutors are online