Python exam-2019s2「The University of Melbourne」

AI悦创原创
  • Python 练习
  • Python 练习
大约 6 分钟

答案获取须知!

关注公众号:AI悦创,添加我好友。微信:Jiabcdefh

注意:关注公众号,添加好友,备注来意!以此获取答案密码。

公众号:AI悦创【二维码】

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.

阅读时间:15分钟。写作时间:2小时。

This paper has 19 pages including this cover page.

包括封面在内,这篇论文共有19页。

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.

考试共有10道题,共计120分,占该科目总成绩的50%。

  • 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

第1部分:算法思维

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.

在项目1中,您考虑了优先投票,在这种投票中,有效的投票要求投票人按他们喜欢的顺序列出所有候选人。

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

下面的函数排序(候选人)旨在递归地为给定的n个候选人集合生成所有可能的有效选票。

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.

然而,在给定的函数定义中有几个错误。准确识别三(3)个错误并指定:(a)发生错误的行号;(b)错误的类型,如语法、逻辑或运行时;以及(c)如何以修正后的代码的形式修正每个错误。

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悦创,有更多更好玩的等你发现!

公众号:AI悦创【二维码】

AI悦创·编程一对一

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

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

方法一:QQopen in new window

方法二:微信:Jiabcdefh

上次编辑于:
贡献者: AndersonHJB

你认为这篇文章怎么样?

  • 0
  • 0
  • 0
  • 0
  • 0
  • 0
评论
  • 按正序
  • 按倒序
  • 按热度