Question

In: Computer Science

In the funcs directory create a file named funcs.py. This section requires that you implement and...

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

Solutions

Expert Solution

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


Related Solutions

Create a file named work.sh in your hw5 directory. Give this file read and write permission...
Create a file named work.sh in your hw5 directory. Give this file read and write permission (no execute permissions) for you alone. No other account should have any access privileges to this file. Change the permissions on the work.sh file so you have read and write permissions. Give everybody else, including the group, read permissions only. Give yourself read, write and execute permissions to the file work.sh. Give everyone else, including the group, execute permissions only. Create a directory named...
Implement a python program in file named tarvel.py. Create an empty dictionary named responses. Implement while...
Implement a python program in file named tarvel.py. Create an empty dictionary named responses. Implement while loop to take in user's name and desired destination for as long as there are user inputs. Prompt user to input yes to continue and no to quit. Prompt for user's name. Receive the name into the program and save it as the value of name variable. Prompt user for their desired vacation destination. Receive response and save it as the value of a...
UNIX/LINUX SCRIPT: Create a named directory and verify that the directory is there by listing all...
UNIX/LINUX SCRIPT: Create a named directory and verify that the directory is there by listing all its contents. Write a shell script to validate password strength. Here are a few assumptions for the password string.   Length – a minimum of 8 characters. • Contain alphabets , numbers , and @ # $ % & * symbols. • Include both the small and capital case letters. give a prompt of Y or N to try another password and displays an error...
Create a file named StudentArrayList.java,within the file create a class named StudentArrayList. This class is meant...
Create a file named StudentArrayList.java,within the file create a class named StudentArrayList. This class is meant to mimic the ArrayList data structure. It will hold an ordered list of items. This list should have a variable size, meaning an arbitrary number of items may be added to the list. Most importantly this class should implement the interface SimpleArrayList provided. Feel free to add as many other functions and methods as needed to your class to accomplish this task. In other...
II. Big Numbers 1. Implement this program in a file named bigNums.py. 2. Create a numerical...
II. Big Numbers 1. Implement this program in a file named bigNums.py. 2. Create a numerical (integer) list, named numbers. Populate the list with 1,000,000 (one million) integer values with values from 0 to 999,999. Hint: Use the range() function. 3. Print the length of the list to the screen. Ensure you have 1,000,000 items in your list. Hint: Use the len() function. 4. Print the smallest item value in the list to the screen. Hint: use the min() function....
Part II: gdb (Debugging under Unix) create a new directory named by Inlab6Part2 enter the directory...
Part II: gdb (Debugging under Unix) create a new directory named by Inlab6Part2 enter the directory Inlab6Part2 create a file named by reverse_new.c with the following contents: /*reverse.c */ #include <stdio.h> void reverse(char *before, char *after); main() {       char str[100];    /*Buffer to hold reversed string */       reverse("cat", str); /*Reverse the string "cat" */       printf("reverse(\"cat\")=%s\n", str); /*Display result */       reverse("noon", str); /*Reverse the string "noon" */       printf("reverse(\"noon\")=%s\n", str); /*Display result */       } void reverse(char *before,...
You first needs to create a file called “myJS.js” in your project directory. Then, include the...
You first needs to create a file called “myJS.js” in your project directory. Then, include the AngularJS library and myJS.js in the header of the index.html page. Now, you need to define your angular application and a controller in this file such that the controller has the following variables with these initial values: name = "Alex Cool"; faculty= "Faculty of Busienss and IT"; university = {name:"University of Ontario Institute of Technology", url:"http://www.uoit.ca"}; contacts = {email:"[email protected]", phone:"xxx-xxx-xxxx"}; skills = [ {name:...
Go to your “Documents” directory; a) Create a file “file1” with the text “hello” in it;...
Go to your “Documents” directory; a) Create a file “file1” with the text “hello” in it; Provide command and screenshot. b) Create a hard link named “file2” linked to “file1”; c) Create a soft link named “soft1” linked to “file1”; create another soft link named “soft2” linked to “file2”. d) View information of the 4 files (what command should you use to produce output) – how do you verify which file is a hard link and which file is a...
Consider a text file that you will create named “employees.txt”. The file contains data organized according...
Consider a text file that you will create named “employees.txt”. The file contains data organized according to the following format:John Smith 10 15Sarah Johnson 40 12Mary Taylor 27 13Jim Stewart 25 8For instance, “John” is the first name, “Smith” is the last name, “10” is the number of hours per week, and “15” is the hourly rate.Write a program that computes the weekly salary of each employee. The program prints the first name, last name, and weekly salary of each...
Move the file “nfile2” into the “unix” directory. Copy the file “nfile1” into the “java” directory....
Move the file “nfile2” into the “unix” directory. Copy the file “nfile1” into the “java” directory. Show how you would create the files “test1”, “test2”, & “test3” in the “test” directory to ensure that they have the same file creation date/time. From the ~ folder, use the * and then the [ ] wildcard procedures to list all the files in the “test” directory. From the directory structure above, give an example of a relative path & an absolute path....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT