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

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

# # TESTS. Test the class Zillion  # Every test is followed by a comment which shows what must be printed if your # code works correctly. It also shows how many points the test is worth. #  try: z = Zillion('') except RuntimeError: print('Empty string') # It must print 'Empty string' without apostrophes. 2 points.  try: z = Zillion(' , ') except RuntimeError: print('No digits in the string') # It must print 'No digits in the string' without apostrophes. 2 points.  try: z = Zillion('1+0') except RuntimeError: print('Non-digit in the string') # It must print 'Non-digit in the string' without apostrophes. 2 points.  try: z = Zillion('0') except RuntimeError: print('This must not be printed') # It must print nothing. 2 points.  print(z.isZero()) # It must print True. 2 points.  try: z = Zillion('000000000') except RuntimeError: print('This must not be printed') # It must print nothing. 2 points.  print(z.isZero()) # It must print True. 2 points.  try: z = Zillion('000 000 000') except RuntimeError: print('This must not be printed') # It must print nothing. 2 points.  print(z.isZero()) # It must print True. 2 points.  try: z = Zillion('997') except RuntimeError: print('This must not be printed') # It must print nothing. 2 points.  print(z.isZero()) # It must print False. 2 points.  print(z.toString()) # It must print 997. 2 points.  z.increment() print(z.toString()) # It must print 998. 2 points.  z.increment() print(z.toString()) # It must print 999. 2 points.  z.increment() print(z.toString()) # It must print 1000. 2 points.  try: z = Zillion('0 9,9 9') except: print('This must not be printed') # It must print nothing. 3 points.  z.increment() print(z.toString()) # It must print 1000. 2 points.  

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!