• Non ci sono risultati.

Introduction to Computer Science Application Final Exam

N/A
N/A
Protected

Academic year: 2021

Condividi "Introduction to Computer Science Application Final Exam"

Copied!
1
0
0

Testo completo

(1)

Introduction to Computer Science Application Final Exam

Ugo Dal Lago

Marco Di Felice

June 11, 2009

Exercise 1.

Given a function fun, defined as follows:

def fun(a,b,c):

if (a>1):

return 2*fun(a-1,b+1,c) elif (b>2):

return 2+fun(a,b-1,c) else:

return c

Decide what is the value of the following three expressions: fun(1,4,7), fun(2,3,2) and fun(3,1,6)

Exercise 2.

What is the output of the following program?

a=[0,1,2]

b=a a[2]=b[1]

a[1]=[3,2,5]

c=b[1]

c[2]=7 (a,b)=(b,c) print a print b print c Exercise 3.

Write a function multiplyLists which takes two arguments, a list l1 and another list l2, and returns a third list r with length equal to the length of l1. Every element of r is obtained from l2 by multiplying all of its elements by the corresponding element of l1. For example multiplyLists([1,4,2],[3,7]) should return [[3,7],[12,28],[6,14]].

Exercise 4.

Any triangle in the two-dimensional cartesian plane can be described by an object of the class

dallago@cs.unibo.it

difelice@cs.unibo.it

Triangle. Every triangle can be described by three pairs of floating point numbers (x1,y1), (x2,y2) and (x3,y3), which become the at- tributes of the class. Write the definition of that class Triangle. The class has three methods:

• A constructor Triangle, which creates a new object of the class, given its six coor- dinates.

• A modifier translate with two arguments dx and dy, which translates the triangle by dx along the x axis and by dy along the y axis.

• The method str , whose behaviour should be as expectable as possible.

Exercise 5.

Write a function deepSum which returns the sum of all the natural numbers appearing in an arbi- trary nested list of natural numbers. For exam- ple, deepSum([[2,6,0],[[1],12],7]) should return the natural number 2+6+0+1+12+7=26.

Hint: you can use the built-in function isinstance(o,t) which returns True if and only if o is an object of type t.

1

Riferimenti

Documenti correlati

The main idea, in the application of elastic wavelets to the evolution of solitary waves, consists not only in the wavelet representation of the wave, but also in the assumption

[r]

Solution proposed by Roberto Tauraso, Dipartimento di Matematica, Universit`a di Roma “Tor Vergata”, via della Ricerca Scientifica, 00133 Roma,

Solution proposed by Roberto Tauraso, Dipartimento di Matematica, Universit`a di Roma “Tor Vergata”, via della Ricerca Scientifica, 00133 Roma, Italy. (a) Without loss of generality

• The initialization method, which takes as argument the list of Cluster objects cre- ated by the K-Means algorithm and the number of iterations performed by the al- gorithm. • A

By using the function safeDistance(tl,car) defined above, write a function move(tl,car,tstep) which takes a TLight object and a Car object as arguments and updates the car’s

The file is compiled in final mode: figures are inserted at their position; the caption on every slide contains the text given (optionally) by the user with the macro

[r]