PracticeFinal SOL S2019 1 PDF

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

Practice Final Exam

CPE101

1. Use the definition of a Point below for this question. Write a function distances that takes
a list of Points. The function returns a list of the distances between each Point in the list. See
below for an example. For full credit you must decompose your solution into multiple functions.

class Point:
def __init__(self, x, y):
self.x = x
self.y = y

>>> in_list = [Point(0,0), Point(0,5.5), Point(3,5.5),


Point(3,7.5)]
>>> distances(in_list)
[5.5, 3.0, 2.0]
def dist(p1, p2):
return sqrt((p1.x-p2.x)**2 + (p1.y-p2.y)**2)

def distances(points):
res = []
for i in range(len(points)-1):
res.append(dist(points[i], points[i+1]))
return res

2. Given the function below, what do the following function calls print?

1 def f(x, y, z):


2 if x > y + z:
3 x += 4
4 if x > z * 3:
5 print ('yay', x)
6 elif y == x:
7 z = 10
8 if z > 10:
9 print ('zzzzz')
10 else:
11 y = 1
12 print ('one')
13 print (x, y, z)

This study source was downloaded by 100000840397610 from CourseHero.com on 03-20-2024 20:18:37 GMT -05:00

https://www.coursehero.com/file/103540890/PracticeFinal-SOL-S2019-1pdf/
f(10, 4, 3)

yay 14
one
14 1 3
f(3, 3, 12)

one
3 1 10
f(100, 1, 98)

zzzzz
104 1 98

3. Write a function shuffle that takes two lists as arguments. It returns a new list consisting of
alternating members of the input lists, starting with the first list. If one input list runs out of
members, the remaining items will be from the other list. For example:
>>> shuffle([2, 7, 1, 4], [6, -1]) ➔ [2, 6, 7, -1, 1, 4]
19 def shuffle(l1, l2):
20 res = []
21 len1 = len(l1)
22 len2 = len(l2)
23 for i in range(max(len1,len2)):
24 if i < len1:
25 res.append(l1[i])
26 if i < len2:
27 res.append(l2[i])
28 return res
29 print(shuffle([2, 7, 1, 4], [6, -1]) )
4. Write a function sequence that takes a list of numbers and returns the length of the longest
contiguous (i.e., touching) repeating sequence of the same number. For example, when called
on [1, 1, 5, 5, 5, 2, 4, 4, 5, 4, 4] the function will return 3 because 5 repeats three times early in
this list and no other contiguous sequence is longer.

31 #list -> int


32 def sequence(list):
33 if len(list) == 0:
34 return 0
35 long = 1
36 cur = 1
37 for i in range(len(list)-1):
38 if list[i] == list[i+1]:
39 cur += 1
40 if cur > long:

This study source was downloaded by 100000840397610 from CourseHero.com on 03-20-2024 20:18:37 GMT -05:00

https://www.coursehero.com/file/103540890/PracticeFinal-SOL-S2019-1pdf/
41 long = cur
42 else:
43 cur = 1
44 return long
45 print(sequence([1, 1, 5, 5, 5, 2, 4, 4, 5, 4, 4]))

5. Trace the following partial code and write the output for every call function.

def divide(x, y):


try:
res = x/y
raise NameError
except ZeroDivisionError:
print('divided by Zero')
except Exception as msg:
print('Some exception happened ', msg)
else:
print('result = ', res)
finally:
print('End of function')

divide(21,2)
Some exception happened
End of function
divide(7,0)
divided by Zero
End of function
divide(‘5’,’2’)
Some exception happened unsupported operand type(s) for /: 'str' and 'str'
End of function

1. What is the output of this program?


def f1(n):
try:
print('try')
raise ValueError()
except ValueError:
print ('My First Exception')
try:
raise NameError()
except NameError:
print('My Second Exception')
finally:
print('My Finally')
f1(5 )

This study source was downloaded by 100000840397610 from CourseHero.com on 03-20-2024 20:18:37 GMT -05:00

https://www.coursehero.com/file/103540890/PracticeFinal-SOL-S2019-1pdf/
try
My First Exception
My Second Exception
My Finally

2. Sort the following list using insertion and selection sort. Show all steps.

Lst = [-9, 23, 19, 78, 0, 11]


The same way we did in class.
Selection Sort:

L= [-9, 23, 19, 78, 0, 11]


Step 1: [-9, 23, 19, 78, 0, 11]
Step 2: [-9, 0, 19, 78, 23, 11]
Step 3: [-9, 0, 11, 78, 23, 19]
Step 4: [-9, 0, 11, 19, 23, 78]
Step 5: [-9, 0, 11, 19, 23, 78]
Sorted L: [-9, 0, 11, 19, 23, 78]

Insertion Sort:
L= [-9, 23, 19, 78, 0, 11]
Step 1: [-9, 23, 19, 78, 0, 11]
Step 2: [-9, 19, 23, 78, 0, 11]
Step 3: [-9, 19, 23, 78, 0, 11]
Step 4: [-9, 0, 19, 23, 78, 11]
Step 5: [-9, 0, 11, 19, 23, 78]
Sorted L: [-9, 0, 11, 19, 23, 78]

This study source was downloaded by 100000840397610 from CourseHero.com on 03-20-2024 20:18:37 GMT -05:00

https://www.coursehero.com/file/103540890/PracticeFinal-SOL-S2019-1pdf/
Powered by TCPDF (www.tcpdf.org)

You might also like