联系方式

  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-23:00
  • 微信:codinghelp

您当前位置:首页 >> Python编程Python编程

日期:2021-04-22 11:16

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


版权所有:留学生编程辅导网 2020 All Rights Reserved 联系方式:QQ:99515681 微信:codinghelp 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。 站长地图

python代写
微信客服:codinghelp