10/20/19 1 Computational Structures in Data Science Lecture 6: Recursion (cont) UC Berkeley EECS Lecturer Michael Ball http:cs88.org October 14, 2019 Computational Concepts Toolbox • Data type: values, literals, operations, – e.g., int, float, string • Expressions, Call expression • Variables • Assignment Statement • Sequences: tuple, list – indexing • Data structures • Tuple assignment • Call Expressions • Function Definition Statement • Conditional Statement • Iteration: – data-driven (list comprehension) – control-driven (for statement) – while statement • Higher Order Functions – Functions as Values – Functions with functions as argument – Assignment of function values • Recursion • Lambda - function valued expressions 10/14/19 UCB CS88 Fa19 L4 2 Today’s Lecture • Recursion • More practice • Some tips & tricks • Abstract Data Types – More use of functions! – Value in documentation and clarity – Not on next Monday’s midterm 10/14/19 UCB CS88 Fa19 L4 3 Announcements • MIDTERM MOVED! • Monday 10/21 7-9pm • No Assignments Due this week • Saturday October 19th 5 - 8:30 pm 4 Recall: Iteration def sum_of_squares(n): accum = 0 for i in range(1,n+1): accum = accum + i*i return accum 1. Initialize the “base” case of no iterations 2. Starting value 3. Ending value 4. New loop variable value Recursion Key concepts – by example 6 def sum_of_squares(n): if n < 1: return 0 else: return sum_of_squares(n-1) + n**2 1. Test for simple “base” case 2. Solution in simple “base” case 3. Assume recusive solution to simpler problem 4. Transform soln of simpler problem into full soln UCB CS88 Fall 2019 L5 Oct 7, 2019
6
Embed
Lecture 6: Recursion (cont) - GitHub Pages · Recursion (cont) UC Berkeley EECS Lecturer Michael Ball October 14, 2019 http:cs88.org Computational Concepts Toolbox ... E.g. Calling
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
10/20/19
1
Computational Structures in Data Science
Lecture 6:Recursion (cont)
UC Berkeley EECSLecturer Michael Ball
http:cs88.orgOctober 14, 2019
Computational Concepts Toolbox• Data type: values, literals,
operations, – e.g., int, float, string
• Expressions, Call expression
• Variables• Assignment Statement• Sequences: tuple, list
– indexing• Data structures• Tuple assignment• Call Expressions• Function Definition
Statement• Conditional Statement
• Iteration: – data-driven (list
comprehension)– control-driven (for
statement)– while statement
• Higher Order Functions– Functions as Values– Functions with functions as
argument– Assignment of function
values
• Recursion• Lambda - function valued
expressions
10/14/19 UCB CS88 Fa19 L4 2
Today’s Lecture
• Recursion• More practice• Some tips & tricks
• Abstract Data Types– More use of functions!– Value in documentation and clarity– Not on next Monday’s midterm
10/14/19 UCB CS88 Fa19 L4 3
Announcements• MIDTERM MOVED!• Monday 10/21 7-9pm• No Assignments Due this week• Saturday October 19th 5 - 8:30 pm
4
Recall: Iteration
def sum_of_squares(n):accum = 0for i in range(1,n+1):
accum = accum + i*ireturn accum
1. Initialize the “base” case of no iterations
2. Starting value
3. Ending value
4. New loop variable value
Recursion Key concepts – by example
6
def sum_of_squares(n):if n < 1:
return 0else:
return sum_of_squares(n-1) + n**2
1. Test for simple “base” case 2. Solution in simple “base” case
3. Assume recusive solution to simpler problem 4. Transform soln of simpler