< Back

Final Exam


This portion of the test is worth 20 total points. You may only access the Python documentation website. NO OTHER WEBSITES ARE PERMITTED. As usual, you are not allowed to copy any code from the Internet or another individual. 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 15

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

(20 points total)


Challenge

Create a file called bonus_question.py. In this file, write a function compute_pascal, which takes an integer ≥ 1, and returns the pascal triangle of the specified depth. Pascal's triangle can be represented as a jagged 2-dimensional list, where each row is defined based off the previous row. Row 1 is always defined as [1]. For every subsequent row i, each element j is defined as the sum of elements j and j - 1 from row i - 1. if either j - 1 or j do not exist in row i - 1, substitute the value 0 in their place.

      >>> print(compute_pascal(1))
      [[1]]
      >>> print(compute_pascal(2))
      [[1], [1, 1]]
      >>> print(compute_pascal(6))
      [[1], [1, 1], [1, 2, 1], [1, 3, 3, 1], [1, 4, 6, 4, 1], [1, 5, 10, 10, 5, 1]]
      >>>

(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 inquire.roanoke.edu. You should see an available assignment called Final Exam.