0. Introduction. This assignment asks you to write two recursive Python function
ID: 3870957 • Letter: 0
Question
0. Introduction.
This assignment asks you to write two recursive Python functions that take other Python functions as their arguments.
1. Implementation.
Define and test these two Python functions. You must use recursion to define them: you are not allowed to use loops or variables. Also you are not allowed to change list elements. Both these functions are simple and short: if you find yourself writing many pages of code, then you do not understand the assignment.
most(P, S)
Here P is a function of one argument that returns either True or False, and S is a list. The function most calls P on each element of S. It must return True if P returns Truemore often than it returns False. It must return False otherwise. Here are some examples of how most must work, where the symbol ‘’ means returns, and where the function odd tests if a number is odd.
most(odd, [])
False
most(odd, [0])
False
most(odd, [1])
True
most(odd, [1, 2])
False
most(odd, [1, 2, 3])
True
These are only examples! Your function most must work correctly for any P, and any S whose elements are compatible with P.
sigma(F, B, E)
Here F is a function of one argument that returns an integer, B is a nonnegative integer, and E is a nonnegative integer. The function sigma must call F on all nonnegative integers from B to E, and return the sum of those calls. If B > E then sigma must return 0. Here are some examples of how sigma must work, where the function sqr returns the square of its argument.
sigma(sqr, 0, 0)
0
sigma(sqr, 1, 0)
0
sigma(sqr, 0, 4)
30
sigma(sqr, 1, 1)
1
sigma(sqr, 2, 100)
338349
These are only examples! Your function sigma must work correctly for any F, B, and E.
Hint: you may write additional functions that are called by more and sigma to help them do their jobs. However, these ‘‘helper’’ functions must also be recursive, without loops and variables.
2. Tests.
To grade thee work, look briefly at your functions to see if they are recursive, and if they seem general enough to work for arguments other than the ones in the examples. If they are not, then you will receive 0 points.
If your functions are recursive and general, you will test them using the code on the file tests.py, If a test behaves exactly as it should, then you will receive all the points for that test. If a test does anything else, then you will receive no points for that test.
3 Deliverables.
Run the tests in the file tests.py. Then submit the Python code for your functions most and sigma.
HERE THE TESTS.PY
most(odd, [])
False
most(odd, [0])
False
most(odd, [1])
True
most(odd, [1, 2])
False
most(odd, [1, 2, 3])
True
Explanation / Answer
def sigma(f,b,e):
if b > e:
return 0
else:
return f(e)+sigma(f,b,e-1)
def most(P,S):
if len(S) == 0:
return 0
else:
if P(S[0]) + most(P,S[1:]) > len(S) // 2:
return True
return False
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.