# CS8—Midterm 2 E02, 10M, Phill Conrad, UC Santa Barbara 08/24/2010

Name: ________________________________________________________

Lab Section (circle one):      11am         12:30pm

Extra code handout (PDF)

This exam is closed book, closed notes, closed mouth, cell phone off,
except for:

• You are permitted one sheet of paper (max size 8.5x11") on which to write notes
• These sheets will be collected with the exam, and might not be returned

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 a question is worth 10 points, spend no more than 5 minutes on it
• if a question is worth 20 points, spend no more than 10 minutes on it
• if a question is worth 40 points, spend no more than 20 minutes on it
• etc.

If you do that, you'll complete your first pass through the exam in 50 minutes, and still have 35 minutes to

• revisit any questions where you need more time

1. (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:

• For even numbers, the line goes from point i to a point that is directly across the circle
(What point is that? You need to figure that out as part of solving the problem.)
• For odd numbers, the line goes from point i to a point in the middle of the opposite side,
between the points `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:

• You may assume that this code gets added at the bottom of the code you were given on the handout, so you do NOT have to write import math, or import cTurtle, or anything like that, and you may call the functions already defined in the code on the handout—functions such as `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.

```
2. (20 pts) Write a Python function definition for a function called hypotenuse. This function should solve the Pythagorean theorem for c—that is, it should take two parameters a, and b, it should calculate and return the value of c, given the formula:

Remember to include `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)```

3. (12 pts) Look on the back of the handout with the diagrams, find the function definitions for `fn11()`, `fn12()`, `fn13()`.

Using those function definitions, for each of the function calls below, indicate exactly what would be printed, including the Python prompt >>> that would follow the output, just as we practiced in lecture on Monday 08/23 (the class before this exam).

For error messages—you do not have to write the full text of the error message (I don't expect you to memorize that level of detail). Instead, use the single word ERROR in all caps to indicate where an error message would be printed. For example:

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)
TypeError: unsupported operand type(s) for *: 'int' and 'NoneType'
>>>

```
```>>> 2 * fn10 (2)
2
2
ERROR
>>>
```

For full credit: pay attention to formatting—i.e. if your output would be one number per line, write it that way, and make sure that if your numbers would be lined up under the left most > in the >>> prompt, that you write them there.

 ```>>> fn11(3) ``` ```>>> 4 * fn11(3) ``` ```>>> fn12(3) ``` ```>>> 4 * fn12(3) ``` ```>>> fn13(3) ``` ```>>> 4 * fn13(3) ```
4. For each of the following questions, perform the conversion indicated:

1. (3 pts) From binary to decimal: 0011 0010

2. (3 pts) From decimal to binary: 65

3. (3 pts) From binary to octal: 011 000 100

4. (3 pts) From octal to binary: 256

5. (3 pts) From hex to binary: C5

6. (3 pts) From binary to hex: 1111 0100

5. 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.

1. (4 pts) Stub

2. (6 pts) Three test cases

3. (10 pts) Correct Function Definition

6. (8 pts) Listed below is an session from IDLE, showing interaction between a user and the Python shell. The response from the shell at various points has been replaced with blanks. Fill in the blanks with what the Python shell would print.

If the blanks are too small, you can write your answers beside them.
```>>> town = "Isla Vista">>> town[0]
______>>> town[-1]
______>>> town[2:]
______>>> town[3:6]
______```
7. (3 pts) Write an example of a Python assignment statement that illustrates "string concatenation" somewhere on the right hand side of the assignment statement.

(The statement can be anything as long as it is an assignment statement that illustrates string concatenation)

8. (4 pts) Write an example of a Python assignment statement that creates a list.
(It can be any list that you like, as long as it is actually an example of the `list` data type in Python).

End of Exam. Total points: ?

# Handout to go with CS8—Midterm 2 E02, 10M, Phill Conrad, UC Santa Barbara 08/24/2010

## Diagrams For Question 1

### ithPointOnUnitCircle:

 6 points distributed evenly around the unit circle 8 points distributed evenly around the unit circle

### Output from drawCutPolygon(t,r,n,i)

 `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)`

# Handout to go with CS8—Midterm 2 E02, 10M, Phill Conrad, UC Santa Barbara 08/24/2010

## Function definitions of fn11(), fn12(), and fn13()

```
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)

```