# Comparative Programming Languages

Λογισμικό & κατασκευή λογ/κού

7 Νοε 2013 (πριν από 4 χρόνια και 6 μήνες)

66 εμφανίσεις

University of Cape Town ~ Department of Computer Science

Computer Science 1018F ~ 2008

Test 1

Marks

: 30

Time

: 40 minutes

Instructions:

a)

b)

c)

Show all calculations where
applicable.

Student Number

: __________________________
________

Question

Max

Mark

Internal

External

1

8

2

4

3

18

TOTAL

30

Name:

______________________

______________________

Student Number
:

______________________

2

Question 1 [8]

a)

Python is said to have strong and dynamic typing. What does this mean in practice?

[3]

_________________________________________________________________________

__________________________________________________________________
_______

_________________________________________________________________________

_________________________________________________________________________

dynamic typing means that types are dicovered at run
-
time, so that the types of variables do
not ha
ve to be explicitly declared. Strong typing means that types are enforced and cannot
be interchanged without some form of type conversion, so types cannot be arbitrarily mixed
in an assignment without converting them to a common type.

b)

In what fundamental

way does a tuple differ from a list?

[2]

_________________________________________________________________________

_________________________________________________________________________

Tuples are immutable while lists are mutable. This means that tupl
es cannot (unless they
have mutable members) be changed once they have been created, unlike lists.

c)

Write a Python statement that will print out
Hello World!

[1]

_________________________________________________________________________

_____________________
____________________________________________________

print ‘Hello World!’

d)

Why is whitespace considered significant in the Python programming language?

[2]

_________________________________________________________________________

___________________________
______________________________________________

whitespace is used to indicate the structure of a Python program. Tabs and spaces
determine indentation which is used to define a block of statements and carriage returns are
the seperator between statements.

3

Question 2 [10]

Consider the following program and answer the questions that follow.

def dosomething():

a = raw_input()

ai = int(a)

b = raw_input()

bi = int(b)

c = raw_input()

ci = int(c)

x = (ai+bi+ci)/3;

print x

a)

What does this program do?

[2]

_________________________________________________________________________

_________________________________________________________________________

calculates the average of 3 integers and prints out the result

b)

What

is the output if the input is the numbers 3, 5 and 5?

[1]

_________________________________________________________________________

_________________________________________________________________________

4

c)

How would you fix the calculation of the variab
le
x

so that the answer is not rounded off?
[1]

_________________________________________________________________________

_________________________________________________________________________

change 3 to 3.0

4

Question 3 [18]

Assume that an image compose
d of pixels in the range 0 (black) to 255 (white) is represented as a
list of lists of integers, with each sublist representing a row of pixels.

a)

Show how you would allocate an image with the above format to the variable img. The image
has a 3x3 resolution
and contains the following pixel data:

[3]

0

255

0

255

255

255

0

255

0

_________________________________________________________________________

_________________________________________________________________________

___________________
______________________________________________________

img = [ [0, 255, 0], [255, 255, 255], [0, 255, 0]]

b)

Write a function

shift

to the pixels of the image
img

and return the results as a new image. Your functi
on should have a doc string and use for loops

[7]

_________________________________________________________________________

_________________________________________________________________________

____________________________
_____________________________________________

_________________________________________________________________________

_________________________________________________________________________

______________________________________________________________
___________

_________________________________________________________________________

_________________________________________________________________________

_________________________________________________________________________

______________________
___________________________________________________

>>

""" Add the shift value to every pixel in img

>>

and return the resulting image. """

>>

>>

retimg = []

>>

for row in img:

>>

retrow = []

>>

for pixel in row:

>>

v = pixel + shift

>>

retrow.append(v)

>>

retimg.append(retrow)

>> return retimg

5

c)

Add a check to make sure that shifted pixel values do not go outside the bounds [0, 255].

[4]

______________________________________________________
___________________

_________________________________________________________________________

_________________________________________________________________________

_________________________________________________________________________

>> if v < 0:

>>

v = 0

>> elif v > 255:

>> v = 255

d)

Now rewrite the function Adjust (in part (b)) so that it uses list comprehension. Do not worry
about including a doc string or the bounds check this time.

[4]

____________________________________________
_____________________________

_________________________________________________________________________

_________________________________________________________________________

_________________________________________________________________________

____
_____________________________________________________________________