CS8, 09M, UCSB
H10: (Based on online reading assignment---see below) Total points: ? (printable PDF)
Accepted: on paper, in lecture (11am) on Wednesday, September 2nd, in Phelps 1401
No email submission allowed.
Name: (4 pts)______________________________ UCSBNetID (3 pts) _____________________
Lab Section (3 pts) Circle one: Thu 11am Thu 12:30pm Unknown
Circle the one you plan to attend, which may or may not be the one you are registered for.
There is an online reading assignment at the link below. This online reading assignment includes two chapters from an online Python textbook by Allen B. Downey, and then some material written by me—but don't worry:
- the chapters, and the article are relatively short—much shorter than the ones in our printed textbook, and
- they are mostly review of material we've already covered in lecture.
Here are the links to the chapters:
There is also an article by me about fruitful vs. void functions.
I say that this is "mostly review"—there is a little bit of new material that I want you to see, plus some material that although it might not be entirely new, is explained in a particular way that I think will help you understand it better. In particular, I'd like you to focus on a few key ideas.
- The explanation of stack diagrams for functions
- What happens to the stack when a function is called
- What happens to the stack when you return from a function
- The ideas of
- logical operators (and, or, not)
- chained conditionals (if/elif/.../elif/else)
- nested conditions (if/elif/else statements nested inside other if/elif/else statements)
- The difference between
- functions that don't return a value (void functions) and
- functions that do return a value (what this author calls "fruitful" functions—because they "bear fruit", i.e. they "produce" something.
- The idea of a guardian statement
- How to check for type (also covered in lab05)
- What it means when a function calls itself—the idea of recursion
So, read all of that, then answer these questions:
- (10 pts) Downey Chapter 3 refers to "composition" of functions. Write an example of a Python assignment statements where there is a variable on the left hand side, and on the right hand side, you illustrate composition of the functions math.sqrt and math.sin
- (10 pts) Section 5.9 and 5.10 talk about stack diagrams for recursive functions, and infinite recursion. Section 5.10 indicates that Python reports an error message when the "maximum recursion depth is reached", for example after 1000 function calls.
What would the run-time stack look like at this point? Describe what would be on the bottom and the top, and based on that, suggest why you think there is a limit on recursion--i.e. what would happen if there were not a maximum recursion depth?
Remember that a stack frame is an area in memory, and takes up memory space.
Please turn over for more problems
Continued from other side
- (20 pts) Section 6.8 discusses the idea of a guardian statement—one that protects code from errors.
In section 6.8, the guardian checks for type. Another kind of error that can occur is division by zero. Python will produce a traceback and halt your program if it encounters division by zero.
Write a funciton that takes four parameters: x1,y1,x2,y2, where x1,y1 is one point on a line, and x2,y2 is another point, and returns the slope of the line connecting those points.
To compute the slope of a line, we divide rise/run, i.e the change in y over the change in x.
Your function should have a guardian statement that prints an error message and returns None if the run is zero---that is, if your function would end up dividing by zero.
End of H10