CAS CS 111 Lecture 9: CAS CS 111 B1 September 25 Lecture 9 Notes

79 views12 pages
Verified Note
Recall: Designing a Recursive Function
Start by programming the base case(s)/
What instance(s) of this problem can I solve directly
(without looking at anything smaller)?
i.
1.
Find the recursive substructure.
How could I use the solution to any smaller version of the
problem to solve the overall problem?
i.
2.
Solve the smaller problem using a recursive call!
Store its result in a variable
i.
3.
Do your one step.
i.
Use concrete cases to figure out what you need to do
ii.
4.
Recursively Finding the Largest Element in a List
Design Questions for mymax()
(base case) - When can I determine the largest element in a list
without needing to look at a smaller list?
(recursive case) - How could I use the largest element in a smaller
list to determine the largest element in the entire list?
Compare the first element to largest element in the rest of the list1.
Return the larger of the two2.
LET THE RECURSIVE CALL HANDLE THE REST OF
THE LIST!
Recursively Finding the Largest Element in a List
How many times will max_rest be returned?
Question:
§
Answer:
§
§
§
§
§
§
§
§
§
§
§
§
§
§
§
Final Answer:
§
Recursively Replacing Characters in a String
Design Questions for replace()
(base case) - When can I determine the "replaced" version of s
without looking at a smaller string?
(recursive case) - How could I use the "replaced" version of a
smaller string to get the "replaced" version of s?
Complete This Function Together!
FINAL'VERSION!!!!!
§
Add'Temporary'prints'for'Debugging/Tracing
Monday's)Lecture
Tuesday,' 25'September' 2018
6:10'PM
Unlock document

This preview shows pages 1-3 of the document.
Unlock all 12 pages and 3 million more documents.

Already have an account? Log in
Recall: Designing a Recursive Function
Start by programming the base case(s)/
What instance(s) of this problem can I solve directly
(without looking at anything smaller)?
i.
1.
Find the recursive substructure.
How could I use the solution to any smaller version of the
problem to solve the overall problem?
i.
2.
Solve the smaller problem using a recursive call!
Store its result in a variablei.
3.
Do your one step.
Build your solution on the result of the recursive calli.
Use concrete cases to figure out what you need to doii.
4.
Recursively Finding the Largest Element in a List
Design Questions for mymax()
(base case) - When can I determine the largest element in a list
without needing to look at a smaller list?
(recursive case) - How could I use the largest element in a smaller
list to determine the largest element in the entire list?
Compare the first element to largest element in the rest of the list
1.
Return the larger of the two
2.
LET THE RECURSIVE CALL HANDLE THE REST OF
THE LIST!
Recursively Finding the Largest Element in a List
How many times will max_rest be returned?
Question:
§
Answer:
§
§
§
§
§
§
§
§
§
§
§
§
§
§
§
Final Answer:
§
Recursively Replacing Characters in a String
Design Questions for replace()
(base case) - When can I determine the "replaced" version of s
without looking at a smaller string?
(recursive case) - How could I use the "replaced" version of a
smaller string to get the "replaced" version of s?
Complete This Function Together!
FINAL'VERSION!!!!!
§
Add'Temporary'prints'for'Debugging/Tracing
Monday's)Lecture
Tuesday,' 25'September' 2018 6:10'PM
Unlock document

This preview shows pages 1-3 of the document.
Unlock all 12 pages and 3 million more documents.

Already have an account? Log in
Recall: Designing a Recursive Function
Start by programming the base case(s)/
What instance(s) of this problem can I solve directly
(without looking at anything smaller)?
i.
1.
Find the recursive substructure.
How could I use the solution to any smaller version of the
problem to solve the overall problem?
i.
2.
Solve the smaller problem using a recursive call!
Store its result in a variablei.
3.
Do your one step.
Build your solution on the result of the recursive calli.
Use concrete cases to figure out what you need to doii.
4.
Recursively Finding the Largest Element in a List
Design Questions for mymax()
(base case) - When can I determine the largest element in a list
without needing to look at a smaller list?
(recursive case) - How could I use the largest element in a smaller
list to determine the largest element in the entire list?
Compare the first element to largest element in the rest of the list1.
Return the larger of the two2.
LET THE RECURSIVE CALL HANDLE THE REST OF
THE LIST!
Recursively Finding the Largest Element in a List
How many times will max_rest be returned?
Question:
§
Answer:
§
§
§
§
§
§
§
§
§
§
§
§
§
§
§
Final Answer:
§
Recursively Replacing Characters in a String
Design Questions for replace()
(base case) - When can I determine the "replaced" version of s
without looking at a smaller string?
(recursive case) - How could I use the "replaced" version of a
smaller string to get the "replaced" version of s?
Complete This Function Together!
FINAL'VERSION!!!!!
§
Add'Temporary'prints'for'Debugging/Tracing
Monday's)Lecture
Tuesday,' 25'September' 2018 6:10'PM
Unlock document

This preview shows pages 1-3 of the document.
Unlock all 12 pages and 3 million more documents.

Already have an account? Log in

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers

Related Documents