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
Create a file called question_12.py
. Put your answers to
the following questions there.
(32 points total)
function takes two parameters: a list representing the current value of the correct list, and a list representing the users current guess. This function should return the number of correct guesses the player has made.
>>> print(compute_correct_guesses(['R', 'G', 'R', 'B'], ['R', 'G','Y', 'B']) 3
(6 points)
Write a function remove_spaces
that takes a string as a
parameter, and returns a
new string that is equivalent to the input string with sequences of
neighboring space characters reducted to a single space character.
>>> print(remove_spaces("1 2 3")) '1 2 3' >>> print(remove_spaces("123") '123'
(6 points)
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)]] >>>
(7 points)
Write a function sum_cross
, which takes two parameters:
a square, 2-dimensional list, and a single integer. Your function
should return the sum of the elements along the specified row and
column of the 2-dimensional list.
>>> my_list = [[0, 1], [2, 3]] >>> print(sum_cross(my_list, 0)) 3 >>> my_list_2 = [[0, 1, 2], [3, 4, 5], [6, 7, 8]] >>> print(sum_cross(my_list_2, 1)) 24
(6 points)
Write a function translate_morse_code
, which takes a
single string of upper case characters as a parameter. This
function should return a string,
the parameter string translated to morse code, as defined by the
following table. Each translated letter should be separated by a
space, and spaces should not be translated. You must use the
following dictionary for this question.
{"A":".-", "G":"--.", "M":"--", "S":"...", "Y":"-.--", "B":"-...", "H":"....", "N":"-.","T":"-", "Z":"--..", "C":"-.-.","I":"..","O":"---","U":"..-","D":"-..","J":".---", "P":".--.","V":"...-", "E":".","K":"-.-","Q":"--.-","W":".--", "F":"..-.","L":".-..", "R":".-.", "X":"-..-"}
>>> print(translate_morse_code("HI OH")) .... .. --- .... >>> print(translate_morse_code("HIOH")) .... .. --- .... >>> print(translate_morse_code("SOS")) ... --- ... >>> print(translate_morse_code("hello, world") >>>
(7 points)
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)
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":
6 | 24 | 1 |
13 | 16 | 10 |
20 | 17 | 15 |
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 |
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)
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
.