PracticeFinal SOL S2019 1 PDF
PracticeFinal SOL S2019 1 PDF
PracticeFinal SOL S2019 1 PDF
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
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?
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.
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.
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
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.
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)