Python exam-2019s2「The University of Melbourne」

AI悦创原创Python 练习Python 练习大约 6 分钟...约 1935 字





The University of Melbourne

School of Computing and Information Systems

Final Examination, Semester 2, 2019

COMP10001 Foundations of Computing

Reading Time: 15 minutes. Writing Time: 2 hours.


This paper has 19 pages including this cover page.


Instructions to Invigilators:


Students must write all of their answers on this examination paper. Students may not remove any part of the examination paper from the examination room.

学生必须把他们所有的答案都写在这张试卷上。 考生不得将试卷的任何部分带出考场。

Instructions to Students:


There are 10 questions in the exam worth a total of 120 marks, making up 50% of the total assessment for the subject.


  • All questions should be answered by writing a brief response or explanation in the lined spaces provided on the examination paper.

  • 所有的问题都应该在试卷上划线的地方写一个简短的回答或解释。

  • It is not a requirement that all the lined spaces be completely filled; answers should be kept concise. Excessively long answers or irrelevant information may be penalised.

  • 并不是要求所有的内衬空间都被完全填满;回答应该简明扼要。过于冗长的回答或不相关的信息可能会被扣分。

  • Only material written in the lined spaces provided will be marked.

  • 只有在划线区域内填写的材料才会被标记。

  • The reverse side of any page may be used for notes or draft answers.

  • 任何一页的背面都可以用作注释或答案草稿。

  • Your writing should be clear; illegible answers will not be marked.

  • 你的写作应该清晰;难以辨认的答案将不会被标记。

  • Extra space is provided at the end of the paper for overflow answers.

  • 额外的空间提供在论文的结尾,为溢出的答案。

  • Please indicate in the question you are answering if you use the extra space.

  • 如果你使用了多余的空间,请在回答的问题中注明。

  • Your answers should be based on Python 3 (the version that Grok uses), and can use any of the standard Python libraries.

  • 你的答案应该基于Python 3 (Grok使用的版本),并且可以使用任何标准的Python库。

Authorised Materials: No materials are authorised.


Calculators: Calculators are not permitted.


Library: This paper may be held by the Baillieu Library.

图书馆:本论文可能由 Baillieu 图书馆收藏。

Examiners’ use only

Student Number

Part 1: Algorithmic Thinking


Question 1

Evaluate the following expressions, and provide the output in each case.


(a) 'snout'[2:]

(b) sorted(['fog', 'cog', 'dog'])

(c) ('now'[-1] + 'south'[-1] + 'hurry'[-1])

(d) sorted({'owls': 'good', 'more owls': 'not so good'}.values())[1]

(e) 'nic' not in sorted('cinema')

(f) [i%2 for i in range(0, 10, 2)]

Question 2

Rewrite the following function, replacing the for loop with a while loop, but preserving the re- mainder of the original code structure:

重写以下函数,用' while '循环替换' for '循环,但保留原始代码结构的其余部分:

def all_vowels(word):
    vowels = 'aeiou'
    seen = ''
    for c in word:
        if c in vowels:
            seen += c
    return sorted(seen) == list(vowels)

Question 3

In project 1, you considered preferential voting, in which a valid vote required a voter to list all candidates once in their preferred order.


The following function orderings(candidates) is intended to recursively generate all possible valid votes for a given set of n candidates.


def orderings(candidates):
    if not candidates:
        return [candidates]
    vote == []
    for i in range(len(candidates)):
        current = candidates(i)
        remainining = candidates[i] + candidates[i+1:]
        for o in orderings(remaining)
            vote.append([candidates] + o)
    return vote

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, logic or runtime; and (c) how you would fix each error, in the form of corrected code.


Question 4

To “obfuscate” text means to make it hard to read. Obfuscating text by substituting or modifying certain characters, can make it more difficult for a casual reader to understand, while still leaving it readable by someone familiar with the rules of modification.


The following function is intended to obfuscate a (lowercase) string of text according to the fol- lowing rules:


  1. First, all consecutive duplicate letters are replaced with a single letter; for example, ’hello’ becomes ’helo’ and ’mississippi’ becomes ’misisipi’.

首先,将所有连续重复的字母替换为单个字母;例如,' hello '变成' helo ', ' mississippi '变成' misisipi '。

  1. Next, some characters are replaced with numbers/symbols, according to the substitution dic- tionary below; e.g., ’a’ becomes ’@’ and ’e’ becomes ’3’.
  1. 接下来,根据下面的替换字典,将一些字符替换为数字/符号;例如,' a '变成' @ ',' e '变成' 3 '。
  1. Finally, given the updated string after applying rules 1 and 2, convert each character at an even- numbered index (i.e., index positions 0, 2, 4, etc) in this string to uppercase; e.g., the string ’doubt’ becomes ’DoUbT’.

3.最后,给出应用规则1和规则2后更新的字符串,将该字符串中偶数索引(即索引位置0、2、4等)的每个字符转换为大写;例如,字符串' doubt '变成' doubt '。

For example:

>>> obfuscate_text('keeping secrets is wise')
'K3P!Ng $3cR3T$ !$ W!$3'

Assume you are given the dictionary subs as follows:


subs = {
    'a': '@',
    's': '$',
    'i': '!',
    'e': '3',
    'l': '1'

As presented, the lines of the function are out of order. Put the line numbers in the correct order and introduce appropriate indentation (indent the line numbers using the columns in the answer table provided to show how the corresponding lines would be indented in your code).


1 obs_text = ''
2 i=0
3 if short_text[i] in subs:
4 while i < len(short_text):
5 else:
6 obs_text += short_text[i]
7 obs_text += short_text[i].upper()
8 obs_text += subs[short_text[i]]
9 elif i%2 == 0:
10 i+=1
11 return obs_text
12 def obfuscate_text(text):
13 prev_char = ''
14 if c == prev_char:
15 short_text = ''
16 continue
17 short_text += c
18 for c in text:
19 prev_char = c

Part 2: Constructing Programs




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

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

方法一:QQopen in new window


贡献者: AndersonHJB,AI悦创
  • 0
  • 0
  • 0
  • 0
  • 0
  • 0
  • 按正序
  • 按倒序
  • 按热度