Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

1 2 3 4 5 TOTAL

( 20 ) ( 20 ) ( 20 ) ( 20 ) ( 20 ) (100)

CS101 Introduction to Programming 2013 Fall Midterm Examination

SECTION STUDENT ID NAME

※ Please check to see if you have all 14 pages in your test material.
※ 시작하기 전에 반드시 페이지의 수를 확인 하십시오.(전체 : 14 쪽)
※ Fill in your section, student identification number and name. Or you will lose 1 point for each
missing piece of information
※ 위의 정보(분반,학번,이름)를 정확히 기입하지 않을 경우, 각 실수 당 1점이 감점 됩니다.
※ We will not answer questions about the problems during this exam. If you think there is
anything ambiguous, unclear or wrong about a problem, please write down your reasons and
make necessary assumptions to proceed with the problem. We will take your explanation into
consideration when grading.
※ 시험시간동안 질문을 받지 않습니다. 만일 문제에 오류나 문제가 있을 경우, 왜 문제가 이상이 있다
고 생각하는지에 대해서 기술하시면 되겠습니다. 또한 문제가 애매하다고 생각되는 경우 문제를
푸실 때 본인이 생각하는 가정을 함께 작성하셔서 문제를 푸시면 되겠습니다. 채점 시 가정 및 설
명을 고려하도록 하겠습니다.

1. (20 points) Answer each question.


1-1. (4 points) Compare for loops and while loops. Describe how they are different.

While for loops repeat instructions , while loops repeat


instructions .

1-2. (4 points) The following example shows types of some complicated objects:

>>> type(Robot())
<class ’cs1robots.Robot’>
>>> type( (3, -1.5, 7) )
<type ’tuple’>

Describe the differences between <class ’xxx’> and <type ’xxx’> in terms of how objects
are defined.

When object types are , their types are <class ’xxx’>.


When object types are , their types are <type ’xxx’>.

- 1 -
1-3. (4 points) For a given image, we often visit every pixel of the image as follows:

w, h = img.size()
for y in range(h):
for x in range(w):
p = img.get(x, y)
...

In which order, do we visit the pixels? Notice that the answer format should be
(x-coordinate,y-coordinate). Fill in the blanks: (0,0) ( , ) ( , ) ... ( , ) ( , )

1-4. (4 points) Consider the following example:

from math import sin, pi


print math.sin(pi / 4)
print sin(pi / 5)
print math.cos(pi / 4)
print cos(pi / 4)

Among 4 print statements, what are valid and what are not? Write OK or Error:
print math.sin(pi / 4)
print sin(pi / 5)
print math.cos(pi / 4)
print cos(pi / 4)

1-5. (4 points) Describe how mutable and immutable objects are different.

Mutable objects ,

but immutable objects .

2. (20 points) Answer each subproblem according to the following instructions.


2-1 ~ 2-3. (6 points) Write down the expected results of the following programs.
2-1. (2 points)

def print_message():
print “CS101 is fantastic!”
print “Programming is so much fun!”

def repeat_message():
print_message()
print_message()

repeat_message()

- 2 -
2-2. (2 points)

print not 3 < 5

2-3. (2 points)

for i in range(3):
print “!” * (i + 2)

2-4. (4 points) <Figure 1> depicts a state of the program after executing 3rd line of the
following source code. Complete <Figure 2> to describe a state of the final result upon
completion of the program. Notice that a Robot object with ‘yellow’ body is
represented by ‘○’ and a Robot object with ‘blue’ body is represented by ‘●’. Describe
directions the Robot objects face as arrows by using ‘←’, ‘→’, ‘↑’, or ‘↓’, respectively.

<Figure 1> <Figure 2>

- 3 -
Program

line 01 from cs1robots import * line 07 hubo.move()


line 02 create_world() line 08 hubo = Robot(“blue”)
line 03 hubo = Robot(“yellow”) line 09 hubo.move()
line 04 hubo.move() line 10 ami.turn_left()
line 05 ami = hubo line 11 ami.move()
line 06 ami.turn_left()

2-5. (10 points) Fill out the box with “T” if the corresponding description is correct, or
with “F” if the description is incorrect. Please notice that you will get 1 point per
description for a correct answer.

Description Answer
(Example) Cheating is strongly recommended. F
We cannot define new instructions and raise the level of abstraction.
A program implements an algorithm (a recipe for solving a problem).
The keyword ‘not’ inverts the sense of the condition: not True is False.
There is a Python object that does not have a type.
The first character of a variable can be _ (underscore).
What Python objects can do depends on the name of object.
Packing and unpacking cannot occur in one line.
A parameter can be used outside the function.
Global variables can be used inside a function.
A function is an object.

× Hints for 3~5.

- create_world(avenues=5, streets=5)
Two arguments, avenues and streets, denote the 2D size of the world, width
and height respectively.
- hubo=Robot(color = "red", orientation = "N",avenue = 3,street = 1)
The first two arguments denote the color and orientation of hubo in order.
The rest two arguments represent a 2D position in a world, column and row
respectively.
- hubo.move(), hubo.turn_left(), hubo.front_is_clear(),
hubo.left_is_clear() hubo_right_is_clear(), hubo.pick_beeper(),
hubo.drop_beeper() hubo.carries_beepers(), hubo.on_beeper(),
hubo.facing_north()
The hubo has methods including the above.

- 4 -
3. (20 points) You are asked to complete a program which makes hubo fill the
empty space with beepers as shown below. This question is from your first
homework.

from cs1robots import *


load_world('number1.wld')
hubo=Robot(orientation = "N",avenue = 3,street = 1)

def turn_right():

(3-1)

def follow_right_wall():

(3-2-1)
turn_right()
hubo.move()

(3-2-2)
hubo.move()

- 5 -
(3-2-3)
hubo.turn_left()

hubo.move()

(3-3)

# 'carries_beepers()' function checks whether hubo has beeper.


# If not, it returns false.
while hubo.carries_beepers():
follow_right_wall()

(3-4)

3-1. (4 points) Hubo doesn’t have turn_right() function. Therefore you should
make turn_right() function using turn_left() function and for-statement.

3-2. (6 points) You should fill the three blanks to make hubo move in the
manner of ‘follow-right-wall’. The function follow_right_wall() means that if
front of hubo is clear then hubo should move, if hubo’s right is clear then
hubo should take a right turn and move, otherwise hubo should take a left
turn.

- 6 -
*You must use conditional statements. Also each answer should be just one
line.

3-2-1 (2 points)

3-2-2 (2 points)

3-2-3 (2 points)

3-3. (5 points) In this blank, hubo should pick up all beepers on the place.
You must use while statement for doing this.

3-4. (5 points) In this blank, if hubo is not on beepers, hubo should drop a
beeper.

- 7 -
4. (20 points) Answer each question according to the instruction.
4-1. (11 points) Hubo wants to harvest all beepers in the world. Implement each function
according to the instruction

from cs1robots import *


load_world("harvest.wld")
hubo = Robot(avenue=5,street=1)
def turn_right():
for i in range(3):
hubo.turn_left()
def pick_and_move_diagonal():

def turn_around_diagonal_at_right():

def turn_around_diagonal_at_left():

def go_around_the_world():

go_around_the_world()

- 8 -
4-1-1. (3 points) Function pick_and_move_diagonal() makes hubo pick all of beepers in the
current position, and move diagonal. Implement a function pick_and_move_diagonal().

def pick_and_move_diagonal():

4-1-2. (2 points) Function turn_around_diagonal_at_left() makes hubo pick all of beepers in the
current position, and move appropriate position to pick beepers at the next diagonal line at
the left side of map. Implement a function turn_around_diagonal_at_left().

def turn_around_diagonal_at_left():

- 9 -
4-1-3. (2 points) Function turn_around_diagonal_at_right() makes hubo pick all of beepers in
the current position, and move to appropriate position to pick beepers at the next diagonal
line at the right of map. Implement a function turn_around_diagonal_at_right().

def turn_around_diagonal_at_right():

4-1-4. (4 points) Implement go_around_the_world() using three functions already implemented


to pick all the beepers in the world. Function pick_and_move_diagonal(),
turn_around_diagonal_at_left(), and turn_around_diagonal_at_left() is working as an example.

def go_around_the_world():

- 10 -
4-2. (9 points) Hubo wants to traverse all over the world in zigzag1 and zigzag2. Fill in the
blank following the instructions.

from cs1robots import *


load_world("zigzag1.wld")
# or load_world("zigzag2.wld")
hubo = Robot(orientation="N")
hubo.set_trace("blue")

def turn_right():
hubo.turn_left()
hubo.turn_left()
hubo.turn_left()

while : # Do zigzag during appropriate condition

while :
# Go straight to the Northern side
hubo.move()

turn_right() # Check whether the next avenue is


if : exist or not, if the next avenue is
hubo.move() exist then makes hubo move and get
turn_right() ready for next movement

while :
# Go straight to the southern side
hubo.move()
and Check whether the next avenue is
hubo.turn_left()
exist or not, if the next avenue is
exist then makes hubo move and get
ready for next movement

- 11 -
5. (20 points) Answer each question according to the instruction.
5-1. (6 points) What is the result of the following program?

def MyFunction1() :
return MyFunction2(5)

def MyFunction2(value) :
return -value

def MyFunction3(value) :
if value < 0 :
return -value
return value

def MyFunction4(value1, value2) :


return value2 + value1

result = MyFunction1()
print result
print MyFunction2(MyFunction2(MyFunction2(MyFunction2(-10))))
print MyFunction4(MyFunction4(9, 3), MyFunction3(-3))
print MyFunction4(MyFunction4("CS","101"),MyFunction4("CS"*2,"101"))

- 12 -
5-2. (6 points) What is the result of the following program? In other words, draw the
exact trace of the robots, named 'Hubo' and 'Ami' in the world. You should express an
ending position as "□" with directions ("←", "↑", "→", or "↓") of the robots, respectively.
(You don't need to draw the trace caused by multiple turns in the same position.)

def Turn_Around(Robot) :
for i in range(2) :
Robot.turn_left()

def Check(Robot) :
Turn_Around(Robot)
if Robot.front_is_clear() :
Turn_Around(Robot)
return Robot.front_is_clear() and True
Turn_Around(Robot)
return False

def MyMove(Robot1, Robot2) :


if not Check(Robot1) or Check(Robot2) :
Robot2.move()
while Check(Robot2) and Robot1.left_is_clear() :
Robot1.move()
Robot2.turn_left()
Robot2.move()
while Check(Robot2) :
Robot2.move()
return Robot1

create_world(avenues=5, streets=5)
Hubo = Robot(color="gray", orientation='E', avenue=1, street=1)
Ami = Robot(color="yellow", orientation='W', avenue=5, street=5)
MyMove(Hubo, Ami).move()

- 13 -
5-3. (3 points) Draw the result of the following program. You should paint the black
pixels of image object named 'img'.

<answer for img>


img = create_picture(5,5,Color.white)
(0,0) (1,0) (2,0) (3,0) (4,0)
w, h = img.size()
(0,1) (1,1) (2,1) (3,1) (4,1)
for y in range(h) :
for x in range(w) : (0,2) (1,2) (2,2) (3,2) (4,2)

if (x == y) or (not (x * y != 0)) :
(0,3) (1,3) (2,3) (3,3) (4,3)
img.set(x, y, Color.black)
(0,4) (1,4) (2,4) (3,4) (4,4)
img.show()

5-4. (5 points) Draw the result of the following program. You should paint the black
pixels of image objects named 'img2' and 'img3'.

<answer for img2>


img2 = create_picture(5,5,Color.white)
(0,0) (1,0) (2,0) (3,0) (4,0)
w, h = img2.size()
(0,1) (1,1) (2,1) (3,1) (4,1)
for y in range(h) :
for x in range(w) : (0,2) (1,2) (2,2) (3,2) (4,2)

if (y % 2) == 0 :
(0,3) (1,3) (2,3) (3,3) (4,3)
img2.set(x, y, Color.black)
(0,4) (1,4) (2,4) (3,4) (4,4)
img2.show()

<answer for img3>


img3 = create_picture(5,5,Color.white) (0,0) (1,0) (2,0) (3,0) (4,0)

w, h = img3.size() (0,1) (1,1) (2,1) (3,1) (4,1)


for y in range(h) :
(0,2) (1,2) (2,2) (3,2) (4,2)
for x in range(w) :
img3.set(x, y, img2.get(y, x)) (0,3) (1,3) (2,3) (3,3) (4,3)

img3.show() (0,4) (1,4) (2,4) (3,4) (4,4)

- 14 -

You might also like