CS 100 Introduction to Computer Science Midterm Sample Solutions The answers below are shown in blue. 1. Convert the following numbers from binary to decimal. a. 1110 8 + 4 + 2 = 14 b. 1010 8 + 2 = 10 c. 1110 0000 128 + 64 + 32 = 224 2. Convert the following numbers from decimal to binary. a. 39 39 = 32 + 7 = 32 + 4 + 3 = 32 + 4 + 2 + 1 = 10 0111 b. 100 100 = 64 + 36 = 64 + 32 + 4 = 110 0100 3. Below is the function to copy a picture to a new canvas with a left and a top margin that we saw in class. Show how you would change this function so that the top and bottom margin could be different from the left and right margins. def copyPicture ( picture , margin ) : def copyPicture ( picture, leftRightMargin, topBottomMargin ) : width = getWidth ( picture ) height = getHeight ( picture ) canvas = makeEmptyPicture ( width + margin * 2 , height + margin * 2 ) canvas = makeEmptyPicture ( width + leftRightMargin * 2 , height + topBottomMargin * 2 ) for y in range ( height ) : for x in range ( width ) : originalPixel = getPixel ( picture , x , y ) color = getColor ( originalPixel ) copyPixel = getPixel ( canvas , x + margin , y + margin ) copyPixel = getPixel ( canvas , x + leftRightMargin , y + topBottomMargin ) setColor ( copyPixel , color ) # This is indented to the same level as the “for y” line. return ( canvas ) page of 1 9
9
Embed
CS 100 Introduction to Computer Science Midterm Sample ...blerner/cs100/midterm/Sample...CS 100 Sample Midterm Solutions 4. Here is a function that modifies a picture to keep only
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
CS 100 Introduction to Computer Science Midterm Sample Solutions The answers below are shown in blue.
1. Convert the following numbers from binary to decimal. a. 1110
8 + 4 + 2 = 14 b. 1010
8 + 2 = 10 c. 1110 0000
128 + 64 + 32 = 224 2. Convert the following numbers from decimal to binary.
100 = 64 + 36 = 64 + 32 + 4 = 110 0100 3. Below is the function to copy a picture to a new canvas with a left and a top margin that
we saw in class. Show how you would change this function so that the top and bottom margin could be different from the left and right margins. def copyPicture ( picture , margin ) :def copyPicture ( picture, leftRightMargin, topBottomMargin ) :
for y in range ( height ) : for x in range ( width ) : originalPixel = getPixel ( picture , x , y ) color = getColor ( originalPixel )
copyPixel = getPixel ( canvas , x + margin , y + margin ) copyPixel = getPixel ( canvas , x + leftRightMargin , y + topBottomMargin )
setColor ( copyPixel , color ) # This is indented to the same level as the “for y” line. return ( canvas )
page ! of !1 9
CS 100 Sample Midterm Solutions
4. Here is a function that modifies a picture to keep only the redness in the pixels. def justRed ( picture ) : for pixel in getPixels ( picture ) : setGreen ( pixel , 0 ) setBlue ( pixel , 0 )
a. (10 points) Show how you would change this function so that it makes the changes on a new canvas created inside the function and sent back to the caller. The original picture should not be changed.
def justRed2 ( picture ) : canvas = makeEmptyPicture ( getWidth(picture) , getHeight(picture) ) for row in range (getHeight(picture)) : for col in range (getWidth(picture)) : pictPixel = getPixel (picture, col, row) canvasPixel = getPixel ( canvas, col, row) setGreen ( canvasPixel , 0 ) setBlue ( canvasPixel , 0 ) setRed ( canvasPixel, getRed (pictPixel) ) return canvas
b. (5 points) Write a test function that lets the user select a file with a file browser, calls the function you wrote in part a and displays the resulting picture to the user. def testJustRed () : pict = makePicture ( pickAFile()) newPict = justRed2 (pict) show (newPict)
page ! of !2 9
CS 100 Sample Midterm Solutions
5. For the function below, show what the function computes by filling in the grid on the right using the grid on the left as the original picture. def mystery ( picture ) : width = getWidth ( picture ) height = getHeight ( picture )
canvas = makeEmptyPicture ( width , height )
for y in range ( height ) : for x in range ( width / 2 ) : originalPixel = getPixel ( picture , x , y )
color = getColor ( originalPixel )
copyPixel = getPixel ( canvas , x , y ) setColor ( copyPixel , color ) copyPixel2 = getPixel ( canvas , width / 2 + x , y )
setColor ( copyPixel2 , color )
# This is indented to the same level as the “for y” line. return ( canvas )
page ! of !3 9
A B C D
E F G H
I J K L
M N O P
A B A B
E F E F
I J I J
M N M N
CS 100 Sample Midterm Solutions
6. For the function below, explain in English what the function displays on the screen when it is done.
redCanvas = makeEmptyPicture ( width, height ) greenCanvas = makeEmptyPicture ( width, height ) blueCanvas = makeEmptyPicture ( width, height ) for y in range ( height ) : for x in range ( width ) : originalPixel = getPixel ( picture , x , y ) redness = getRed ( originalPixel ) greenness = getGreen ( originalPixel ) blueness = getBlue ( originalPixel ) redPixel = getPixel ( redCanvas , x , y ) greenPixel = getPixel ( greenCanvas , x , y ) bluePixel = getPixel ( blueCanvas , x , y )
setColor ( redPixel , makeColor (redness , 0 , 0 ) ) setColor ( greenPixel , makeColor (0 , greenness , 0 ) ) setColor ( bluePixel , makeColor (0 , 0 , blueness ) ) # These are indented to the same level as the “for y” line. show ( redCanvas ) show ( greenCanvas ) show ( blueCanvas )
It shows 3 canvases, one contains just the red values from the original image, one just the green values and one just the blue values.
page ! of !4 9
CS 100 Sample Midterm Solutions
7. Briefly describe what each of these functions does. a. def mystery1 () :
canvas = makeEmptyPicture (100, 100) for row in range ( getHeight ( canvas ) ) : for col in range ( row ) : pixel = getPixel ( canvas , col , row ) setColor ( pixel , makeColor ( 255 , 0 , 0 ) ) # show lines up with the "for row" line show ( canvas )
This creates a new picture that is 100 pixels wide and 100 pixels tall. It draws a red triangle in the bottom left corner. It displays the draw-ing.
b. def mystery2 ( picture , newColor ) : for row in range ( 0, getHeight ( picture ) , 10 ) : for col in range ( 0, getWidth ( picture ) , 10 ) : pixel = getPixel ( picture, col, row ) setColor ( pixel , newColor )
This changes an existing picture by placing dots in every 10th row and 10th column. The color of the dots is determined by the newColor parameter. The modified picture is not displayed by the function.
8. Do these two functions do the same thing or different things? Explain your answer. def mystery3 (picture) : for pixel in getPixels ( picture ) : setRed ( pixel , 0 )
def mystery4 (picture ) : width = getWidth ( picture) height = getHeight ( picture ) for col in range ( 0 , width / 2 ) : for row in range ( 0 , height / 2 ) : setRed ( getPixel ( picture , col, row ) , 0 )
They do not do the same thing. mystery3 removes all the red from every pixel in the pic-ture. mystery4 only removes red from the top, left corner of the picture. This is because mystery3 operates over all the pixels, while mystery4 only considers pixels that are in the left half of the columns and top half of the rows.
page ! of !5 9
CS 100 Sample Midterm Solutions
9. The following function does not work correctly. The programmer expects a result like the picture on the left, but gets the result on the right. How can you fix this function?
def buggyChangeCorner ( picture , newColor ) :
# Visit the top half of the rows
for row in range ( getHeight ( picture ) / 2 ) :
# For each row, visit the left half of the row
for col in range ( getWidth ( picture ) / 2 ) :
# Change the color of the pixel
pixel = getPixel ( picture , col, row )
setColor ( pixel , newColor )
# This is indented to line up with the “for row” line.