Question: Python 3: strict instructions are to be followed: (Exact Code Preffered) Pay close attention to the return value for each function, as per the project
Python 3: strict instructions are to be followed:
(Exact Code Preffered)
Pay close attention to the return value for each function, as per the project specification. Programming style is important! Remember to
Include a docstring in every function (include type contract, brief description of the function, examples of use)
Use descriptive variable names
Add appropriate comments
Test your code using testmod from the doctest module.
(1a) Complete the examples in the docstring and then write the body of the following function:
def diff_first_last(L):
'' (list) -> boolean
Precondition: len(L) >= 2
Returns True if the first item of the list is different from the last; else returns False.
>>> diff_first_last([3, 4, 2, 8, 3])
False
>>> diff_first_last(['apple', 'banana', 'pear'])
??
>>> diff_first_last([4.0, 4.5])
??
'''
(1b) Add two more examples/test cases to the docstring of diff_first_last and indicate why they are useful test cases.
(2a) Write a function middle which takes a list L as its argument and returns the item in the middle position of L, when L has an odd length. Otherwise, middle should return 999999. For example, calling middle([8, 0, 100, 12, 18]) should return 100. You should, at least, have five different lists to test in the docstring: lists of even and odd number of numeric elements, lists of even and odd number of string elements, and a blank list. (Hint: you may use the // integer division operator).
(2b) Write a function, mySum, with one parameter, L, a list of numbers. mySum returns the sum of the numbers in the input list. (mySum should not use Pythons builtin sum function.)
For example,
>>> mySum([2, 3, 2])
7
In the docstring, there should be minimally five examples of function calls including a blank list, a single element list, and list of positive and negative numbers.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
