2024-11-25
Max. 100 points
Name:
Task | Max. | Achieved |
---|---|---|
1 | 20 | |
2 | 30 | |
3 | 25 | |
4 | 25 | |
Sum | 100 |
Statement | True | False |
---|---|---|
Python lists are mutable. | ||
The sole purpose of indentation in Python is to make the code easier to read. | ||
The "ifmain" pattern allows a module to behave differently when it is executed vs. imported. | ||
Python code can be edited with any regular text editor. No special editor is required. | ||
Python strings are mutable. | ||
while loops are used for repeated execution
as long as an expression is true. | ||
Python modules provide namespaces. | ||
sys.path is used to find Python modules when
importing them. |
l = ['a', 'b', 'c', ]
print(l.append('d'))
def sum(a, b):
return a + b + c
sum(4, 2, 3)
def area(length, width):
return length * width
print(area(5, 2))
l = [4, 3, 2]
print(l.sort())
import math
print(circumference(0.5 / math.pi))
print({1, 2, 10, 2, 1, 5})
filter_by_divisor(l, divisor)
in Python that takes a list of integers and an integer
divisor
.
The function must return a list containing only the elements of the
original list that can be divided by divisor
without a
remainder. For example
filter_by_divisor([], 3) # -> []
filter_by_divisor([9], 2) # -> []
filter_by_divisor([9], 3) # -> [9]
filter_by_divisor([9, 2, 3], 3) # -> [9, 3]
filter_by_divisor([3, 5, -4, 2], 2) # -> [-4, 2]
filter_by_divisor([3, 5, -4, 2], 5) # -> [5]
(25 points)
def filter_by_divisor(l: List[int], divisor: int) -> List[int]:
"""Return a list containing elements of `l` divisible by `divisor`."""
result = []
for e in l:
if e % divisor == 0:
result.append(e)
return result
cumsum(l)
in Python that takes a list of integers and returns
a list containing the cumulative sum of the numbers. For example
cumsum([]) # -> []
cumsum([9]) # -> [9]
cumsum([9, 2, 3]) # -> [9, 9+2, 9+2+3] -> [9, 11, 14]
cumsum([3, 5, -4, 2]) # -> [3, 3+5, 3+5+(-4), 3+5+(-4)+2] -> [3, 8, 4, 6]
Don't forget to deal with empty lists. (25 points)
def cumsum(l: List[int]) -> List[int]:
"""Return the cumulative sum of the given list."""
if not l: return []
result = l[:1]
for e in l[1:]:
result.append(result[-1] + e)
return result