CS5nm Midterm Exam 2 Study Guide

Textbook Coverage

Here is an updated version of the textbook chapters, and which exams cover them.
The entries that are in light grey italics indicate that the material is covered, but not emphasized

Chapter
Midterm 1 Midterm 2 Final Exam
1: The way of the program
Entire chapter Entire chapter Entire Chapter
2: Variables, Expressions,
         Statements
Entire chapter Entire chapter Entire Chapter
3: Functions
3.1–3.7 3.1–3.7
3.8–end
Entire Chapter
4: TurtleWorld
  Entire Chapter Entire Chapter
5: Conditions and Recursion
  5.1–5.7 5.1–5.7
5.8–end
6: Fruitful Functions
  6.1–6.4 6.1–6.4
6.5–end
7: Iteration
    Entire Chapter
8: Strings
  8.1, 8.2, 8.4 Entire Chapter
9: Case Study, Word Play
    Entire Chapter
10: Lists
  10.1, 10.2, 10.5,
10.6 (append and sort only)
Entire Chapter
11: Dictionaries
    Entire Chapter
12: Tuples
  12.1, 12.2, 12.3 Entire Chapter

Other topics for Midterm 2

Study Guide by Chapter

Chapter 3: Functions

Chapter 4: TurtleWorld

If I ask you to write function definitions using TurtleWorld, I'll provide you with this brief guide on the exam itself:

Note:

Practice Problems

These problems are based on the figures shown. Be sure to include comments in these function definitions where appropriate.

Figure 1 Figure 2 Figure 3
Turtle Graphics Triangle with turtle at bottom left of equallateral triangle pointing up Turtle Graphics triangle, with turtle in center of top of equillateral triangle facing down snowman general points for turtle graphics

 

  1. Write a function equalTri(t,length) that takes a turtle as a parameter, and the length of one side of an equilateral triangle. Assuming that the turtle is facing "east" (towards the right hand side of the monitor when you start), draw an equilateral triangle that points up (the bottom is flat and the top points towards the top of the monitor). The triangle's bottom left point should be the place where the turtle starts and ends. After the function call the turtle should be facing the same direction that it started from, as shown in Figure 1 above:
  2. Try the same problem, but this time call your function deltaTri(t,length). The triangle should still be an equilateral triangle, but you should assume that the turtle starts in the center of a line that is assume that the triangle must face down, and that the turtle starts and ends facing east, in the center of the top line, like this:
  3. Assume that you have a turtle function called drawCircle(t,r) that takes a turtle and the radius of a circle, and draws a circle. Assume that if the turtle starts out facing east, he will finish facing east, seated at the very bottom of the circle. Using that function, create a function drawSnowman(t,h), where h is the height of the snowman. The snowman should be structured exactly at in ex09, according to the following diagram. Be sure the turtle starts and finishes, facing east, at the bottom of the snowman (at the point indicated in the diagram below.)

 

Chapter 5: Conditions and Recursion (5.1–5.7 on exam)

Fill in the blanks with what IDLE would print in the cases below.

>>> windy = True
>>> raining = False
>>> windy and raining
_____
>>> windy or raining
_____
>>> not windy and raining
_____
>>> windy and not raining
_____
>>> not (windy or raining)
_____
>>> not windy and not raining
_____
>>> not (windy or not raining)
_____
>>> windy and (not raining)
_____
>>> 

Rewrite each of these expressions to remove the word not, without changing the meaning of the expression. The first two are done for you as an example:

before after
not (a < b) a >= b
a > 5 and not b == 10 a > 5 and b != 10
not( (a < 5) and (b == 10) )  
(not (a < 5))and (b ==10)  
a <=5 or not b != 10  
not (a==5 or b==10)  

 

Chapter 6: Fruitful Functions (6.1–6.4 on exam)

For these problems, you also need material from section 8.4 on string slices.

(Note: Chapter 7 is not covered on Midterm 2)

Chapter 8: Strings (8.1, 8.2, 8.4)

Fill in the blanks from this IDLE session

>>> beach = "Rincon"
>>> beach[0]
________
>>> beach[-1]
________
>>> beach[2:4]
      
________
>>> beach[3:5]
       
________
>>> beach[3:3]
        
________
>>>

(Note: Chapter 9 is not covered on Midterm 2)

Chapter 10: Lists

Fill in the blanks from this IDLE session

>>> primes = [2,3,5,7,11,13]
>>> primes[0]
______
>>> primes[3]
______
>>> primes[5:]
______
>>> primes[11:]
______
>>> primes[1:4]
______
>>> primes.append(13)
>>> primes[-1]
______
>>> 

(Note: Chapter 11 is not covered on Midterm 2)

Chapter 12: Tuples

Fill in the blanks in the following IDLE session

>>> (x,y,z) = (1,2,3)
>>> x
_______
>>> z
______
>>> (y,z)
______
>>> coins = (1,2,5,10,25)
>>> coins[3:4]
______
>>> coins[4:6]
______
>>> coins[2:4]
______
>>> coins[:1]
______
>>> coins[9:]
______
>>> coins[1:]
______
>>> coins[4]
______
>>> coins[-1]
______
>>>

A combined session on strings, lists and tuples:

>>> x = (1,2,3)
>>> y = [1,2,3]
>>> z = '123'
>>> x[1]
______
>>> y[2]
______
>>> z[0]
______
>>> type(x)
______
>>> type(x[0])
______
>>> type(x[0:1])
______
>>> x[0:1]
______
>>> type(y)
______
>>> type(y[0])
______
>>> type(y[2:3])
______
>>> type(z[0])
______
>>> type(z[0:1])
______
>>> len(z[0:1])
______
>>> len(z[0:0])
______
>>>

Drawing in TurtleWorld and PyGame

As a reminder:

Some questions:

A PyGame programming problem.

Suppose we want to make a function drawBoxes() to draw the following figure in PyGame.

Figure 1 Figure 2
drawBoxes with specific points PyGame drawBoxes, fill in the points

Below, Figure 3 shows a function definition for drawBoxes(), and a Figure 4 shows the strategy for drawing the box (i.e. the order in which the points will be drawn).

Figure 3 Figure 4
def drawBoxes(screen, color, x, y, width, height):
	pygame.draw.lines(screen, color, False,
	makeBoxesPoints(x,y,width,height), 1)
Pygame drawBoxes, points in order

Given that, finish the incomplete function definitions shown below. Your answer should pass the test cases given.

You only need to finish the function definition for makeBoxesPoints()—don't worry about adding any more test cases, or any of the import pygame or pygame.init() type stuff that might be needed. Don't worry about the while(True): loop or the pygame.display.update(), or any of that stuff—assume that has already been done correctly. Just focus on this one function definition.

def makeBoxesPoints(x,y,width,height):
   points = []
   points.append( (x+width/2.0, y - height) ) # point 1 from diagram
   # Fill in the rest of this function...


check_expect("makeBoxesPoints 1",
   		makeBoxesPoints(50,100,100,50),
   		[(100,50),(100,100),(150,100),(150,50),(50,50),
   			(50,100),(100,100)])
check_expect("makeBoxesPoints 2",
  			 makeBoxesPoints(50,250,200,100),
   		[(150,150),(150,250),(250,250),(250,150),(50,150),
   			(50,250),(150,250)])

Answers to PyGame programming problem

(end of study guide for Midterm 2)