In: Computer Science
In the funcs directory create a file named funcs.py. This section requires that you implement and test multiple functions. You should develop these functions and their tests one at a time. The function implementations must be placed in funcs.py. The test cases will, of course, be placed in the provided funcs_tests.py.
You must provide at least two test cases for each of these functions. In addition, you should separate your testing into multiple functions (the file includes stubs for the first two testing functions to get you started).
This part will be executed with: python funcs_tests.py
f(x) = 7x2 + 2x
Write a function, named f (a poor name, really, but maps from the context), that corresponds to the stated mathematical definition.
g(x, y) = x2 + y2
Write a function, named g (again, a poor name), that corresponds to the stated mathematical definition.
hypotenuse
Write a function, named hypotenuse, that takes two arguments corresponding to the lengths of the sides adjacent to the right angle of a right-triangle and that returns the hypotenuse of the triangle. You will need to use the math.sqrt function from the math library, so be sure to import math at the top of your source file.
is_positive
Write a function, named is_positive, that takes a single number as an argument and that returns True when the argument is positive and False otherwise. You must write this function using a relational operator and without using any sort of conditional (i.e., if); the solution without a conditional is actually much simpler than one with. Your test cases should use assertTrue and assertFalse as appropriate.
NOTE:All of the files said to be provided are just templates for the solution but are not needed to do the problem
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
Note: Please maintain proper code spacing (indentation), just copy the code part and paste it in your compiler/IDE directly, no modifications required. Also place the below two files in proper folder
#code
#funcs.py
import math
#returns the result of 7x^2 + 2x
def f(x):
return 7*x**2 + 2*x
#returns the result of x^2 + y^2
def g(x, y):
return x**2+y**2
#returns the hypotenuse of triangle with other sides side1 and
side2
def hypotenuse(side1, side2):
#returning square root of (side1^2
+side2^2), could also use the method g()
return
math.sqrt(side1**2+side2**2)
#returns True if x>0, else False
def is_positive(x):
return x>0
#funcs_tests.py
import funcs import unittest #test case for testing all methods in funcs class Test(unittest.TestCase): #method testing f() method def testf(self): self.assertTrue(funcs.f(2)==32) self.assertTrue(funcs.f(0) == 0) self.assertTrue(funcs.f(-2) == 24) # method testing g() method def testg(self): self.assertTrue(funcs.g(1,2)==5) self.assertTrue(funcs.g(0, 2) == 4) self.assertTrue(funcs.g(-1, -2) == 5) # method testing hypotenuse() method def testhypotenuse(self): self.assertTrue(funcs.hypotenuse(4,3) == 5) self.assertTrue(funcs.hypotenuse(3, 4) == 5) self.assertTrue(funcs.hypotenuse(0, 0) == 0) # method testing ispositive() method def testispositive(self): self.assertTrue(funcs.is_positive(5)) self.assertFalse(funcs.is_positive(-2)) self.assertFalse(funcs.is_positive(0)) if __name__ == '__main__': #running unittest unittest.main()
#output
....
----------------------------------------------------------------------
Ran 4 tests in 0.000s
OK