Question

In: Computer Science

Complete the following functions. You MUST, MUST, MUST add: 1) a doc comment in the proper...

Complete the following functions. You MUST, MUST, MUST add:

1) a doc comment in the proper location for EACH function that describes
the basic purpose of the function.
2) AT LEAST 5 doc test comments in EACH function that:
* test that the function does what it is supposed to do
* tests that it does what it's supposed to do with odd inputs
* tests "edge" cases (numbers at, just above, just below min/max, empty strings, etc.)

You MUST, MUST, MUST then test each of your methods by BOTH:

1) running the "main.py" script
2) running this module directly to run the doc tests

Except as noted, you can implement the functions however you like. And if your grade-school
math's out of date, Google's your friend for formulas (BUT NOT FOR CODE).

CHALLENGE: Use try/except blocks to avoid crashes when passing in unexpected parameters.

circle_area
-----------
RETURN the area of a circle with a radius supplied by the parameter.
Note that you MUST use the "pi" constant from the math module, so
use an import statement. If the radius passed is less than 1 or
greater than 1000, PRINT "ERROR" and RETURN 0.

sphere_surface_area
-------------------
RETURN the surface area of a sphere with the supplied radius. Slightly
different error check here: if the radius passed is less than 1 or
greater than 250, PRINT "ERROR" and RETURN 0.

sphere_volume
-------------
RETURN the volume of a sphere with the supplied radius. Again, slightly
different error check here: if the radius passed is less than 1 or
greater than 100, PRINT "ERROR" and RETURN 0.
'''

# IMPORT THAT PI CONSTANT HERE


def circle_area(radius):

def sphere_surface_area(radius):

def sphere_volume(radius):

if __name__ == "__main__":
import doctest
doctest.testmod()

Solutions

Expert Solution

from doctest import testmod
import math

#Function to return area of circle.
def circle_area(radius):
'''
Defining input and expected output:
  
Testing the function do what it supposed to do.

>>> circle_area(4)
50.26548245743669

Testing the function at odd inputs.

>>> circle_area(-20)
ERROR
0

>>> circle_area(1020)
ERROR
0

Testing the function at edge cases.

>>> circle_area(0)
ERROR
0

>>> circle_area(2)
12.566370614359172

>>> circle_area(999)
3135312.609875267

>>> circle_area(1001)
ERROR
0
'''
if(radius<1 or radius>1000):
print("ERROR")
return 0
else:
return ((math.pi)*(radius**2))

#Function to return the surface area of sphere.
def sphere_surface_area(radius):
'''
Defining input and expected output:
  
Testing the function do what it supposed to do.

>>> sphere_surface_area(4)
201.06192982974676

Testing the function at odd inputs.

>>> sphere_surface_area(-20)
ERROR
0

>>> sphere_surface_area(270)
ERROR
0

Testing the function at edge cases.

>>> sphere_surface_area(0)
ERROR
0

>>> sphere_surface_area(2)
50.26548245743669

>>> sphere_surface_area(249)
779127.544460883

>>> sphere_surface_area(251)
ERROR
0
'''
if(radius<1 or radius>250):
print("ERROR")
return 0
else:
return (4*math.pi*(radius**2))

#Function to return volume of sphere.
def sphere_volume(radius):
'''
Defining input and expected output:
  
Testing the function do what it supposed to do.

>>> sphere_volume(4)
268.082573106329

Testing the function at odd inputs.

>>> sphere_volume(-20)
ERROR
0

>>> sphere_volume(270)
ERROR
0

Testing the function at edge cases.

>>> sphere_volume(0)
ERROR
0

>>> sphere_volume(2)
33.510321638291124

>>> sphere_volume(99)
4064378.94691403

>>> sphere_volume(101)
ERROR
0
'''
if(radius<1 or radius>100):
print("ERROR")
return 0
else:
return (4/3*math.pi*(radius**3))

try:
print(circle_area(float(input("Enter the radius of circle to find it's area:"))))
print(sphere_surface_area(float(input("Enter the radius of sphere to find it's surface area:"))))
print(sphere_volume(float(input("Enter the radius of sphere to find it's volume:"))))

except ValueError:
print("Please enter a valid value of radius")

if __name__=='__main__':
testmod(verbose = True)


Related Solutions

''' string_funcs ========== Complete the following functions. You MUST, MUST, MUST add: 1) a doc comment...
''' string_funcs ========== Complete the following functions. You MUST, MUST, MUST add: 1) a doc comment in the proper location for EACH function that describes the basic purpose of the function. 2) AT LEAST 3 doc test comments in EACH function that: * test that the function does what it is supposed to do * tests that it does what it's supposed to do with odd inputs * tests "edge" cases (numbers at, just above, just below min/max, empty strings,...
In C++ Complete the template program. ADD to your c++ program as a comment the PARTIAL...
In C++ Complete the template program. ADD to your c++ program as a comment the PARTIAL output from executing your program - Only copy the last 6 lines of output. There is no input data for this problem. // Find Pythagorean triples using brute force computing. #include <iostream> using std::cout; using std::endl; int main() { int count = 0; // number of triples found long int hypotenuseSquared; // hypotenuse squared long int sidesSquared; // sum of squares of sides cout...
You must write each of the following scheme functions. You must use only basic scheme functions...
You must write each of the following scheme functions. You must use only basic scheme functions do not use third-party libraries to support any of your work. Do not use any function with side effects. Write a function (merge-sorter L1) that takes list-of-integers L1 and returns all elements of L1 in sorted order. You must use a merge-sort technique that, in the recursive case, a) splits L1 into two approximately-equal-length lists, b) sorts those lists, and then c) merges the...
You must write each of the following scheme functions. You must use only basic scheme functions...
You must write each of the following scheme functions. You must use only basic scheme functions do not use third-party libraries to support any of your work. Do not use any function with side effects. This problem need to use DrRacket software. Racket Language. Write a function named (forget-n L1 N) that returns the elements of L1 except for the first N. If N is negative, return all elements. If N exceeds the length of L1 return the empty list....
You must write each of the following scheme functions. You must use only basic scheme functions...
You must write each of the following scheme functions. You must use only basic scheme functions do not use third-party libraries to support any of your work. Do not use any function with side effects. This problem needs to use DrRacket software. Racket Language. Write a function (indices L1 X) that takes a list of elements L1 and an element X. The function returns a list of the indices in L1 that contain X. See the following examples for clarification....
You must write each of the following scheme functions. You must use only basic scheme functions,...
You must write each of the following scheme functions. You must use only basic scheme functions, do not use third-party libraries to support any of your work. Do not use any function with side effects. This problem need to use DrRacket software. Racket Language. Write a function named (first-n L1 N) that returns the first N elements of L1. If N is negative, return the empty list. If N exceeds the length of L1 return all elements of L1. (first-n...
You must write each of the following scheme functions. You must use only basic scheme functions...
You must write each of the following scheme functions. You must use only basic scheme functions do not use third-party libraries to support any of your work. Do not use any function with side effects. This problem need to use DrRacket software. Racket Language. Write a function (join-together L1 L2) that takes a sorted list (ascending) of integers L1 and a sorted list of elements L2 and returns a sorted list containing all elements of both L1 and L2. See...
You must write each of the following scheme functions. You must use only basic scheme functions...
You must write each of the following scheme functions. You must use only basic scheme functions do not use third-party libraries to support any of your work. Do not use any function with side effects. Write a function (indices L1 X) that takes a list of elements L1 and an element X. The function returns a list of the indices in L1 that contain X. See the following examples for clarificaton. (indices '(a b c a e f a) 'a')...
You must write each of the following scheme functions. You must use only basic scheme functions...
You must write each of the following scheme functions. You must use only basic scheme functions do not use third-party libraries to support any of your work. Do not use any function with side effects. Write a function named (list-replace ALIST SYM VAL) that accepts a list of elements and returns that list where all SYM's (a single symbol) have been replaced by the VAL (some scheme value). The replacement must occur even within nested lists. For example: (list-replace '(a...
Remember all responses must be thorough... complete and proper sentences are expected! 1. What human relations...
Remember all responses must be thorough... complete and proper sentences are expected! 1. What human relations skills do you think would be helpful to you in a new job (accountant)? 2. What conclusions can you draw about the importance to you and to your employer in continuing to develop your human relations skills?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT