2024-11-27
Max. 100 points
Name:
| Task | Max. | Achieved |
|---|---|---|
| 1 | 20 | |
| 2 | 30 | |
| 3 | 20 | |
| 4 | 30 | |
| Sum | 100 |
| Statement | True | False |
|---|---|---|
| Python dictionaries are great for intersection and union of two collections. | ||
| Python tuples are mutable. | ||
sys.exit(0) indicates success to the operating
system. | ||
a, b = b, a is a good way for swapping values
in Python | ||
| Python strings are immutable. | ||
for loops are used for repeated execution
as long as an expression is true. | ||
| Python blocks create a new scope. | ||
| Python supports default parameters and keyword arguments. |
l = ('a', 'b', 'c', )
print(l.append('d'))
def sum(a, b):
return a + c
sum(4, 2)
def concat(c1, c2):
return c1 + c2
print(concat([1, 2], [3]))
s = 'c++'
print(s.upper())
import math
round(math.sin(0.0))
print([1, 2].append(3))
for loop as while loop.
for i in range(10, 0, -2):
print(f'{i*i=}')
i by 2i = 10
while i > 0:
print(f'{i*i=}')
i -= 2
count_words(text, words)
in Python that takes a text and a sequence of words and returns
a dictionary containing each of the words occurs in the text.
Add a docstring to receive all points.
Some examples of possible inputs and outputs:
count_words('', ()) # -> {}
count_words('', ('foo', 'bar')) # -> {'foo': 0, 'bar': 0}
count_words('foo is bar', ('foo', 'bar')) # -> {'foo': 1, 'bar': 1}
count_words('foo is bar', ('is', )) # -> {'is': 1}
count_words('fi foo bar boo foo', ('foo', 'bar')) # -> {'foo': 2, 'bar': 1}
from collections.abc import Sequence # solely for type hints
def count_words(text: str, words: Sequence[str]) -> dict[str, int]:
"""Return how many times each word in words is in text."""
words = set(words)
occurrences = dict.fromkeys(words, 0)
for word in text.split():
if word in words:
occurrences[word] += 1
return occurrences