Question

In: Computer Science

''' 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, 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. You can use string
functions OR f-strings to accomplish the formatting.

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

IMPORTANT!!! MIMIR RUNS PYTHON 3.5 WHEN YOU RUN python3. F-STRINGS ARE NEW TO PYTHON
3.6 AND ABOVE. IF TESTING ON MIMIR, YOU MUST RUN python3.6 INSTEAD OF JUST python3
IF YOU USE F-STRINGS.

print_header
------------
Print out a header line that prints the "text" parameter such that it is centered and
takes up a total of 60 characters. It should then print a second line that contains
60 "+" symbols. In other words, calling:

print_header('2021 Crop Report')

should print as follows:

2021 Crop Report
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

print_integer_line
------------------
Print out a line of text where the "text" parameter is left aligned and takes up
a total of 50 characters and the "value" parameter is right aligned and takes up
10 characters. In other words, calling:

print_integer_line('Fall 2020 Enrollment',504)

should result in a printed line as follows:

Fall 2020 Enrollment 504

print_float_line
----------------
Print out a line of text where the "text" parameter is left aligned and takes up
a total of 45 characters and the "value" parameter is right aligned and takes up
15 characters AND is displayed as a float value with TWO decimal place. So, calling:

print_float_line('The value of e',2.718281828459045)

will print as follows:

The value of e 2.72

print_footer
------------
Print out a footer line that prints a single line of 60 "+" characters followed
by a line that prints the "left", "middle" and "right" parameters such that left
is aligned to the left, middle is centered, and right is aligned to the right.
Each should take up 20 characters. In other words, if I call:

print_footer('1/1/2020','Summary Data','page 1 of 1')

your printed output should be:

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1/1/2020 Summary Data page 1 of 1

print_circle_area
-----------------
Finally, print out the surface are of a circle using your print_float_line function AND
using your circle_area function from math_funcs. You will thus need to import the
required function from math_funcs (remember that this is in the "funcs" directory, so
you need to be sure to import it correctly). Call your print_float_line function with the
results and appropriate description. For example, calling:

print_circle_area(5)

should result in a printed line as follows:

The area of a circle with radius of 5 78.54

'''

def print_header(text):

def print_integer_line(text,value):

def print_float_line(text,value):

def print_footer(left,middle,right):

def print_circle_area(radius):

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

Solutions

Expert Solution

Save the code in file
main.py

# define print_header function
def print_header(text):
'''
Print out a header line that prints the "text" parameter such that it is centered and
takes up a total of 60 characters. It should then print a second line that contains
60 "+" symbols. In other words, calling:
print_header('2021 Crop Report')
should print as follows:
2021 Crop Report
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
'''
print(text.center(60))
print('+' * 60)

# define print_integer_line function
def print_integer_line(text, value):
'''
print_integer_line
------------------
Print out a line of text where the "text" parameter is left aligned and takes up
a total of 50 characters and the "value" parameter is right aligned and takes up
10 characters. In other words, calling:
  
print_integer_line('Fall 2020 Enrollment',504)
  
should result in a printed line as follows:
  
Fall 2020 Enrollment 504
'''
print("{:50}".format(text) +"{:10d}".format(value));

# define print_float_line function
def print_float_line(text, value):
'''
print_float_line
----------------
Print out a line of text where the "text" parameter is left aligned and takes up
a total of 45 characters and the "value" parameter is right aligned and takes up
15 characters AND is displayed as a float value with TWO decimal place. So, calling:
print_float_line('The value of e',2.718281828459045)
will print as follows:
The value of e 2.72
'''
print("{:45}".format(text) +"{:15.2f}".format(value));

# define print_footer function
def print_footer(leftText, midText, rightText):
'''
print_footer
------------
Print out a footer line that prints a single line of 60 "+" characters followed
by a line that prints the "left", "middle" and "right" parameters such that left
is aligned to the left, middle is centered, and right is aligned to the right.
Each should take up 20 characters. In other words, if I call:
print_footer('1/1/2020','Summary Data','page 1 of 1')
your printed output should be:
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1/1/2020 Summary Data page 1 of 1
'''
print("{:20}".format(leftText) +"{:^20}".format(midText) +"{:>20}".format(rightText));
  
# define print_circle_area function
def print_circle_area(radius):
'''
print_circle_area
-----------------
Finally, print out the surface are of a circle using your print_float_line function AND
using your circle_area function from math_funcs. You will thus need to import the
required function from math_funcs (remember that this is in the "funcs" directory, so
you need to be sure to import it correctly). Call your print_float_line function with the
results and appropriate description. For example, calling:
print_circle_area(5)
should result in a printed line as follows:
The area of a circle with radius of 5 78.54
'''
PI = 3.142
area = PI*radius*radius
print_float_line('The area of a circle with radius of', area)


def main():
print_header('2021 Crop Report')
print_integer_line('Fall 2020 Enrollment', 504)
print_float_line('The value of e',2.718281828459045)
print_footer('1/1/2020','Summary Data','page 1 of 1')
print_circle_area(5)

if __name__ == "__main__":
main()
import doctest
doctest.testmod(name ='print_header', verbose = True)
doctest.testmod(name ='print_integer_line', verbose = True)
doctest.testmod(name ='print_float_line', verbose = True)
doctest.testmod(name ='print_footer', verbose = True)
doctest.testmod(name ='print_circle_area', verbose = True)

The image version of the code is also given as indentation is important in python

Here is the test output:


Related Solutions

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...
// Add the following functions: // 1. 'getRadius' asks the user to enter the radius of...
// Add the following functions: // 1. 'getRadius' asks the user to enter the radius of a circle // and returns the given value. (should return a double) // 2. 'calcArea' takes the radius and returns the area of a circle. // 3. 'printResults' void type function that should print the results to // console. // Your function needs to have a local variable called 'PI' which holds the // value '3.14159'. // The function call is provided, you just...
The area under the curve must add up to one for a. all density functions. b....
The area under the curve must add up to one for a. all density functions. b. just one density function. c. no density function. d. a special group of density functions. 3 points    QUESTION 2 If the mean of a normal distribution is negative, a. the variance must also be negative. b. the standard deviation must also be negative. c. a mistake has been made in the computations, because the mean of a normal distribution can not be negative....
Complete the attached program by adding the following: a) add the Java codes to complete the...
Complete the attached program by adding the following: a) add the Java codes to complete the constructors for Student class b) add the Java code to complete the findClassification() method c) create an object of Student and print out its info in main() method of StudentInfo class. * * @author * @CS206 HM#2 * @Description: * */ public class StudentInfo { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT