
Laboratory 3: working with lists
Prerequisites: as in lab2, +lists, +iterating, ifelse
Implement the following functions in a file called training3.py:
A function degree(x) that takes an argument x in radian and returns the corresponding value in degrees. I.e. given a value x, the function should return
Example:
In [ ]: degree(math.pi)
Out[ ]: 180.0
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)
A function geometric_mean(xs) that computes the geometric mean of the numbers given in the list xs. Hint: Remember that a**b computes a^{b} (i.e. takes atothebthpower).
Example:
In [ ]: geometric_mean([1, 2])
Out[ ]: 1.4142135623730951
Once you have completed these tasks, and you are confident that your functions work as they should, please submit training3.py electronically by email with subject line training3.
If some of the tests fail, you can improve your code and resubmit training3.py as many times as you like.
Create a file lab3.py that contains the following functions:
A function swing_time(L) that computes and returns the time T [in seconds] needed for an idealized pendulum of length L [in meters] to complete a single oscillation, using the equation
Example:
In [ ]: swing_time(1)
Out[ ]: 2.0060666807106475
A function range_squared(n) that takes an nonnegative integer value n and that returns the list [0, 1, 4, 9, 16, 25, ..., (n1)^2]. If n is zero, the function should return the empty list.
Example:
In [ ]: range_squared(3)
Out[ ]: [0, 1, 4]
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
Remember to check all functions for correctness, in particular: are the (i) functions tested, (ii) documented, (iii) have the right name, (iv) and does the file execute silently and without errors.
Then submit lab3.py by email for assessment of this laboratory session.
