2023-01-11
Max. 32 points
Name:
Task | Max. | Achieved |
---|---|---|
1 | 6 | |
2 | 10 | |
3 | 6 | |
4 | 4 | |
5 | 6 | |
Sum | 32 |
Statement | True | False |
---|---|---|
In Python, the def keyword is used to
define a function. | ||
In Python it is recommended to use four spaces for indentation. | ||
The "ifmain" pattern allows a module to behave differently when it is executed vs. imported. | ||
Python code cannot be edited with a regular text editor. A special editor is required. | ||
In Python, it is possible to iterate over sequences. | ||
A Python dictionary is particularly useful when frequently testing if a value exists in the data structure. | ||
Dictionaries in Python are immutable. | ||
Python sets are mutable. | ||
Python tuples are immutable. | ||
Python sets support the intersection, difference and union operations. | ||
Doctests are a great way to illustrate how code is supposed to be used. | ||
Comments are the same as docstrings. |
def sum(a, b, c):
return a + b + c
sum(4, 2, 3)
def circumference(length, width):
return 2 * length + 2 * width
print(circumference(5, 2))
s = 'Hello World!'
s.lower()
print(s)
l = [4, 2, 3, 1]
print(l.append(0))
def fun(a, b=5, c):
return a + b + c
print(fun(10, 5))
find_max(iterable)
that returns the
maximum value of an iterable. It can be assumed that the iterable contains
at least one value and that all values in the iterable are numeric. Don't
forget to add a proper docstring. You are not allowed to use the built in
max(.)
function.
(6 points)
def find_max(iterable):
"""Return the maximum value of the iterable."""
max_ = iterable[0]
for item in iterable[1:]:
if max_ < item:
max_ = item
return max_
l = [1, 2, 3, 4]
l.append(l[-1] + l[-2])
l.pop(0) # Remove and return item at index (default last).
print(l.count(3)) # Print number of occurrences of value.
t = (1, 2, 3, 4)
t = t + (t[-1] + t[-2],)
t = t[1:]
print(t.count(3))
as_numeric(text)
that returns a string
containing only the numbers that correspond to the input text.as_numeric('0800 reimann')
should return
'0800 7346266'
.def as_numeric(number_name):
"""
Take telephone number as name string and return string of digits.
>>> as_numeric('0800 reimann')
'0800 7346266'
"""
number = ""
number_name = number_name.lower()
for char in number_name:
if char in string.digits + ' ':
number += char
elif char in {'a', 'b', 'c'}:
number += '2'
elif char in {'d', 'e', 'f'}:
number += '3'
elif char in {'g', 'h', 'i'}:
number += '4'
elif char in {'j', 'k', 'l'}:
number += '5'
elif char in {'m', 'n', 'o'}:
number += '6'
elif char in {'p', 'q', 'r', 's'}:
number += '7'
elif char in {'t', 'u', 'v'}:
number += '8'
elif char in {'w', 'x', 'y', 'z'}:
number += '9'
return number