# Python exam-2022-online「The University of Melbourne」

AI悦创原创
• Python 练习
• Python 练习

## # Question1

Construct a single Python expression which evaluates to the following values, and incorporates the specified operations in each case (executed in any order).

(a) Output value: 'grin'

Required operations:

1. string slicing

1. string indexing

(b) Output value: True

Required operations:

1. range() function

2. in

(c) Output value: 2

Required operations:

1. (float) division

2. integer division

(d) Output value: 0

Required operations:

1. dictionary lookup

1. .pop() method

2. list indexing

(e) Output value: 'ace'

Required operations:

1. items method

2. list indexing

3. tuple indexing

## # Question 2

The following function is intended to calculate the similarity between two names by calculating what proportion of "n-grams" (substrings of n characters) from the first name can be found in the second name, and averaging across the resulting values for each value of n from 2 up to maxn (by default, 3). If the first name has fewer characters than any tested value of n, the function should return 0.0.

Example function calls are:

``````>>> name_similarity('toto', 'totoro')
1.0
>>> name_similarity('tim', 'totoro')
0.0
>>> name_similarity('totoro', 'toto')
0.55
>>> name_similarity('', 'totoro')
0.0
``````

All the required lines of the function are available, out of order, in the editor panel. Drag each line across to the solution area to the right, put them in the correct order, and introduce appropriate indentation. Note that ALL lines must be used in your solution.

## # Question3

``````SUIT = 1
BLACK = 0
RED = 1

SUIT_COLOURS = {'S': BLACK,
'H': RED,
'D': RED,
'C': BLACK}

def same_colour(cards):
for i in range(len(cards)):
if i == 0:
colour = SUIT_COLOURS[cards[i][SUIT]]
elif SUIT_COLOURS[cards[i][SUIT]] != colour:
return False
return True
``````

## # Question 4

Sandy has enrolled in a Bachelor of Magical Arts. In order to graduate, they must complete the subject WIZ90001 Advanced Wizardry. However, this subject has a number of prerequisites, each of which must be completed before they can take WIZ90001. Each of those subjects in turn may have its own prerequisites, and so on.

Given a particular list of prerequisite requirements, the function `wiz_study_length(prereq_list, final='WIZ90001')` is intended to calculate the minimum number of semesters it will take Sandy to graduate. The arguments are:

• `prereq_list` , a list of `(subject, prerequisite)` tuples specifying that prerequisite must be completed before enrolling in subject. Note that a subject may have more than one prerequisite.

Prereq_list，一个(subject，prerequisite)元组列表，指定在注册 subject 之前必须完成先决条件。注意，一个主题可能有多个先决条件。

• final, the target subject to complete, with a default value of 'WIZ90001'.

final 是要完成的目标主题，默认值为'WIZ90001'。

Any subject that appears as a prerequisite in a prereq_list tuple but does not appear as a subject can be assumed to have no prerequisites. Furthermore, we assume that Sandy has capacity to take as many subjects as required each semester, as long as they have completed their prerequisites.

For example:

``````>>> wiz_study_length([('WIZ90001', 'WIZ40027'), ('WIZ90001', 'WIZ20003')])
2
``````

Here, both WIZ40027 and WIZ20003 are prerequisites of WIZ90001, and so must be completed before studying WIZ90001. Neither of these two subjects have any prerequisites. Therefore it will take two semesters of study for Sandy to graduate

``````>>> wiz_study_length([('WIZ90001', 'WIZ40027'), ('WIZ90001', 'WIZ20003'),
('WIZ40027', 'WIZ30015'), ('WIZ20003', 'WIZ20001'),
('WIZ20003', 'WIZ10015'), ('WIZ20001', 'WIZ10001')])
4
``````

In this case, it will take 4 semesters for Sandy graduate, due to the prerequisite chain: WIZ10001 -> WIZ20001 -> WIZ20003 -> WIZ90001.

``````•	from collections import defaultdict
•	semesters = 0; cur_prereqs = [final]
•	prereqs = defaultdict(list)
•	for subject in prereq_list:
•	cur_prereqs = new_prereqs; semesters += 1
•	prereqs[subject[0]].append(subject[1])
•	def wiz_study_length(prereq_list, final='WIZ90001'):
•	for subject in cur_prereqs:
•	new_prereqs += prereqs[subject]
•	if subject in prereqs:
•	while cur_prereqs:
•	new_prereqs = []
•	return semesters
``````

## # Question 5

A school of minnows swims in the sea. Sharks eat minnows, according to the following strict rules. Sharks swim, one at a time, from left to right and eat each minnow they encounter, starting at the height of the first minnow that remains uneaten. Each time a shark eats a minnow it gets heavier and sinks one metre lower in the water. Once a shark reaches the sea floor, it is no longer able to eat minnows.

For example, minnows are located at the following heights (in metres) above the sea floor (e.g. a height of 3 indicates that the minnow is 3m above the sea floor) in sequence, left to right:

`[4, 3, 1, 2, 4]`

The first shark will start at height 4 and eat the minnow at that height (element 0), then sink to height 3 where it will eat the second minnow (element 1), and sink to height 2. It will pass above the third minnow (because it is at a lower height), before eating the fourth minnow (element 3) at height 2, and passing under the final minnow (element 4). At this point, the following minnows will remain:

`[None, None, 1, None, 4]`

The second shark will start at height 1 and eat the minnow at that height (element 2), then sink to the sea floor (height 0) and eat no further minnows. This will result in the following minnows:

`[None, None, None, None, 4]`

The third shark will start at height 4 and eat the minnow at that height (element 4), after which no minnows will remain.

The function sharks_minnows(minnows, sharks) is intended to determine whether a given number of sharks will be sufficient to eat all of the minnows in the sea, positioned based on minnows. The function takes the following parameters:

• minnows, a list of positive integers indicating the height of each minnow, as in the example above.

Minnows，一个正整数列表，表示每条鲦鱼的高度，如上例所示。

• sharks, a positive integer number of sharks.

The function should return True if the specified number of sharks is sufficient to eat all the minnows, and False otherwise.

``````def sharks_minnows(minnows, sharks):
shark_count = 0
minnow_count = len(minnows)
for i in range(minnow_count):
curr_shark_height = minnows[i]
if curr_shark_height is not None:
minnows[i] = None
for j in range(i + 1, minnow_count):
if minnows[j] == curr_shark_height:
minnows[j] = None
curr_shark_height -= 1
shark_count += 1
return shark_count <= sharks
``````

The provided code is imperfect, in that it sometimes returns `True` when it should return `False`, and sometimes returns `False` when it should return `True`.

(a) Provide an example of a function call where the provided code will correctly return `True` (i.e. a True Positive)

(b) Provide an example of a function call where the provided code will correctly return `False` (i.e. a True Negative)

(c) Provide an example of a function call where the provided code will incorrectly return `True` (i.e. a False Positive)

(d) Provide an example of a function call where the provided code will incorrectly return `False` (i.e. a False Negative)

## # Question 6

Construct a single Python expression which evaluates to the following values, and incorporates the specified operations in each case (executed in any order).

(a) Output value: 'eeezy'

Required operations:

1. string indexing

2. `*`

(b) Output value: True

Required operations:

1. in

2. `range()`

(c) Output value: '100% pythonic'

Required operations:

1. `f-string`

2. `/`

(d) Output value: 'comp10001'

Required operations:

1. tuple indexing

2. dictionary lookup

3. `.lower()`

(e) Output value: '42'

Required operations:

1. list indexing

2. `.keys()`

3. string slicing

## # Question 7

The following function is meant to take a lowercase string as input and return a reordered version of that string in which all of the vowels appear first (in the order in which they appear in the original string), followed by all of the consonants (again, in the order in which they appear in the original string), discarding any non-alphabetic characters in the process.

For example:

``````>>> reorder('stay at home')
'aaoestythm'
>>> reorder('the cat in the hat')
'eaieathctnthht'
>>> reorder('rhythm')
'rhythm'
``````

However, there are several errors in the given function definition.

Identify exactly three (3) errors and specify: (a) the line number where the error occurs; (b) the type of error, as syntax, run-time, or logic; and (c) how you would fix each error, in the form of the corrected (single) line of code.

``````def reorder(my_string):               #  1
v_list = list(aeiou)              #  2
vowels = []                       #  3
consonants = []                   #  4
for c in my_string:               #  5
if c not in v_list:           #  6
vowels.append(c)          #  7
else c:isalpha():             #  8
consonants.append(c)      #  9
return join(vowels + consonants)  # 10
``````

AI悦创·编程一对一

AI悦创·推出辅导班啦，包括「Python 语言辅导班、C++ 辅导班、java 辅导班、算法/数据结构辅导班、少儿编程、pygame 游戏开发」，全部都是一对一教学：一对一辅导 + 一对一答疑 + 布置作业 + 项目实践等。当然，还有线下线上摄影课程、Photoshop、Premiere 一对一教学、QQ、微信在线，随时响应！微信：Jiabcdefh

C++ 信息奥赛题解，长期更新！长期招收一对一中小学信息奥赛集训，莆田、厦门地区有机会线下上门，其他地区线上。微信：Jiabcdefh

• 0
• 0
• 0
• 0
• 0
• 0

• 按正序
• 按倒序
• 按热度