0% found this document useful (0 votes)
12 views11 pages

Part A

Uploaded by

ming55661
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views11 pages

Part A

Uploaded by

ming55661
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

University Number:

THE UNIVERSITY OF HONG KONG

FACULTY OF ENGINEERING
DEPARTMENT OF COMPUTER SCIENCE

Part A
ENGG1330 Computer Programming I

Date: 9 December 2022


Time: 2:30 pm - 4:30 pm

This examination consists of three parts: A to C. This is Part A.


Answer ALL questions in the space provided.
Use of a calculator is not allowed.
Write your university number in the space provided at the top of every odd-numbered page.

Your Mark
Max. Mark
(examiner use only)

42

Part A, Page 1 / 4
Write down the output of the following 14 programs in the space provided. We will ignore spaces when
marking your output. Each program only has a single line of output. A correct output is worth +3 marks and
an incorrect output 0 marks.

Program Output

1:
i = 42
ii = i//2
print(i, ii, i/ii, i//ii, i%ii)

2:
a = 2.0
b = a + 1
print(2**256%2, -1.0**18, a*b**b)

3:
x = 12.345
print(x, str(x)*2, f'{x:.2f}')

4:
ar = 2, 3
for i in range(5, 15, 4):
ar += i,
print(ar)

5:
ar = [2, 3]
for i in range(15, 5, -4):
ar += i,
print(ar[::-1])

6:
b = [21]
a = [42, b]
b = [2]
print(a)

7:
x = [1, 2, 3]
xx = x[1:]
x[2] = 42
print(xx, x)

Part A, Page 2 / 4
University Number:

8:
d = {}
for i in range(5):
d[i] = i**2
print(d)

9:
doc = '<html>Hello world!!!</html>'
print(doc.lower().strip('<>html'))

10:
s = ''
ii = 7
while True:
i = ii
while i>2:
i -= 3
s += str(i)+'+'
s += str(i)+'-'
ii += 1
if ii%2:
break
print(s)

11:
def a(c):
c += 1
return c
def b(c):
c += 3
d = a(c)
d += c
return d
def c(a):
a += 2
return b(a)
print(c(6))

Part A, Page 3 / 4
12:
def r(p, s):
s[0] += str(p)
if p<7:
r(p+1, s)
p = p+1
s[0] += str(p)
p = 5
s = ['']
r(p, s)
print(p, s)

13:
n = 'Schnieders'
s = ''
for v in n:
l=[i for i, w in enumerate(n) if w==v]
s += str(l[-1])
print(s)

14:
def x(a):
if a:
return str(a)
else:
return ''
s = ''
for i in range(5, 0, -4):
for ii in range(0, i, 4):
for iii in range(3):
s += x(i)
s += x(ii)
s += x(iii)
print(s)

End of Part A

Part A, Page 4 / 4
University Number:

THE UNIVERSITY OF HONG KONG

FACULTY OF ENGINEERING
DEPARTMENT OF COMPUTER SCIENCE

Part B
ENGG1330 Computer Programming I

Date: 9 December 2022


Time: 2:30 pm - 4:30 pm

This examination consists of three parts: A to C. This is Part B.


Answer ALL questions in the space provided.
Use of a calculator is not allowed.
Write your university number in the space provided at the top of every odd-numbered page.

Your Mark
Max. Mark
(examiner use only)

32

Part B, Page 1 / 4
Shiritori is a game in which players have to think of a word beginning with the final letter of the previous
word. For example if the first player starts with the word ball, the second player can say any word that
starts with the letter l, for example laugh. The first player will then have to say a word that starts with h.
Words cannot be repeated. Consider the following example chain of words.
ball -> laugh -> help -> particular -> round -> distant -> …
Note that the last letter of the previous word is the same as the first letter of a current word. In this example,
all words are in lower case. However, the code that you are going to write should not make this assumption.
For example, ball -> Laugh is a valid match as well. In the following, we are going to write functions
related to generating such a word chain.

B1 (8 marks)
Your first task is to read a dictionary of words in the file words.txt. This file has all words of the English
language, one per line. Below you can find the first few lines of the file.
felt
perhaps
pick
sudden
count
square
reason
length
represent

Complete the following function read_words_from_file such that a list with all words that can be found
in the file words.txt is returned by the function.

def read_words_from_file(file_name):
words = []
# Your code here. Use at most 6 lines of code

return words
words = read_words_from_file('words.txt')

Part B, Page 2 / 4
University Number:

B2 (8 marks)
Your second task is to find the longest match x (or at least as long as any other match) for a given word w
and return it. A word x matches w if x starts with the same character (case insensitive) that w ends with. For
example, given w=ball we may find that x=laugh matches. Note that there might be more than one match
x for a word w. For example, there could be another word in the dictionary that also starts with l, e.g., long.
You should return the longest x in terms of number of characters, i.e., the longest word. Note that there is
one additional requirement for a match: A match cannot appear in the words_already_used set. If no
match can be found you should return None.

def get_longest_match(words, w, words_already_used):


x = ''
# Your code here. Use at most 8 lines of code

return x

B3 (8 marks)
Next, make use of the two functions that you have written above. Start with the word at index 100 and
output it. Next find a novel match using the function written in B2 and output it. Repeat the process until no
more novel matches can be found. Here, a novel match is a match that has not been found before. You
should use and maintain the words_already_used set data structure.

# Your code here. Use at most 10 lines of code

Part B, Page 3 / 4
B4 (8 marks)
Finally, write a function similar to get_longest_match but this time we call it get_best_match. This
function will not return the longest match, instead it will score the matches by the number of unique vowels
(a, e, i, o, u) that are found in the match. It will return the match x with the highest number of unique
vowels (or as high as any other match). You may declare your own helper function in the space provided.

def get_best_match(words, w, words_already_used):


x = ''
# Your code here.
# Use at most 10 lines of code (including your helper function above)

return x

End of Part B

Part B, Page 4 / 4
University Number:

THE UNIVERSITY OF HONG KONG

FACULTY OF ENGINEERING
DEPARTMENT OF COMPUTER SCIENCE

Part C
ENGG1330 Computer Programming I

Date: 9 December 2022


Time: 2:30 pm - 4:30 pm

This examination consists of three parts: A to C. This is Part C.


Answer ALL questions in the space provided.
Use of a calculator is not allowed.
Write your university number in the space provided at the top of every odd-numbered page.

Your Mark
Max. Mark
(examiner use only)

26

Part C, Page 1 / 3
Given a string s, find the longest (in terms of number of characters) amazing substring x. An amazing string
is defined as a string where the right half is the reverse of the left half. For example ‘abcddcba’ is an
amazing string and ‘cabac’ is not an amazing string because it cannot be split in equal halves.

Write a function get_longest_amazing_substring(s) which will return the longest (or at least as
long as any other) amazing substring of s. If no such string can be found you should return None. You may
assume that the provided string s is a sequence of lower case characters.

For example consider the string

s=’nvkdsnvuijaerveoussuoevrecjkdslndsjkfndslfknerveoussuoevren’

Your function get_longest_amazing_substring(s) should return

’nerveoussuoevren’.

You may declare your own helper functions in the space provided.

Part C, Page 2 / 3
University Number:

def get_longest_amazing_substring(s):
x = ''
# Your code here.
# Use at most 14 lines of code (including your helper function above)

return x

End of Part C
Part C, Page 3 / 3

You might also like