A document from MCS 275 Spring 2022, instructor David Dumas. You can also get the notebook file.

MCS 275 Spring 2022 Homework 10

  • Course Instructor: David Dumas

Instructions:

  • Complete the problems below, which ask you to write Python scripts.
  • Upload your python code directly to gradescope, i.e. upload the .py files containing your work. (If you upload a screenshot or other file format, you won't get credit.)

Deadline

This homework assignment must be submitted in Gradescope by Noon central time on Tuesday 29 March 2022.

Collaboration

Collaboration is prohibited, and you may only access resources (books, online, etc.) listed below.

Resources you may consult

These things might be helpful while working on the problems. Remember that for worksheets, we don't strictly limit what resources you can consult, so these are only suggestions.

Point distribution

This homework assignment has two problems. The grading breakdown is:

Points Item
2 Autograder
4 Problem 2
4 Problem 3
10 Total

The part marked "autograder" reflects points assigned to your submission based on some simple automated checks for Python syntax, etc.. The result of these checks is shown immediately after you submit.

What to do if you're stuck

Ask your instructor or TA a question by email, in office hours, or on discord.

No problem 1 as usual

Problem 2: RGB channel separator

Write a program that reads a color bitmap image (mode RGB) from a file, whose name is given as the first command line argument. It should then create three image files, red.png, green.png, and blue.png of the same size as the input file. Each of these should be a grayscale (mode L) image, with contents as follows:

  • The brightness of the pixel at (x,y) in red.png is equal to the red component of the pixel (x,y) in the input file
  • The brightness of the pixel at (x,y) in green.png is equal to the green component of the pixel (x,y) in the input file
  • The brightness of the pixel at (x,y) in blue.png is equal to the blue component of the pixel (x,y) in the input file Thus, for example, if the pixel at (20,22) in the input file has color (8,220,91), then the pixel in red.png at (20,22) has brightness 8, the pixel in green.png at (20,22) has brightness 220, and the pixel in blue.png at (20,22) has brightness 91.

Here is a sample of what the output would look like. If the input image is: sunflower

Then:

  • red.png would be: sunflower

  • green.png would be: sunflower

  • blue.png would be: sunflower

Save your program as hwk10prob2.py and upload it to Gradescope.

Problem 3: Explain three lines of code

For this quiz problem, you need to add explanatory comments to a given code sample.

In [16]:
# Undocumented sample
import numpy as np

A = np.arange(35).reshape(5,7)
print(A[:,2])
print(A[4,3])
[ 2  9 16 23 30]
31

Here is a documented version of the same program, where every line of code is explained by a comment above it.

In [86]:
# Documented sample
import numpy as np

# Arrange the integers 0...34 in a matrix
# with 5 rows and 7 columns.  Square each
# one and add 10.
A = np.arange(35).reshape(5,7)**2 + 10

# Print column 2 of the matrix
print(A[:,2])

# Print the average (arithmetic mean) of 
# all the entries of the matrix
print(np.mean(A))
[ 14  91 266 539 910]
401.0

Notice that each comment is detailed enough that if you were only given the contents of the comment, you would be able to re-create the corresponding code. (In normal programming practice you wouldn't use this many comments, nor include this much detail, but I am showing what is expected in this problem.)

Add the same kind of explanatory comments to the following program:

In [ ]:
# document this code
import numpy as np

B = np.linspace(21,30,10).reshape(5,2).T + np.array([5,50,500,5000,-1])

print(np.sum(B[:,2:-1]))

print(np.sum(np.exp(-0.1*B[1])))

Put your commented version of this in a file called hwk10prob3.py and upload it to gradescope.