solve 4 questions on homework 2 pdf file attached. There are 3 files attached for the fourth question as well which need to be changed from pdf filesto csv upon download to be used.
Python Programming
Homework 2
Instructor: Dr. Ionut Cardei
The following problems are from Chapters 6 – 9.
For full credit:
make sure you follow exactly all requirements listed in this document,
the program must have all required features and run as expected,
follow the coding style taught in the book. Check out the Google Python Style Guide.
upload on Canvas: the PDF file with all solutions plus the .py source files, in this order.
First, write the solution to a problem i in in a Pyhton file pi.py, for i=1…n.
Then, paste your answers into a new Word document called h2.doc. When done, convert it to PDF and
upload the PDF version (h2.pdf) and the solution .py files on Canvas.
The PDF file is very important: the instructor will write comments on top of the PDF file on Canvas for
you to read and learn from your mistakes.
Write your full name at the top of the file and add a heading before each problem solution.
List the solutions in the PDF file in the given order, 1, 2,.…
Syntax highlighting for the code pasted in the Word file makes it more readable and helps avoid
grading mistakes. Pasting from Spyder (or another IDE) into Word may preserve the Python
colors and styles. If not, consider using http://hilite.me/ or some other online service.
The problems are missing some details (e.g. on design, some method names). Where
unspecified by requirements, feel free to make your own decisions on design and
implementation as long as they comply with the book material, coding style, and don’t
contradict the requirements. If in doubt, ask on the Homework Q&A Forum, or ask the
instructor by email.
You have unlimited attempts to upload this assignment, but only the last one before the deadline
will be graded.
IMPORTANT:
Your solution must be your own original work and it must NOT include code that was shared or
copied from somewhere else. Doing otherwise is against the FAU Code of Academic Integrity.
For this assignment the instructor will use a cheating detector application that finds submissions that
indicate code shared or copied.
Name each required .py file like this: px_Lastname_Firstname.py, Substitute x with the problem
number, and Lastname, Firstname with your own last name and first name.
Example: if the problem number is 2 and your name is Jane Austen, name the problem 2 source file
p2_Austen_Jane.py.
Problem 1.
Write the code for this problem in a file named p1_Lastname_Firstname.py, as required above.
In this problem we parse Python files. (This is typically done by IDE applications, like Spyder or
PyCharm, that must understand the structure of code to do syntax highlighting, for instance.)
a) Write a function called line_number that takes as parameters two strings representing file names.
Assume these are text files. The function reads the file indicated by the first parameter and writes its
lines prefixed by the line number to the file represented by the second parameter.
The function must have a proper docstring and annotations. Use try/except and in case of an error print
a user-friendly message to the terminal and re-raise the exception.
Example: suppose file test.py has this content:
import math
y = math.sqrt(2)
print(y)
Function call line_number(‘test.py’, ‘test.py.txt’) creates a new file named test.py.txt with content:
1. import math
2. y = math.sqrt(2)
3. print(y)
Write a main function that tests function line_number on the problem 1 Python source file itself. Make
sure you don’t overwrite your program by mistake.
b) Write a function called parse_functions that takes as parameter a string representing the name of
a .py file. The function reads and parses the Python file and returns a tuple of tuples where each tuple
has its element 0 the line number of the function definition, element 1 the function name, element 2 the
formal argument list as a string, and element 3 the function code as a string (signature and body), with
all empty lines and comments removed.
The top-level tuple returned must be ordered alphabetically by the function name.
Function parse_functions must have a proper docstring and annotations. Use try/except and in case of
an error print a user-friendly message to the terminal and re-raise the exception.
Write in the main function code that calls parse_functions on the problem 1 Python file and displays
the returned tuple.
Example: suppose file funs.py has this content:
# File with sample functions.
def sum(x, y): # sums up two numbers
“”” Adds two numbers.
Returns the sum.”””
return x + y
# Returns the product.
def mul(x, y): # multiplies two numbers
# z is a local variable
z=x*y
return z
def print_pretty(a):
print(“The result is {:.3f}.”.format(a))
# test these functions:
print_pretty(mul(10, sum(3, 5)))
A call to parse_functions(“funs.py”) returns tuple:
( (9, “mul”, ‘x, y’, ‘def mul(x, y):\n\tz = x * y\n\treturn z\n’ ),
(14, “print_pretty”, ‘a’, ‘def print_pretty(a):\n\tprint(“The result is {:.3f}.”.format(a))\n’ ),
(3, “sum”, ‘x,y’, ‘def sum(x, y):\n “”” Adds two numbers. \n Returns the sum.”””\n\yreturn a + b\n’ ) )
Take a screenshot of the program’s output (parts a-b) . You get 5 points deducted if the screenshot is
missing.
Problem 2. Comprehensions
Write the code for this problem in a file named p2_Lastname_Firstname.py, as required above.
No credit is given if your solutions don’t use the required list/dictionary comprehensions.
a) Write a list comprehension that returns all tuples (a,b,c,d ), with a,b,c,d distinct integers, such that
1