Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

234128 Introduction to Computing with Python

Term A – 2022/09/13 14:00- 17:00

Section I - Multiple Choice: (choose only one answer) (40pts)

For questions 1-4, refer to the following code segment:

strings = ['alice', 'bo', 'ozzy', '', 'olivia', 'charles', 'ivy'] print(sorted(strings, key=func))

1.   What will be printed given the following key?

def func(item):

return abs(len(item) - 10)

a.   ['', 'bo', 'ivy', 'ozzy', 'alice', 'olivia', 'charles']

b.   3

c.    ['alice', 'bo', 'ozzy', '', 'olivia', 'charles', 'ivy']

d.   ['bo', 'ivy', 'ozzy', '', 'alice', 'olivia', 'charles'] e.   ['charles', 'olivia', 'alice', 'ozzy', 'ivy', 'bo', '']

f.    -3

2.   What will be printed given the following key?

def func(item):

return 0

a.   ['ivy', 'bo', 'ozzy', 'alice', '', 'olivia', 'charles'] b.   ['alice', 'bo', 'ozzy', '', 'olivia', 'charles', 'ivy']

c.    [0, 0, 0, 0, 0, 0, 0]

d.   ['0', '0', '0', '0', '0', '0', '0']

e.   ['', 'bo', 'ivy', 'ozzy', 'alice', 'olivia', 'charles']

f.    [‘’]

3.   What will be printed given the following key?

def func(item):

if item:

return ord(item[len(item)//2])

return float('inf')

a.   ['alice', 'bo', 'charles', 'olivia', 'ivy', 'ozzy', '']

b.   []

c.    ['ivy', 'bo', 'ozzy', 'alice', 'olivia', 'charles', '']

d.   ['alice', 'bo', 'ozzy', '', 'olivia', 'charles', 'ivy']

e.   ['', '', '', '', '', '', '']

f.    ['bo', 'ivy', 'ozzy', 'alice', 'olivia', 'charles', '']

4.   What will be the new output if we change the print line as follows?

print(sum([len(s) / len([s for s in strings if s]) for s in strings]))

a.   4

b.   27

c.   27.0

d.   7

e.   4.5

f.    3

5.   Given the following function:

def f1(n):

while n < 10**6: print(n)

n = n**2 + 1

The input parameter n is a number (any integer or float is possible). What is the time complexity of f1(n)?

a.   0(1)

b.   0(logn)

c.   0(n)

d.   0(nlogn)

e.   0(n2)

f.    0(2n )

6.   Given the following function:

def f2(n):

i = 1

t = 0

while i <= n:

for j in range(n**2): t += 1

i *= 2

return t

What is the time complexity of the function?

a.   0(1)

b.   0(logn)

c.   0(nlogn)

d.   0(n2 logn)

e.   0(n)

f.    0(n2)

7.   Given the following code:

def func(s):

if len(s) == 2:

print(s[0], end='')

return

print(s[-1], end='')

func(s[::2])

What will be printed when running func('python')?

a.   ‘

b.   ‘python’

c. pto’

d.   ‘ptoo’

e. nhy’

f. nop

8.   Which of the following expressions evaluates to True?

a.   1 in list(range(ord('0'), ord('9')+1)

b.   type(5>9) != bool

c.   int(int('6') * int('6')) in list(range(36,66,6))

d.   len(list(range(50))) > 100

e.   5.5 in list(range(5,10))

f.    All expressions return False

Section II - Open Questions: (65pts)

Answer the following questions in the included answer sheet.

Write clear and readable code and pay attention to indentation. You may add notes or comments explaining your solution.

If you write I don’t know” as an answer, you will get 20% of the score. This is recommended if you know that you don’t know the answer.

Question 1: (15pts)

Part I: (7pts)

What will be printed when running the following program:

def print_something(m): for r in m:

for i in r:

print(i)

break

for r in m:

print(r)

break M=[[1,2,3,4], [5,6,7,8]] print_something(M)

Part II: (8pts)

What will be printed when running the following program:

def fun(string, k, s): # abcdefghijklmnopqrstuvwxyz lst = [c.lower() for c in string]    for i in range(len(lst)):

c = lst[i]

if c.islower():

tmp = ord(c)+s

lst[i] = chr(tmp)

lst = lst[k:] + lst[:k]

lst[0] = lst[0].upper()

return ''.join(lst)

print(fun('AOU', 2, 4))

Question 2: (15pts)

Write a function: contains_duplicate(s)

The function gets a string and returns True if the string includes any duplicate characters. Otherwise, the function returns False. For example:

contains_duplicate(‘moon’) == True

contains_duplicate(‘sun’) == False

contains_duplicate(‘stars’) == True

contains_duplicate(‘!’) == False

The string s is composed of at least one character.

Requirement : Time complexity of 0(nlogn) or better, given n = len(s).

Question 3: (25pts)

Part I: (20pts)

Write a function: add_x(num, x)

The function gets two integer numbers as input parameters: 0 ≤ num,  0 ≤ x ≤ 9.

Assume input parameters are legal. The function computes a new number composed of the digits of num with x added to every digit. The function returns this new number. For example:

add_x(3521, 3) == 6854

add_x(673, 2) == 895

If adding x to a digit of num results in a number bigger than 9, only the units digit is considered. For example:

add_x(48, 5) == 93

add_x(38958, 2) == 50170

add_x(69435, 4) == 3879

Requirement : Using lists or strings is NOT allowed. A solution with lists or strings will receive a maximum of 10 points.

Part II: (5pts)

Write a function: check_nums(num1, num2)

The function gets two positive integers num1 and num2 as input. The function    checks if adding a digit using add_x() to one of the numbers results the other number. If yes, return this digit. Otherwise, the function returns -1. For example:

check_nums(123, 678) == 5

check_nums(6482, 2048) == 6

check_nums(123, 679) == -1

You may use the function from Part I, even if you didn’t solve it.

Question 4: (10pts)

Write a function: reduce_string(s)

The function gets a string as input parameter. The function returns a new string composed of the characters from s such that each appears only once . The function ignores the space character – ‘ ‘ .

For example:

reduce_string(‘good luck everyone!’) == ‘godluckevryn!’

reduce_string(‘666’) == ‘6’

reduce_string(‘gtiit’) == ‘gti’