Define and test these two python functions

Assignment Help Python Programming
Reference no: EM132077785

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

Reference no: EM132077785

Questions Cloud

Make the sort methods return an integer with this count : For each algorithm, count the number of times it loops (you can make the sort methods return an integer with this count).
Write a program that simulates a lotto : Write a program that simulates a Lotto 6/49. The program should have an array of six integers named lottoNumbers.
Find all of the different stores that are in the central : How would i find all of the different stores that are in the 'central' district?.
Prepare a job claim statement : ACC703 Accounting Information - King’s Own Institute - You may contact the employer and get the position description to contain a more in depth description
Define and test these two python functions : Define and test these two Python functions. You must use recursion to define them: you are not allowed to use loops or local variables.
Make an interface that allows the user to search for an item : Make an interface that allows the user to search for an item with a nice background photo.
Create an account object and make sure it has values : Create an Account object and make sure it has values for all its instance variables. Similarly, you can use hard-code parameter values.
What is the maximum profit : How many pounds of each blend should she make to maximize profits? What is the maximum profit?
Write a method to recursively computer ackermann function : Write a method to recursively computer the Ackermann function. Note that the Ackermann function grows extremely quickly for even small values of m and n.

Reviews

Write a Review

Python Programming Questions & Answers

  Compute the orders for a takeaway coffee shop

Create a Python program that will compute the orders for a Takeaway Coffee shop.

  The number of lowercase letters in the file

The number of uppercase letters in the file The number of lowercase letters in the file

  Write test cases for code

Write file that will import the modules unittest, unittest.mock, zipfile and bb and/or parts of these modules 3. You will be mocking the following:

  Write program that create list large enough to hold all data

Write a program that creates a list large enough to hold all the data, then reads all the data from the file into that list.

  Programming in Python Project - Word Cookies Game

CE 364 Programming in Python Project - Word Cookies Game. Create a test file from the sample input tiles provided. Your code should be robust and resilient

  Basic simulation model to simulate tumour growth

Question: Come out with a basic simulation model to simulate tumour growth which follows the paper CPM and LGCA

  Write a python program that generates a small web site

ICT112 Assignment - Generated Web Site for Sports Club. You first task is to choose a sport or club, and get some suitable input data

  Disabled individuals open jars and containers

What variables are you going to need and what will be their datatype and how would you break this problem into numerous smaller units?

  Fill in the python code

Fill in the Python code to play Tic Tac Toe. I won't award points unless it runs succesfully. # Tic-Tac-Toe Game def drawBoard(board): # Draws the board using the list of numbers print(" ") print(" ",board[0]," | ",board[1]," | ", board[2]) print("--..

  Write a python expression that given the variable inputstr

Write a Python expression (or script) that, given the variable inputStr, set as: inputStr = "The quick brown fox jumps over the lazy dog".

  Write a python program that will accept the base and height

Write a Python program that will accept the base and height of a triangle and compute the area. Write a Python program to compute the greatest common divisor (GCD) of two positive integers.

  Write an expression that concatenates the string

Write an expression that concatenates the String variable suffix onto the end of the String variable prefix .

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd