The assignment should be submitted via Canvas.
Submit a file called assignment1.zip, containing the following files:
- answers.pdf, for your answers to the written tasks. Only PDF files will be accepted. All text should be typed, and if any figures are present they should be computer-generated. Scans of handwriten answers will NOT be accepted.
- file_stats.py, containing your code for Task 7.
- nth_smallest.py, containing your code for Task 8.
The above naming conventions are mandatory, non-adherence to these specifications can incur a penalty of up to 20 points.
Your name and UTA ID number should appear on the top line of your answers.pdf document.
Task 1 (10 points)
def factorial(n):
result = 1
for i in range(2, (n+1)):
result = result * i;
return result
Consider the factorial function above, implemented in Python. What is the time complexity of this function, in Θ notation, with respect to n?
Task 2 (10 points)
Re-implement, in Python, the factorial function of Task 1 so that it uses a recursive function call instead of using any loops (like while loops and for loops). Do not call any built-in or library functions for computing the factorial. You do NOT need to do any error-checking (like checking if the input argument is negative). For this task, please include your code in the answers.pdf file, do NOT submit a separate code file.
Task 3 (10 points)
def foo(n):
result = 0
for i in range(1, n+1):
for j in range(1, i+1):
result = result + 1
return result
Consider the foo function above, implemented in Python. What is the time complexity of this function, in Θ notation, with respect to n?
Task 4 (10 points)
Consider matrices A and B defined as:
What is the result of matrix multiplication A*B? Specify:
- The number of rows of the result matrix.
- The number of columns of the result matrix.
- The values at all positions of the result matrix.
Task 5 (10 points)
Consider function f(x) = 3x2 + 5x - 7.
Part a: What is the derivative f'(x)? Provide a specific formula as a function of x.
Part b: What is f'(5)? Your answer should be a real number.
Part c: What is the second derivative f''(x)?
Provide a specific formula as a function of x.
Part d: What is f''(5)? Your answer should be a real number.
Task 6 (10 points)
Two hens lay a combined total of two eggs in two days. If this rate of egg production per hen per day continues, how many eggs do ten hens lay in ten days?
Task 7 (10 points)
Implement a a Python executable file called file_stats.py that reads computes the average and standard deviation of each column of numbers contained in some specified file. In particular, your program should be invoked from the command line as follows:
python file_stats.py pathname
Argument pathname is the path name of the data file. The path name can specify any file stored on the local computer (not just in the current directory). This file will contain data in tabular format, so that each value is a number, and values are separated by white space. An example of such a file is numbers1.txt.
Returns the average and standard deviation of the numbers contained in the file. For standard deviation, please use the formula that divides by n-1
(when we have n numbers in our dataset). You can assume that the dataset will contain at least two rows, and each all rows contain the same number of values.
The program should print a line for each column of the data, and that line should follow this format:
Column %d: mean = %.4f, std = %.4f
For example, if numbers1.txt is the input file, the output should look exactly like this:
Column 1: mean = 62.3855, std = 34.5852
Column 2: mean = 66.0211, std = 33.0843
Column 3: mean = 58.7307, std = 29.3490
Column 4: mean = 39.7816, std = 36.0666
Column 5: mean = 56.1361, std = 20.2483
Please place your python code in a file called file_stats.py, and include
that file in your assignment1.zip package. Points will be taken off if the solution does not satisfy the provided commandline specifications.
Task 8 (10 points)
Write a python function result = nth_smallest(data, top, bottom, left, right, n) with these specs:
- The first argument, data, is a two-dimensional numpy array.
- The function returns the n-th smallest value in the subarray of data that goes from row top up to (and including) row bottom, and from column left up to (and including) column right.
For example, consider this code:
import numpy as np
def nth_smallest(data, top, bottom, left, right, n):
# your code goes here
a = np.array([[0.8147, 0.0975, 0.1576, 0.1419, 0.6557, 0.7577],
[0.9058, 0.2785, 0.9706, 0.4212, 0.4212, 0.7431],
[0.127 , 0.5469, 0.9572, 0.9157, 0.8491, 0.3922],
[0.9134, 0.9575, 0.4854, 0.7922, 0.934 , 0.6555],
[0.6324, 0.9649, 0.8003, 0.9595, 0.6787, 0.1712]])
print(nth_smallest(a, 1, 2, 3, 4, 1))
print(nth_smallest(a, 1, 2, 3, 4, 2))
print(nth_smallest(a, 1, 2, 3, 4, 3))
print(nth_smallest(a, 1, 2, 3, 4, 4))
If you implement the nth_smallest function correctly, the above code should print:
0.4212
0.4212
0.8491
0.9157
Please place your python code in a file called nth_smallest.py, and include
that file in your assignment1.zip package.
Task 9 (10 points)
Part a: When the deadline for assignment 5 approaches, a student sends the instructor the following email:
"I really, really need an extension, I have three midterms this week, I do not have time to work on the homework. My homework average is already close to 60, I am afraid of failing the class."
Which of these responses should the student expect from the instructor?
- A: Sure, no problem, you can have an extra week. If you need longer just let me know. I don't even know why I bother to post deadlines, I don't care about them anyway.
- B: No, extensions are not provided. If you do not make the deadline, I am afraid your chances of passing the class will be slim.
- C: No, extensions are not provided except in case of an emergency documented in writing (too much other work, computer/network problems do not qualify). However, remember that you can resubmit (or make late submissions) until Tuesday April 29. For the purposes of making a B, C, or D semester grade, the resubmission score fully replaces the original grade. So, if you do not make the deadline and you get a 0, you can fully replace that score with a resubmission. The only caveat is that the resubmission score will not be considered for the purposes of giving an A grade for the semester.
Part b:
Suppose that instead of "three midterms this week", the reason for the extension request was a computer crash or a network problem. In that case, which of the above three responses should the student expect?
Task 10 (10 points)
A student sends the instructor the following email:
"For assignment X, can I use library Y, which we never discussed in class? That library already seems to implement what you are asking."
Which of these responses should the student expect from the instructor?
- A: Yes, go ahead.
- B: No, using external libraries or toolkits is not allowed.
- C: Use at your own risk. The lectures have provided all the information that you need for your implementation. Your solution should produce the correct outputs for the test inputs that we will use during grading. If you get that done using an existing library, that is fine. If the library produces results that do not match my specifications, you bear the responsibility.
- D: Let me check to verify if that library does exactly what I am asking. If so, I will let you know and you can go ahead.
CSE 4311 -
Assignments - Assignment 1