This portion of the test is worth 30 total points. You may reference any files within your directory, as well as the python documentation and the course webpage. As usual, you are not allowed to directly copy any code from the Internet or another individual. You should also follow all coding standards discussed thus far.
Create a directory called test2
under cs120.
Make sure all files you work on today are stored in this directory.
cd cs120 mkdir test2 cd test2
(6 points)
Write a function
called compute_discount(number_of_items)
in a file
called question_10.py. This function takes an amount of
items purchased, and returns a floating point value representing
their discount percentage. You can compute the percentages using
the following table:
Number of Items | Discount |
---|---|
3 or fewer | 0% |
4 - 8 | 5% |
9 - 15 | 8% |
16 - 20 | 12% |
21 - 29 | 17% |
30+ | 20% |
>>> print(compute_discount(6)) 0.05 >>>
(6 points)
Write a function test_arithmetic()
in a file
called question_11.py. This function should not take any
parameters. Your function should generate two random positive
integers in the range [0, 100] (using
the random
module), and ask the user to type in the sum
of the two numbers generated. This process should continue until
the user gets a question wrong, or the user enters
a -1. Your function
should print a statement of the form:
You answered 4 questions correctly out of 5.
The question the user quits on should not be added to the total.
You answered 0 questions correctly out of 0.
(6 points)
Write a function hash(string)
in a file
called question_12.py. This function takes a string
parameter, and returns the normalized sum of the ASCII values
of the characters in the parameter string. In this case,
consider a sum normalized if the sum is restricted to the
range [0, 26), using
the remainder operator.
>>> print(hash("This is a test case")) 49 >>>
(6 points)
Write a function is_prime(test_integer)
in a file
called question_13.py. This function takes a single
parameter: an integer greater than 1. This function should return
True if and only if the parameter is
prime. It should return False in all
other instances. Recall that a number n
is prime if is not divisible by any number in the
range [2, n).
If you noticed, I only said "…if is not divisible by any number…" That's because the condition is sufficient, but not exactly necessary. Modify your code so that it only checks the necessary cases.
(2 bonus points)
>>> print(is_prime(17)) True >>> print(is_prime(21)) False >>>
(6 points)
Using none of the built in String methods, write a function
called and_chars(string, chars)
(in a file
called question_14.py), which takes as a parameter two
strings, and returns a copy of the input
string with only the characters contained in the string
chars left.
>>> print(and_chars("This is a test case", "this ")) his is tst s >>>
Vigenère's Cipher is very easy to understand, but is incredibly difficult to break. It took three centuries before anyone was able to break it. This earned its title: le chiffre indéchiffrable (the indecipherable cipher).
Vigenère's cipher is really just an extension of Caesar's Cipher. The main difference is the key that is used. In Caesar, the key is an integer representing the shift. Vigenère used letters to specify the shift. In fact, Vigenère's key is an entire word. The index of the letter in the Alphabet represents the shift performed using Caesar's cipher.
The added security comes from the fact that now each letter of the plain text is getting encrypted using different shift amounts, being specified by the key word. Consider the key "dawn". Using this key, you would encrypt the first letter of the plain text, performing Caesar with the key 3. Then, you would encrypt the second letter with the key 0. The third letter gets encrypted with the key 22. The fourth letter gets encrypted with the key 13. Then the cycle repeats: letter five uses the key 3, letter six uses the key 0, letter seven uses the key 22, and letter eight uses the key 13, and so on.
Create a file called bonus_question.py
. In this file,
write a function called encode_vigenere
, which takes two
parameters: plaintext and key, both strings.
Your function should return a string, the Vigenère encoding of the
plain text using the specified key.
(8 points)
When you have finished, create a tar file of your test2
directory. To create a tar file, execute the following commands:
cd ~/cs120 tar czvf test2.tgz test2/
To submit your activity, go to inquire.roanoke.edu. You should
see an available assignment called Test 2
.