2022-12-07
Max. 32 points
Name:
Task | Max. | Achieved |
---|---|---|
1 | 6 | |
2 | 10 | |
3 | 6 | |
4 | 4 | |
5 | 6 | |
Sum | 32 |
Statement | True | False |
---|---|---|
In Python, any block of code creates scope. | ||
In Python it is recommended to use two spaces for indentation. | ||
The "ifmain" pattern allows a module to behave differently when it is executed vs. imported. | ||
Python offers a "do-while" loop. | ||
Python code written with micro can be edited
with another editor afterwards. | ||
A Python sequence type can be indexed, sliced and iterated. | ||
Strings in Python are immutable. | ||
Python lists are mutable. | ||
Python tuples are mutable. | ||
Python dictionaries are mutable. | ||
Doctests are a perfect way of testing your code. | ||
Comments are more powerful than docstrings. |
def average(a, b, c):
return (a + b + c) / 3.0
average(4, 2, 3)
def area(length, width):
return length * width
print(area(5, 2))
s = 'Hello World!'
s.upper()
print(s)
l = [4, 2, 3, 1]
print(l.sort())
def fun(a, b, c=5):
return a + b + c
print(fun(10))
needle
should be searched in all of the files in the list of relative
filenames
. The program should count the non-overlapping
occurrences of needle
in all the given files and print it
to standard output. The comparison must not be case sensitive. Use the
string
class' S.count(sub[, start[, end]]) -> int
method which returns the number of non-overlapping occurrences of substring
sub in string S[start:end].
(6 points)
#!/usr/bin/env python
filenames = input('Files: ')
needle = input('Needle: ').lower()
matches = 0
filenames = filenames.split(',') # create list of filenames
for filename in filenames:
with open(filename) as f:
content = f.read().lower()
matches += content.count(needle)
print(matches)
for
loop as while
loop.
(4 points)
for i in range(0, 10, 3):
print(f'{i} * {i} = {i * i}')
i = 0
while i < 10:
print(f'{i} * {i} = {i * i}')
i += 3
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 and a doctest 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