2021-12-09
Max. 22 points
Name:
Task | Max. | Achieved |
---|---|---|
1 | 2 | |
2 | 2 | |
3 | 8 | |
4 | 5 | |
5 | 5 | |
Sum | 22 |
Statement | True | False |
---|---|---|
In Python, functions, modules, classes and loops create scope. | ||
Programs written in C tend to run faster than programs written in Python. | ||
Any for loop can be re-written as
while loop. | ||
C is still one of the most commonly used programming languages. |
if __name__ == "__main__":
import sys
print(sys.exit(0))
def decode(text):
return encode(text)
print(decode('apt-build moo'))
def grmpfzotz():
print('Hooray')
s = "Everyone wants to become a great programmer."
print(s[:3], s[-1])
sum_to(num)
that takes an integer
num >= 0
as sole
argument. The function should return the sum of all positive integers
<= num
. In the function, assert that the input is
≥ 0 by using assert
.def sum_to(num):
"""
Sum all numbers from 0 up to a given integer.
Assert that no negative numbers are entered.
"""
assert num >= 0, "argument must be >= 0"
return int(num / 2.0 * (num + 1))
sumproduct(first iterable, second iterable)
in Python that takes two sequences of numbers with equal length and returns
the sum of the product of the numbers.sumproduct((1, 3, 4), (9, 2, 3)) ⇒ 1*9 + 3*2 + 4*3 = 27
;
sumproduct([3, 5], [2, 4]) ⇒ 26]
.
Add a proper docstring to receive full points.def sumproduct(first, second):
"""
Return the sum of the products of `zip(first, second)`.
>>> sumproduct((1, 3, 4), (9, 2, 3))
27
"""
sum = 0
for i, j in zip(first, second):
sum += i * j
return sum