USYD悉尼大学INFO1110 详细作业解析Week3 all quizzes

Week 3 all the quizzes

第三周所有的quiz,详细解释,答案供参考。详细


You should take it easy. 详细的解说等我闲一点了再添加上去,【官方解释】的是老师给的解释,大部分都是能看明白的。



Preface

提示:这里可以添加本文要记录的大概内容:
例如:随着人工智能的不断发展,机器学习这门技术也越来越重要,很多人都开启了学习机器学习,本文就介绍了机器学习的基础内容。


Lecture: Control flow

Expressions and control flow quiz

question 1

The following code uses int casting. What is the difference between the value calculated with and without the int casting?

print(str(int(float("3.14"))+int(float("5.6"))))

answer : 0.74
讲解: 在代码块里运行的时候,输出结果是8。但是那是错的,正确理解是取浮点数小数点后面的数字相加,就是结果。(待更新更好的解释)

question 2

Do the following two code segments have equivalent outcome (control flow)?

ticket = 240
if speed > 0:
	if speed < 40:
		ticket = 0
	else:
		if speed > 100:
			pass
		else if speed <= 100:
			ticket = 120
		else:
			pass
else:
	pass
ticket = 0 
if speed < 40:
	ticket = 0
else if speed <= 100:
	ticket = 120
if speed > 100:
	ticket = 240

answer :

  • True
  • False

讲解: 显然一个大于另一个! 但请注意覆盖范。第一个嵌套的if是如何解决控制流问题的错误示例。 每个间隔都可以连续测试,而不必测试是否依赖于先前条件的结果。例如,如果速度大于100,则不应首先检查速度是否小于40。 这些是非依赖性标准!【官方解释】

question 3

Do the following two code segments have equivalent outcome (control flow)?

ticket = 0 
if speed < 40:
	ticket = 0
else if speed <= 100:
	ticket = 120
if speed > 100:
	ticket = 240
ticket = 240
if speed < 40:
	ticket = 0
else if speed <= 100:
	ticket = 120

answer:

  • True
  • False

解释: 根据速度值,它们将产生相同的值。请注意,一个行比另一行少。 程序员已仔细选择默认值,使其处于三个可能范围之一中。 通过假设将遵循一个,这避免了控制流的重复。通过在此示例覆盖的三个范围之一内选择默认值,可以完成相同的操作。【官方解释】

question 4

What is the number of unique code paths in this code?

In other words, if we were to run this program many times, how many possible execution configurations could we have? (Hint: draw the control flow diagram and count the paths)

name = input('What is your name ')
attribute = input('Which is your favourite colour among these [Red / Green / Blue] ')

attr_str = 'Courageous'
if attribute == 'Green':
	attr_str = 'Adventurous'
elif attribute == 'Blue':
	attr_str = 'Tenacious'

sibling = input('Do you have siblings [ Yes / No ] ')
sibling_str = 'Lone wolf'
if sibling == 'Yes':
	oldest = input('Are you the oldest? [ Yes / No ] ')
	if oldest == 'Yes':
		sibling_str = 'Leader'
	else:
		youngest = input('Are you the youngest? [ Yes / No ] ')
		if youngest == 'Yes':
			sibling_str = 'Provocateur'
		else:
			sibling_str = 'Conqueror'

print("You are: ")
print(name + " the " + attr_str + " " + sibling_str)

correct answer : 12
讲解:

  • 3种可能的颜色选择之一
  • 是否为同级2个选择中的1个
    • 内有一个兄弟姐妹
    • 最老,中,最年轻的3种选择之一

可以选择3种颜色且没有兄弟姐妹-> 3 * 1 = 3条代码路径
可以选择任意3种颜色和任意3种同级-> 3 * 3 = 9个代码路径
共有12条代码路径【官方解释】


While loops quiz

question 1

Will the following code ever end?

i = 0
while i > 0:
    print(i)
    i = i + 1

answer :

  • True
  • False

解释: 循环继续的条件必须为True,在这种情况下,始终为False!【官方解释】

question 2

Does this code print numbers from 0 to 10 inclusive?

i = 10
while i >= 0:
    x = 10 - i
    print(x)
    i = i - 1

answer:

  • True
  • False

解释: 是的,只是顺序相反!【官方解释】

question 3

How many iterations would be executed for the following loop?

x = 10
while x < 20:
    x = x + 1

answer : 10
解释:
您可以通过观察计数器变量的三项来以这种形式计算迭代次数:
1.起始条件,
2.它如何变化,
3.结束条件
有时它并不那么琐碎,可以在许多地方进行更改!【官方回答】

question 4

How many iterations would be executed for the following while loop?

x = 5
while x <= 15:
    print(x)
    x = x + 2

answer: 6
解释: 注意循环不变式和循环计数器,办公桌检查在这里很有帮助


Arrays and lists part one quiz

question 1(单选)

Which best defines an array?

  • It’s like this:
list = [ 'a','1','2', ]
  • A range of memory,divide into equal sized parts
  • A collection of objects
  • A collection of objects which are internally by an independent define manner(python3).

解释: 数组是一个通用概念,没有附加到Python或列表中。列表与数组不同。数组与列表不同。【官方解释】

question 2(多选)

What is the motivation to use arrays?

  • Avoid repetitive code by way of defining variables
    var1,var2,var3.etc.
  • When you do not know the datatype,use a list.
  • Group related data together
  • Cool to use
  • It is the only way to express arithmetic addition
  • Allow reading and writing a memory based on critical determined at runtiome.
    解释: 数组为程序员提供了多功能性。大型结构化数据可以利用数组,数组是一种存储相关数据的简单方法(无需求助于对象)假定索引是数字,则可以在运行时选择它。 例如 第一个元素索引= 0,例如 如果我们有一个升序排序的数组,并且用户想要第k个最小值,则可以使用index = k + 1。【官方解释】

question 3(判断)

Is the first element of the array located at index 1?

  • True
  • False

解释: 数组始终从索引0开始。【官方解释】

question4(单选)

Suppose there is an array called names, and there are 5 elements. What would happen if we accessed names[12] ?
[ For argument sake, suppose this was done in Python3 ]

  • Nothing
  • There is an attempt to access,but zero is return.
  • None is return
  • An error occurs
  • The original programer is send an email

解释: 会有错误。 不同的编程语言以不同的方式处理此问题。 通常,如果您请求了固定大小的内存,并且开始访问不在该区域内的区域,计算机将停止您的访问。


总结

猿生不能只有 Ctrl + C 和 Ctrl + V,还要有 Ctrl + D

  • 2
    点赞
  • 1
    收藏
  • 打赏
    打赏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:点我我会动 设计师:我叫白小胖 返回首页
评论 5

打赏作者

不二程序猿

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值