lab3

New topics: lists, for-loop

Relevant supplementary videos :

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

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)

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]

Please submit your file lab3.py for this assignment.

Additional (voluntary) tasks are available in lab3-extra.

End of lab3.