Question

1. Design a function called sum that will take as arguments two floating-point numbers and will...

1. Design a function called sum that will take as arguments two floating-point numbers and will return the sum of those two numbers.

2. Design a function called get_longer that will take as arguments two phrases (strings) and will return the longer of the two strings.

3. Design a function called get_hypotenuse that will take as arguments the two lengths of the short sides (a and b in the formula below) of a right-angle triangle and returns the length of the hypotenuse.
Recall: a2 + b2 = c2 where c is the hypotenuse.

4. Design a function called get_restaurant_bill that will take as parameters, the number of people at the table, the $ amount of food and the $ amount of liquor. The function should return the $ share of the bill that each person owes:
-calculate the tip on the total food + liquor bill before tax (15% if less than 8 people, otherwise 20%) -calculate the total tax (15% on liquor and 5% on food)

-calculate the total bill including tip and taxes
-calculate the $ share given the number total bill and number of people at the table

On python, the coding should be similar to this type:

THRESHOLD = 0.1 # to be used to compare floating point values

def main():
#Test 1: test add1 with int argument
print("Test 1:")
n1 = 4
result = add1(n1)
expected = 5

#prints the values of input, output result and expected result:
print("n1:", n1, "result:", result, "expected:", expected)

#Compare the following with how you wrote the print_test
#functions in Assignment 1. How does this work?
print_test("testing add1 with int", result==expected)
print()

#Test 2: test add1 with float argument
print("Test 2:")
n1 = 4.79
result = add1(n1)
expected = 5.8

#prints the values of input, output result and expected result:
print("n1:", n1, "result:", result, "expected:", expected)

# when we test our floating point number values, we make
# they are within 0.1 of the expected result, just because
# rounding errors can occur.
print_test("testing add1 with float", abs(result-expected) < THRESHOLD)

#Exercise 2: test your functions here...
  


#######################
# FUNCTION DEFINITIONS:

# (float -> float)
# returns n + 1
def add1(n):
n *= 1
return n


# (str, bool -> None)
# prints test_name and "passed" if expr is true otherwise prints "failed"
def print_test(test_name, expr):
if(expr):
print(test_name + ": passed")
else:
print(test_name + ": failed")

# The following code will call your main function
# It also allows our grading script to call your main
# DO NOT ADD OR CHANGE ANYTHING PAST THIS LINE
# DOING SO WILL RESULT IN A ZERO GRADE
if __name__ == "__main__":
main()

Homework Answers

Answer #1

CODE IN PYTHON:

import math
def sum(a,b):
return a+b;
def get_longer(phrase1,phrase2):
n = len(phrase1)
m = len(phrase2)
if(n>m):
return phrase1
return phrase2
def get_hypotenus(a,b):
c = a*a + b*b
return math.sqrt(c)
def get_restaurant_bill(n,foodBill,liquorBill):
totalBill = foodBill + liquorBill
tip = 0
if(n<8):
tip = totalBill * 0.15
else:
tip = totalBill * 0.2
tax = liquorBill * 0.15 + liquorBill * 0.05
totalBill += tax + tip
return totalBill/n

print("The sum 12.2 and 13.3 is:",sum(12.2,13.3))
print("longest of havingFun and gettingFun:",get_longer("havingFun","gettingFun"))
print("hypotenus of 3 and 4:",get_hypotenus(3,4))
print("Share of each person:",get_restaurant_bill(5,1000,1500))

INDENTATION:


OUTPUT:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Design a function called print_area that takes two floating-point numbers, representing the length and width of...
Design a function called print_area that takes two floating-point numbers, representing the length and width of a rectangle. The print_area function calculates and prints the area of the rectangle, rounded to two decimal places. Design a function called small_enough_word that takes a string for which the number of characters will be tested, and an integer representing the maximum allowable number of characters that the word can contain. Remember: the arguments MUST be in this order (the string of text before...
Design a function called print_area that takes two floating-point numbers, representing the length and width of...
Design a function called print_area that takes two floating-point numbers, representing the length and width of a rectangle. The print_area function calculates and prints the area of the rectangle, rounded to two decimal places.
Function Example: Write a Python function that receives two integer arguments and writes out their sum...
Function Example: Write a Python function that receives two integer arguments and writes out their sum and their product. Assume no global variables. def writer(n1, n2): sum = n1 + n2 product = n1*n2 print("For the numbers", n1, "and", n2) print("the sum is", sum) print("and the product is", product) ... 1) Create a PYHW2 document that will contain your algorithms in flowchart and pseudocode form along with your screen shots of the running program. 2) Create the algorithm in both...
TO BE DONE IN PYTHON: Write a function `lowest_integer()` which takes 2 input arguments: 1)a function...
TO BE DONE IN PYTHON: Write a function `lowest_integer()` which takes 2 input arguments: 1)a function `g` representing an increasing function g(x) 2) a number `gmin`, and returns an integer `nmin` such that nmin>0 is the smallest integer that satisfies g(nmin)>gmin. test: def g(n): return 2*n print(lowest_integer(g, 10)) Output: 6
1. Write a function called compute_discount which takes a float as the cost and a Boolean...
1. Write a function called compute_discount which takes a float as the cost and a Boolean value to indicate membership. If the customer is a member, give him/her a 10% discount. If the customer is not a member, she/he will not receive a discount. Give all customers a 5% discount, since it is Cyber Tuesday. Return the discounted cost. Do not prompt the user for input or print within the compute_discount function. Call the function from within main() and pass...
##4. What will the following program display? ##def main(): ## x = 1 ## y =...
##4. What will the following program display? ##def main(): ## x = 1 ## y = 3.4 ## print(x, y) ## first printing ## change_us(x, y) ## print(x, y) ##second printing ## ##def change_us(a, b): ## a = 0 ## b = 0 ## print(a, b) ## ##main() ## ##Yes, yes, main() displays ##1 3.4 ##0 0 ##1 3.4 ## The question is: why x and y are still the same while the second printing of (x,y)? ## It seems...
Using Python (Spyder), Write a function convert_temperature that takes two arguments: a float parameter temp, the...
Using Python (Spyder), Write a function convert_temperature that takes two arguments: a float parameter temp, the temperature to convert; and a str parameter conversion that determines which conversion to perform. Assume that conversion can take two possible values, 'celsius_to_fahrenheit' and 'fahrenheit_to_celsius', and make 'celsius_to_fahrenheit' the default value. Have your function print out a sentence explaining the conversion done, such as “0 °C converts to 32 °F”, and have the function return the converted value. Your submission should include the function...
Write a template function maxn() that takes as its arguments an array of items of type...
Write a template function maxn() that takes as its arguments an array of items of type T and an integer representing the number of elements in the array and that returns the largest item in the array. The number of elements should take the default value of 10. The program should include a specialization that takes an array of strings as an argument and returns the longest string. (If there is a tie, the function should return the first one...
c++ Write a program that calls a function calculateSum to calculate the sum from -1 to...
c++ Write a program that calls a function calculateSum to calculate the sum from -1 to N. The function calculateSum has one parameter N of type integer and returns an integer which represents the sum from -1 to N, inclusive. Write another function calculateAverage that calculates an average. This function will have two parameters: the sum and the number of items. It returns the average (of type float). The main function should be responsible for all inputs and outputs. Your...
Define a function called divideSentences in the python file. This function will take in one input...
Define a function called divideSentences in the python file. This function will take in one input argument: a string. This string will typically be a whole piece of text, a paragraph or more. This function will separate the text into sentences, assuming that each sentence ends with either a period, a question mark, or an exclamation mark. It should return a list of the sentence strings. Be sure that the sentence-ending mark is still attached to the sentence. Remove any...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT