题目
Programming Programming In-class Test- Part 1 Programming Programming In-class Test- Part 1
多项选择题
This question is worth 3 marks. Don't guess, you will be penalised 3/4 of a mark for an incorrect answer. You won't be penalised for leaving your answer blank.Consider the following Python code:def func_two(dict_one): x = None y = None for key, value in dict_one.items(): if x: if value > x: x = value y = key else: x = value y = key return y def func_one(input_str): dict_one = {} for i in input_str: if i in dict_one: dict_one[i] += 1 else: dict_one[i] = 1 res = func_two(dict_one) return res if __name__ == "__main__": print (func_one("python programming")) print (func_one("Hello World")) What does the code output?
选项
A.p
l
B.m
l
C.2
3
D.6 11
5 5
E.None of the above
查看解析
标准答案
Please login to view
思路分析
We start by restating what the code does and what outputs are being asked to determine.
- The code defines func_two(dict_one) which iterates over dict_one.items() with variables key and value. It uses a variable x initialized to None and a variable y initialized to None. The logic inside the loop is: if x is truthy, then if value > x, set x = value and y = key; else set x = value and y = key. If x is not truthy (i.e., None or 0), it goes to the else and sets x = value and y = key. After the loop, func_two returns y.
- func_one(input_str) builds a dictionary dict_one countin......Login to view full explanation登录即可查看完整答案
我们收录了全球超50000道考试原题与详细解析,现在登录,立即获得答案。
类似问题
Consider the following code segment: fruit = {"Apple": "Green", "Banana": "Yellow"} fruit["Apple"] = "Red" After it executes, what is the value of fruit?
The index of the first item in a dictionary is 0.
What is the number of the first index in a dictionary?
Which method would you use to get all the elements in a dictionary returned as a list of tuples (key, value)?
更多留学生实用工具
希望你的学习变得更简单
加入我们,立即解锁 海量真题 与 独家解析,让复习快人一步!