Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

Ask Python Expert

Need Help for the Python Code :

Define and test these two Python functions. You must use recursion to define them: you are not allowed to use loops or local variables. Both these functions are 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 True more 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 a number, B is a number, and E is a number. The function sigma must call F on all numbers from B to E and returns 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.

test.py

# The comments show what each call must print.

# ODD. Assume N is an integer. Test if N is odd.

def odd(N):
return N % 2 != 0

print(most(odd, [])) # False
print(most(odd, [0])) # False
print(most(odd, [1])) # True   
print(most(odd, [1, 2])) # False
print(most(odd, [1, 2, 3])) # True   

# SQR. Assume N is a number. Return N times N.

def sqr(N):
return N * N

print(sigma(sqr, 0, 0)) # 0
print(sigma(sqr, 1, 0)) # 0
print(sigma(sqr, 0, 4)) # 30   
print(sigma(sqr, 1, 1)) # 1   
print(sigma(sqr, 2, 100)) # 338349

Python, Programming

  • Category:- Python
  • Reference No.:- M93065259

Have any Question?


Related Questions in Python

In this programming assignment you will write a client

In this programming assignment, you will write a client pingprogram in Python. Your client will send a simple ping message to a server, receive a correspondingpong message back from the server, and determine the delay be ...

Question why is software configuration management

Question : Why is software configuration management considered an umbrella activity in software engineering? Please include examples and supporting discussion. The response must be typed, single spaced, must be in times ...

Questionwhat is a python development frameworkgive 3

Question What is a python development framework? Give 3 examples python development framework used today. and explain which development framework is used in which industry.

Question write a python program with a graphical user

Question: Write a python program with a graphical user interface that will allow a user to create a custom pizza which they wish to order. At minimum, the user should be able to choose the size of the pizza, the type of ...

Sieve of eratosthenes in pythonthe goal is to find all the

Sieve of Eratosthenes (in Python) The goal is to find all the prime numbers less than or equal to some natural number maxn. We have a list that tells us if any of the numbers 0..maxn are "marked". It can be an array of b ...

Project reconnaissance and attack on ics

Project: Reconnaissance and Attack on ICS NetworksEnvironment Setup The second mini project will be based on Industrial Network Protocols, specifically the Modbus protocol. Please follow the instructions carefully to set ...

Homework -this homework will have both a short written and

Homework - This homework will have, both a short written and coding assignment. The problems that are supposed to be written are clearly marked. 1) (Written) Make heuristics Describe two heuristics for the slide problem ...

Tasksdemonstrate data scraping of a social network of

Tasks Demonstrate data scraping of a social network of choice. Develop technical documentation, including the development of the code & detailing the results. Provide a report on the findings, that includes research into ...

Python programming assignment -you first need an abstract

Python Programming Assignment - You first need an abstract base class, called, Account which has the following attributes and methods: accountID: This attribute holds the ID assigned the account , if not provided set to ...

Question a software company sells a package that retails

Question : A software company sells a package that retails for $99. Quantity discounts are given according to the following table: Quantity Discount 10 - 19 20% 20 - 49 30% 50 - 99 40% 100 or more 50% Write a program usi ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As