Python编程设计讲解、辅导programming编程、Python程序调试 辅导Database|辅导Python程序

- 首页 >> C/C++编程
Mock exam Python programming
1 Exercise 1
Write a function which takes two lists and returns a list which contains only
those elements which occur either in the first list, or in the second, but not in
both. The order of the elements can be arbitrary, but you have to preserve the
number of occurrences of each element, i.e., if say 14 occurs 3 times in the first
list, it should occur 3 times in the resulting list. For example, for the lists
a = [ 1 , 1 , 2 , 3 , 5 , 8 , 1 3 , 2 1 , 3 4 , 2 1 , 5 5 , 8 9]
b = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 1 0 , 1 1 , 1 2 , 1 3 , 6]
the function should return [21, 34, 21, 55, 89, 4, 6, 7, 9, 10, 11, 12, 6].
Hint: in order to test if an element x occurs in the list mL use
x i n mL
2 Exercise 2
Write a function which takes as a parameter a list [a1, a2, a3, . . . , an] and returns
the list of cumulative sums [a1, a1+a2, a1+a2+a3, . . . , a1+· · ·+an]. For example,
for the list [1, 2, 3, 4, 5] the result should be [1, 3, 6, 10, 15].
3 Exercise 3
Write a function which takes a list of integers as a parameter and returns the
list of those elements of the original list which are prime numbers. For example,
for the list [4,12,9,8,6,7,11,12,5,16,7] the function should return [7,11,5,7] .
4 Exercise 4
Write a function which copies every kth line of the input file into the output
file. If the input file is
AAA
BBB
CCC
1
DDD
EEE
FFF FFF
GGG
then for k = 2 the result should be
BBB
DDD
FFF FFF
and for k = 3 it should be
CCC
FFF FFF
That is, for k = 2 the function copies every second line, for k = 3 it copies every
third line. The integer k and the names of the input and output files should be
parameters of the function.
2

站长地图