Name: ________________________________________________________
Umail Address: __________________________________@ umail.ucsb.edu
Lab Section (circle one): 11am 12:30pm
Please write your name only on this page. That allows me to grade your exams without knowing whose exam I am grading.
This exam is closed book, closed notes, closed mouth, cell phone off,
except for:
There are 100 points worth of questions on the exam, and you have 85 minutes to complete the exam.
A hint for allocating your time—on your first pass through the exam:
If you do that, you'll complete your first pass through the exam in 50 minutes, and still have 35 minutes to
(15 pts) With this exam, you should have received two separate handouts.
One has code from lab03—the function definitions for for ithPointOnUnitCircleX(),
ithPointOnUnitCircleY()
, drawPolygon()
and drawStar()
. This is provided for your reference—as a kind of helpful "cheat sheet".
The other has diagrams to remind you of how ithPointOnUnitCircleX(),
ithPointOnUnitCircleY()
work,
and then some diagrams that show sample output from the function drawCutPolygon(t,r,n,i)
.
This is a new function that draws a polygon using turtle t
, with radius r
, n
sides, and a "cut" starting at point i
.
Note how drawCutPolygon(t,r,n,i)
handles odd and even numbers of sides differently:
i + n//2
and i + n//2 + 1
The code for drawCutPolygon appears on the next page of this exam—but it is incomplete.
Finish the code by filling in the three missing sections—at each of the places with bold comments.
Some hints:
ithOfNPointsOnCircleX()
—in fact, you will need to do this to solve the problem in the space given. (... continued from previous page)
# drawCutPolygon (turtle, float, int, i) => void # consumes: # t: a turtle (that was already constructed) we'll use to draw the polygon # r: the radius of the polygon # n: the number of sides the polygon has # i: which point on the polygon the cut should start at # # produces: nothing # side-effect: # An n-sided polygon is drawn centered at (0,0) with radius r. # In addition a line is drawing from point i to a point directly opposite that # point (i.e. pi radians, or 180 degrees away from that point). # Leave the pen at (0,0), facing east.
def drawCutPolygon(t,r,n,i):
# @@@ (Part 1: 5 pts) Add a function call here to drawPolygon(), passing in the # appropriate parameters. # Pick up the pen, move to point i, put the pen down t.up() t.goto( ithOfNPointsOnCircleX(i,n,r), ithOfNPointsOnCircleY(i,n,r) )
# @@@ (Part 2: 5 pts) Fill in the blank if the if test with a test # that is true when the number of points on the polygon is even
if ( ):
# @@@ (Part 3: 5 pts) If there are an even number of points, # draw a line to a point directly across from point i.
else: # If n is odd, draw a line to a point that is half way in the middle of the opposite # side---that is, halfway between point (i + n//2) and (i + n//2 + 1)
t.down() t.goto( (ithOfNPointsOnCircleX(i+n//2,n,r) + ithOfNPointsOnCircleX(i+n//2+1,n,r)) / 2, (ithOfNPointsOnCircleY(i+n//2,n,r) + ithOfNPointsOnCircleY(i+n//2+1,n,r)) / 2)
# Leave the pen up, at (0,0), facing east.
t.up(); t.goto(0,0); t.setheading(0);
import math
as the first line of your answer—then you can use math.sqrt()
to calculate the square root.
check_expect_within_tolerance("hypotenuse(3,4)",hypotenuse(3,4),5) check_expect_within_tolerance("hypotenuse(1,1)",hypotenuse(1,1),1.414) # sqrt(2) check_expect_within_tolerance("hypotenuse(5,12)",hypotenuse(5,12),13)
fn11()
, fn12()
, fn13()
.If this is what Python would actually print | ... then this is an acceptable exam answer |
---|---|
>>> 2 * fn10(2) 2 2 Traceback (most recent call last): File "", line 1, in |
>>> 2 * fn10 (2) 2 2 ERROR >>> |
>>> fn11(3) |
>>> 4 * fn11(3) |
>>> fn12(3) |
>>> 4 * fn12(3) |
>>> fn13(3) |
>>> 4 * fn13(3) |
For each of the following questions, perform the conversion indicated:
Write a Python function called ouncesToGallons
that returns an number. The function should take an parameter called ounces
, and return the value of that parameter, divided by 128.
>>> town = "Isla Vista"
>>> town[0]
______
>>> town[-1]
______
>>> town[2:]
______
>>> town[3:6]
______
list
data type in Python).End of Exam. Total points: ?
6 points distributed evenly around the unit circle | 8 points distributed evenly around the unit circle |
drawCutPolygon(sheila, 100.0, 5, 0) |
drawCutPolygon(sheila, 100.0, 5, 1) |
drawCutPolygon(sheila, 100.0, 5, 2) |
drawCutPolygon(sheila, 100.0, 6, 0) |
drawCutPolygon(sheila, 100.0, 6, 1) |
drawCutPolygon(sheila, 100.0, 6, 2) |
def fn11(a): for i in range(a,a+3): print(i)
def fn12(a): for i in range(a,a+3): return(i)
def fn13(a): for i in range(a,a+3): return(a) print(a)