lab2¶
New topics: lists, for-loop
The intended solution path for count, min_max and range_square is the use of
a for loop combined with if statements.
Please ensure the submitted work follows the PEP8 style guide.
Relevant Socratica video:
degree(x)¶
Write a function degree(x) that takes an argument x in radian
and returns the corresponding value in degrees: given a value
\(x\), the function should return \(x \frac{360}{2\pi}\).
Example:
In [ ]: degree(math.pi)
Out[ ]: 180.0
range_squared(n)¶
Implement a function range_squared(n) that takes an non-negative
integer value n and that returns the list
\([0, 1, 4, 9, 16, 25, ..., (n-1)^2]\). If \(n\) is zero, the function
should return the empty list [].
Example:
In [ ]: range_squared(3)
Out[ ]: [0, 1, 4]
count(element, seq)¶
Implement a function count(element, seq) that counts how often the
given element element occurs in the given sequence seq, and
returns this integer value. For example, count(2, list(range(5)))
should return 1.
Example:
In [ ]: count('dog', ['dog', 'cat', 'mouse', 'dog'])
Out[ ]: 2
In [ ]: count(2, list(range(5)))
Out[ ]: 1
min_max(xs)¶
Implement a function min_max(xs) that computes the minimum value
xmin of the elements in the list xs, and the maximum value
xmax of the elements in the list, and returns a tuple
(xmin, xmax).
Example:
In [ ]: min_max([0, 1, 2, 10, -5, 3])
Out[ ]: (-5, 10)
Implement the functions in a file lab2.py and submit this learn@mpsd.mpg.de with subject lab2.
For the following labs, use the same naming scheme.
Additional (voluntary) tasks are available in lab2-extra.
End of lab2.