This portion of the test is worth 30 total points. You may reference any files within your directory, as well as the Internet. 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 test2
under cs120.
Make sure all files you work on today are stored in this directory.
cd cs120 mkdir test2 cd test2
Create a file called question_10.py
. Put your answers to
the following questions there.
(6 points each, 30 points total)
Write a function called compute_discount
. 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 |
---|---|
5 or fewer | 0% |
6 - 10 | 5% |
11 - 20 | 8% |
21 - 25 | 12% |
25 - 30 | 17% |
30+ | 20% |
>>> print(compute_discount(6)) 0.05 >>>
Write a function is_prime
, which 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).
>>> print(is_prime(17)) True >>> print(is_prime(21)) False >>>
Write a function test_arithmetic
. This function
should not take any parameters. Your function should generate two
random (using the random
module) positive integers in
the range [0, 100],
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.
You should not count the question that the user gives up against them.
You answered 0 questions correctly out of 0.
Write a function hash(string)
that 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, 28),
using the remainder operator.
>>> print(hash("This is a test case")) 177 >>>
Using none of the built in String methods, write a function
called strip_chars(string, chars)
, which takes as a
parameter two strings, and returns a copy of the input
string with all of the characters contained in the string
chars removed.
>>> print(strip_chars("This is a test case", "This ")) atetcae >>>
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 cseval.roanoke.edu. You should
see an available assignment called Test 2
.