# CSC 108H5 F 2018 Midterm Test

AI悦创原创麦马大学mcmasterMcMaster University麦马大学mcmasterMcMaster University大约 5 分钟...约 1443 字

This midterm consists of 5 questions on 9 pages (including this one). When you receive the signal to start, please make sure that your copy is complete.

• You may assume all relevant import statements have been performed.
• No error checking is required: assume all user input and all argument values are valid.
• If you use any space for rough work, indicate clearly what you want marked.
• You may use a pencil; however, work written in pencil may not be considered for remarking.

## # Question 1.

Assume each of the pieces of code below is being entered into the Python console. In each space, write what would be displayed on the console, if anything. If an error would be displayed to the console, write ERROR.

Each subquestion is independent of the others.

### # Part (a)

``````>>> x = 5
>>> y = x
>>> x = 2
>>> y
``````

### # Part (b)

``````>>> x = 4
>>> y = 2
>>> z = x/y
>>> y = x+y * z
>>> x = 5 * z // 3
>>> y
>>> out1?
>>> x
>>> out2?
``````

### # Part (c)

Assume the following two functions have been declared.

``````def f1(x, y):
x = y + 2
z = y * x
return 2 * f2(x, z)

def f2(y, x):
x = x - y
z = x - y
return x - (y + z)
``````
``````>>> z = 1
>>> x = 2
>>> f1(z, x)
``````

### # Part (d)

``````>>> s = "EZ Marks."
>>> t = s[1:4]
>>> t
>>> out1?
>>> t[2]
>>> out2?
``````

### # Part (e)

``````>>> L = [5, 8, 3, 0, 1, 2, 6, 24]
>>> for z in L:
if z%3 != 0 and z%2 == 0:
print(z)
``````

### # Part (f)

``````>>> L = [[5, 9, 1, 1, 2], [7, 7], [], [1]]
>>> for i in range(len(L[0])):
s=0
for n in L[i]:
s += n
print(s)
``````

## # Question 2.

Recall the `is_a_number` function from Assignment 1. The function takes in a string and returns True if and only if that string represents a positive number or positive decimal number (see some examples in the docstring below). Implement is a number without the use of loops, or `try/exceptions`. If your code uses either of these constructs, you will receive 0. Focus on passing the examples in the docstring. Do NOT worry about cases like “ 1.0 ”, “4.0e7”, etc.

``````def is_a_number(s:str)-> bool:
"""
Returns True if and only if s is a string representing a positive number.
Examples:
>>> is_a_number("1")
True
>>> is_a_number("One")
False
>>> is_a_number("-3")
False
>>> is_a_number("3.")
True
>>> is_a_number("14.61")
True
>>> is_a_number("3.1.2")
False
"""
``````

``````def is_a_number(s: str) -> bool:
# Check if the entire string consists of digits, in which case it's a positive integer.
if s.isdigit():
return True

# Check if the string has exactly one period, and both parts separated by the period are digits.
if s.count('.') == 1:
left, right = s.split('.')
return left.isdigit() and right.isdigit()

return False

# Test cases
print(is_a_number("1"))       # True
print(is_a_number("One"))     # False
print(is_a_number("-3"))      # False
print(is_a_number("3."))      # True
print(is_a_number("14.61"))   # True
print(is_a_number("3.1.2"))   # False
``````

## # Question 3.

Consider the function below. Reimplement (rewrite) it using only a single if statement. To get full grades there should not be any else or elif clauses, only a single if clause. The behaviour of your new function should be equivalent to the original function in the sense that given the same input, the two functions (original and new) will produce the same output.

``````def my_fun(n: int, m: int)-> int:
if n > 5 and m > 0:
if n > 0:
return 1
else:
return 2
elif m <= 0 or n < 6:
return 3
else:
return 4
``````

## # Question 4.

Consider the function below. Reimplement (rewrite) it using a while loop instead of a for loop. The behaviour of your new function should be equivalent to the original function in the sense that given the same input, the two functions (original and new) will produce the same output.

``````def my_fun(s: str)-> int:
n=0
for char in s:
if not char in "aeiouAEIOU":
n += 1
return n
``````

AI悦创·编程一对一

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

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

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

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