Question

Designing functions Reminder: When designing functions, follow the given steps to reinforce good software development practicWrite the function definition according to the specification provided NOTE: function names and the order and number of the ar5. Design a function called get shipping cost that takes the weight of the letter in kgs and the length and width of a letter# assignment3.py 3 # Student name : 4 #student id: V00 5 import matlh # imports Pythons math library for your use (ie. math.37 def test get rectangle area 3 8 result -get_rectangle_area(0,0) 3 9 40 expected = 0 print test(get rectangle area, abs (64 65 # TODO : define distance 67 68 def test get shipping cost) 69 std len-28.2 std wid- 1O 70 71 72 result - get_shipping_c101 # DOING SO WILL RESULT IN A ZERO GRADE == main : 102 if_name_ main_: 103 main() 104

Designing functions Reminder: When designing functions, follow the given steps to reinforce good software development practices and problem solving strategies: a) Start by writing the documentation for the function. Use docstrings shown in lecture and lab. b) Write test calls to the function in your main function for the different cases of input the function will encounter. This will help you understand the behavior of the function and later behave as tests for your function. c) Define the function d) Run your program to ensure the output of your test calls matches your expected output Get started... Download assignment3.py from conneX Files tab and save it to your working directory. . Write your name and student Vin assignment3.py .For each of the 5 function specifications provided below you must: Write calls to the function within the corresponding test function provided NOTE: Each test function is called from the main function. A test function calls the o function it is testing and then calls print_test to compare the actual result and expected result of the call to the function We have provided you with one example test for each function. You must write additional tests as examples to help you write the function and that ultimately serve to test the function - ensure you have added enough tests to consider all cases.
Write the function definition according to the specification provided NOTE: function names and the order and number of the arguments must be EXACTLY as specified or you will receive a zero grade on that function. o Design a function called get_rectangle_area that takes two floating point numbers that represent the length and width of the rectangle in cm. The function should return the area of that rectangle in cm squared. 1. 2. Design a function called get_average that takes three floating point numbers and returns the average of the three numbers. Design a function called append that takes two phrases and appends them into one new phrase and returns the new phrase with no space between the two phrases joined. Example: If the phrases are "hello there" and "you" the function should return "hello thereyou" 3. 4. Design a function called distance that takes four floating point numbers as arguments that represent 2 points and calculates and returns the distance between the two points. The formula for the distance between two points (xi,yı) and (x2,y2) is: The order of the arguments must be: the first 2 numbers are the x.y coordinate of one point and the second two numbers are the x,y coordinate of the second point. Recall: the math library has a square root function and a power function!
5. Design a function called get shipping cost that takes the weight of the letter in kgs and the length and width of a letter in cm and calculates and returns the cost of shipping that letter. This function must call the get_rectangle_area function. The rules for shipping cost calculations are as follows: If the letter is not bigger than the maximum area of 282cm2 and the weight is less than 0.05k it is a standard size letter and should be charged $1.05 for the baserate + additional 20 cents per 10 grams of weight above 0.03kg Otherwise the letter is non-standard and should be charged $1.90 for the baserate + additional 50 cents per 100 grams of weight above 0.lkg Example: If the package is 12 by 10 cm and weighs 0.24 kgs, it is a non-standard package and 140 grams over the 0.lkg non-standard weight limit, therefore the shipping cost should be approximately: 1.90+1.4*0.5 2.6
# assignment3.py 3 # Student name : 4 #student id: V00 5 import matlh # imports Python's math library for your use (ie. math. sqrt) 7 THRESHOLD- 0.1 # to be used to compare floating point values 9passed0 # number of tests passed # number of tests run 10 tests 0 12 def main(): 13 14 'Complete this assignment by doing the following for each function: - uncommenting one test function call in main NOTE: each test function has at least one test, but you should add additional tests where you feel necessary 15 16 17 18 19 - implement the function (write the documentation and define the function) NOTE: follow the documentation provided for implementing each function in the assignment pdf 20 21 23 - repeat until all functions are tested and implemented 24 # test-get-rectangle-area ( ) # test-get-average () # test-append() # test distance() test-get-shipping-cost () 25 26 27 28 29 # 30 31 32 34 # implement each of the 5 functions after it's corresponding test function 35 # we have provided you with one test for each function 36 # You must add more to ensure you have consider all cases
37 def test get rectangle area 3 8 result -get_rectangle_area(0,0) 3 9 40 expected = 0 print test("get rectangle area", abs (expected-result)
0 0
Add a comment Improve this question Transcribed image text
Answer #1

import math

THRESHOLD = 0.1

passed = 0
tests = 0

def main():

   test_get_rectangle_area()
   test_get_average()
   test_append()
   test_distance()
   test_get_shipping_cost()


def test_get_rectangle_area():
   result = get_rectangle_area(0,0)
   expected = 0
   print_test("get_rectangle_area", abs(expected - result) < THRESHOLD)
   result = get_rectangle_area(3,4)
   expected = 12
   print_test("get_rectangle_area", abs(expected - result) < THRESHOLD)
   result = get_rectangle_area(5,5)
   expected = 25
   print_test("get_rectangle_area", abs(expected - result) < THRESHOLD)

def get_rectangle_area(l,b):
   return l*b

def test_get_average():
   result = get_average(0,0,0)
   expected = 0
   print_test("get_average", abs(expected - result) < THRESHOLD)
   result = get_average(1,1,1)
   expected = 1
   print_test("get_average", abs(expected - result) < THRESHOLD)
   result = get_average(5,6,-11)
   expected = 0
   print_test("get_average", abs(expected - result) < THRESHOLD)

def get_average(a,b,c):
   return (a+b+c)/3

def test_append():
   result = append("","")
   print_test("append", result == "")
   result = append("hello there","you")
   print_test("append", result == "hello thereyou")
   result = append("hi ","hi")
   print_test("append", result == "hi hi")

def append(s,t):
   return s+t

def test_distance():
   result = distance(3,0,0,4)
   print_test("distance", abs(5-result) < THRESHOLD)
   result = distance(0,0,6,8)
   print_test("distance", abs(10-result) < THRESHOLD)
   result = distance(3,4,10,4)
   print_test("distance", abs(7-result) < THRESHOLD)

def distance(x1,y1,x2,y2):
   return math.sqrt((x2-x1)**2 + (y2-y1)**2)

def test_get_shipping_cost():
   std_len = 28.2
   std_wid = 10
   wt03 = 0.03

   result = get_shipping_cost(wt03,std_len,std_wid)
   expected = 1.05
   print_test("get_shipping_cost", abs(expected - result) < THRESHOLD)
   result = get_shipping_cost(0.05,10,20)
   expected = 1.45
   print_test("get_shipping_cost", abs(expected - result) < THRESHOLD)
   result = get_shipping_cost(0.2,20,20)
   expected = 2.4
   print_test("get_shipping_cost", abs(expected - result) < THRESHOLD)

def get_shipping_cost(wt,l,b):

   area = get_rectangle_area(l,b)
   if area <= 282 and wt <= 0.05:
       return 1.05 + 20*(wt-0.03)
   else:
       return 1.9 + 5*(wt-0.1)

def print_test(test_name, expression):

   global tests
   global passed

   tests += 1
   if(expression):
       print(test_name + ': passed')
       passed += 1
   else:
       print(test_name + ': failes')

if __name__ == '__main__':
   main()

Add a comment
Know the answer?
Add Answer to:
Designing functions Reminder: When designing functions, follow the given steps to reinforce good software development p...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 2. Write a program with three functions that will be called from the main function The functions will calculate the...

    2. Write a program with three functions that will be called from the main function The functions will calculate the volume, area and perimeter of a cuboid. You should able to pass three parameters such as length, width and height, and a function will return area, another will return volume and final one will return perimeter. Define all your variables in floating point numbers 3. Modify the problem#2 to calculate the volume, area and perimeter in a sing le function...

  • Project Objectives: To develop ability to write void and value returning methods and to call them...

    Project Objectives: To develop ability to write void and value returning methods and to call them -- Introduction: Methods are commonly used to break a problem down into small manageable pieces. A large task can be broken down into smaller tasks (methods) that contain the details of how to complete that small task. The larger problem is then solved by implementing the smaller tasks (calling the methods) in the correct order. This also allows for efficiencies, since the method can...

  • Note: The order that these functions are listed, do not reflect the order that they should...

    Note: The order that these functions are listed, do not reflect the order that they should be called. Your program must be fully functional. Submit all .cpp, input and output files for grading. Write a complete program that uses the functions listed below. Except for the printOdd function, main should print the results after each function call to a file. Be sure to declare all necessary variables to properly call each function. Pay attention to the order of your function...

  • Project 1 – Classes and Top-down Design Overview Software development projects using the object-oriented approach involve...

    Project 1 – Classes and Top-down Design Overview Software development projects using the object-oriented approach involve breaking the problem down into multiple classes that can be tied together into a single solution. In this project, you are given the task of writing some classes that would work together for providing a solution to a problem involving some basic computations. Learning Objectives The focus of this assignment is on the following learning objectives: • Be able to identify the contents of...

  • OpenSooq Assignment - Software Engineer Given two strings input of printable text, a and b ....

    OpenSooq Assignment - Software Engineer Given two strings input of printable text, a and b . let the edit distance between a and b is least number of operations needed to transform a to b, write php code that calculate edit distance using: 1. hamming distance that only have substitute operations (ex. substitute letter X with letter Y). 2. Levenshtein distance: that have 3 possible operators: insert, delete or substitution operations the function should consider all possibilities but should not...

  • C++...please help follow exact steps output needs to be the exact same Create a class called...

    C++...please help follow exact steps output needs to be the exact same Create a class called Rational for performing arithmetic with fractions. Write a program to test your class. Use integer variables to represent the private data of the elass the numerator and the denominator. Provide a constructor that enables an object of this class to be initialized when it's declared. The constructor should contain default values in case no initializers are provided and should store the fraction in reduced...

  • Question 1) Consider a class Point that models a 2-D point with x and y coordinates. Define the c...

    C++ Question 1) Consider a class Point that models a 2-D point with x and y coordinates. Define the class point that should have the following Private data members x and y (of type int), with default values of 0 A constant ID of type int A private static integer data member named numOfPoints This data member should be o Incremented whenever a new point object is created. o Decremented whenever a point object is destructed. A default constructor An...

  • Lab 7: Void and Value-Returning Functions Lab 7: Void and Value-returning functions             ...

    Lab 7: Void and Value-Returning Functions Lab 7: Void and Value-returning functions              Due date: 11/6/19 Problem: Write a C++ program that calculates the areas of a rectangle and of an ellipse.                    Area = base * height             Area = π * radius a * radius b Note: all images extracted from http://www.mathsisfun.com/area-calculation-tool.html ------------------------------------------------------------------------------------------------------------------------ Your task: implement in C++ the algorithm solution shown below. ------------------------------------------------------------------------------------------------------------------------ Part A (79...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT