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()
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:
Get Answers For Free
Most questions answered within 1 hours.