CPSC 120 B




Lecture
MWF 10:50am - 11:50am
Lab
MWF 12:00pm - 1:00pm

Scotty Smith

Office
Trexler 365-B
Office Hours
Monday / Thursday
3:00pm - 5:00pm
Email
chssmithATroanoke.edu

< Back
Printable Copy

Final Exam


This portion of the test is worth 32 total points. You may reference any files within your directory. You may only access the Python documentation website. NO OTHER WEBSITES ARE PERMITTED. As usual, you are not allowed to directly copy any code from the Internet or another individual without citation. You should also follow all coding standards discussed thus far.

Create a directory called exam under cs120. Make sure all files you work on today are stored in this directory.

cd cs120
mkdir exam
cd exam

Question 12

Create a file called question_12.py. Put your answers to the following questions there.

(32 points total)


Challenge

One very common image transformation is known as a "rotation." Perhaps you took a picture holding the camera the wrong way, and you wish to get a copy of the image rotated 90°. Or perhaps you would just like to trick the work into thinking you are Batman scaling up a wall.

Create a file called bonus_question.py. In this file, write a function called rotate_image. This function takes one parameter: a 2-dimensional list of tuples, where the tuples represent Red, Green, and Blue pixel values from the image. Your function should return a 2-dimensional list, the result of rotating the image right by 90°.

  >>> my_image = [[(0, 255, 0), (0, 255, 0), (0, 255, 0)],
                  [(0, 0, 0), (0, 0, 0), (0, 0, 0)],
                  [(0, 255, 0), (0, 255, 0), (0, 0, 0)]]
  >>> print(rotate_image(my_image))
  [[(0, 255, 0), (0, 0, 0), (0, 255, 0)],
  [(0, 255, 0), (0, 0, 0), (0, 255, 0)],
  [(0, 0, 0), (0, 0, 0), (0, 255, 0)]]
  >>>

(5 points)


Challenge

The Hill cipher is a cryptographic block cipher technique that leverages the usage of linear algebra to compute the cipher text. While the hill cipher requires some knowledge of linear algebra to produce valid keys, given a key it is easy to compute the ciphertext.

The Hill cipher treats all inputs as matricies: The key represents an m × m matrix of integers, where the plain-text block is represented as an m × 1 matrix. To produce the ciphertext, one simply has to perform matrix multiplication on these two inputs, performing all mathematical operations modulo (using the remainder operator) 26.

So, to compute the encryption of the string "CAT", using the key "GYBNQKURP":

6241
131610
201715
×
2
0
19
=
(6 × 2) + (24 × 0) + (1 × 19)
(13 × 2) + (16 × 0) + (10 × 19)
(20 × 2) + (17 × 0) + (15 × 19)
=
31
216
325
=
5
8
13
(mod 26)

Which represents the string FIN.

Create a file called bonus_question_2.py, and implement a function called encrypt_hill. This function takes two parameters: A block (of length m) of the message as a string and a string (of length m × m) that represents the key. Your function should output a string of length m, the encrypted version of the message using the key parameter and the hill cipher algorithm.

  >>> print(encrypt_hill("CAT", "GYBNQKURP"))
  FIN
  >>>

(5 points)



Submission

When you have finished, create a tar file of your exam directory. To create a tar file, execute the following commands:

cd ~/cs120
tar czvf exam.tgz exam/

To submit your activity, go to cseval.roanoke.edu. You should see an available assignment called Final Exam.


Last modified: Wed Dec 11 16:08:03 EST 2013