
Laboratory 3: working with lists
Prerequisites: as in lab2, +lists, +iterating, ifelse
To monitor the coding style we turn on the PEP8 style guide monitoring in Spyder by:
 Going to preferences in Spyder menu
 Clicking on Editor in the selection list on the left
 Clicking on Code Introspection/Analysis (top right corner)
 Ticking the box Realtime code style analysis
 Clicking Apply and OK (bottom right corner)
Implement the following functions in a file called lab3.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
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 emailing it to feeg1001@soton.ac.uk with the subject lab 3 for automatic assessment of
this laboratory session. If some of the tests fail, you can improve your code and resubmit lab3.py
as many times as you like.
