CS 150 - Test Project 1
Due: Wednesday 4/5 at 8am
A test project is an assignment that you complete on your own, without
the help of others. It is a take-home exam. You may use the textbook,
your notes, your previous assignments, the notes and examples on the
course web page and the Python library documentation (linked on the
course web page), but use of any other source is not
allowed. You may not discuss these problems with anyone
except the course instructor. You may only ask the tutors for help
with hardware problems or difficulties in retrieving or submitting
Complete each of the three problems below, each as a separate program,
in a separate file. You are encouraged to reuse code from your
assignments or our class examples. Partial credit will be awarded, so
if you can't solve a whole problem, get as far as you can.
Note that each problem is worth 31 points, 20 of which are for
functionality, and 11 for style. Be sure to write clean code with
good comments and docstrings in order to get all the style points.
Improving the programs and extra points
If you do everything that is stated below, the maximum number of
points you can get is 93 (corresponding to a grade of A). In order to
get the full credit of 100, you must implement some extra features for
at least two of the problems. I've provided some ideas below, but you
should feel free to exercise your creativity. You may receive at most
4 extra points for any given program, with a total of at most 7 extra
For all additions, include in the comments at the top of the file
what you've added or you may not get credit.
Problem 1: GPA calculator
As you talk to some of your friends around campus, you realize there
is a real need for a program that calculates
student's GPA both for their current grades as well as for their
overall time here at Middlebury. When you run the program:
For example, here is a sample transcript from the program running:
- The user is first prompted to enter how many semesters they've
been here (not including the current one), and their GPA so far.
- The user is then prompted to enter grades for their four courses
for the current semester.
- The program outputs the GPA for this semester, as well as
- a revised version of their cumulative GPA that includes this semester.
You must write the following functions:
***** GPA Calculator *****
How many semesters have you been at Middlebury? 4
What is your GPA so far? 3.0
Enter grade for course 1: A
Enter grade for course 2: B
Enter grade for course 3: b
Enter grade for course 4: A
------ Results ------
Your GPA for this semester is: 3.5
Your new cumulative GPA is: 3.1
Possible program extensions:
Include support for "+" or "-" grade variations (without code
duplication, i.e., you may not simply add more elif
statements for every combination); prompt the user for an optional 5th
course; prompt the user for the number of courses; have the user enter
a target GPA and calculate what grade average they must achieve on
average for the remaining semesters to achieve this.
- letter_to_gpa which takes a letter grade as a parameter
and returns the GPA equivalent of that letter grade. The user function
must handle both uppercase and lowercase letters, but it does not need
to handle "+" or "-" variations of the grades.
- semester_gpa which takes no parameters. This function
should prompt the user to enter their grades for their current courses
(like above) and then should return the GPA for this semester. To get
full credit you must use a for loop.
- cumulative_gpa which takes three parameters: (1) the old
GPA of the courses taken so far, (2) the number of semesters the
student has been at Middlebury (not including the current semester),
and (3) the GPA of the current semester. The function should return
the new cumulative GPA by combining the GPA from the previous
semesters with the current semester. Make sure to compute a weighted
average to account for the number of previous semesters.
Problem 2: Word facts
Write a program that prompts the user to enter a word and then
displays the following facts about the word:
- the number of letters in the word
- the number of vowels in the word
- whether the word is a palindrome, i.e., reads the same forwards and backwards
- the first and the last letter, capitalized
Enter a word: APPLE
Apple has 5 letters in it, 2 of them vowels;
it is not a palindrome;
it starts with 'A' and ends with 'E'.
To receive full credit you must:
Enter a word: Racecar
Racecar has 7 letters in it, 3 of them vowels;
it is a palindrome;
it starts with 'R' and ends with 'R'.
Possible program extensions: print out another fact about the
word; print out all the unique consonants in the word; allow the user
to repeatedly enter words until some stopping criteria is reached;
check whether the word is a valid English word (you can use the file
"english.txt" from the class examples on March 15.
- Follow the output language and format above exactly, for
instance, capitalize the word at the beginning, and capitalize the
first and last letter reported.
- Make sure your program works for any capitalization of the input
word (there is a simple way to do this, think about it).
- Write a function called count_vowels that takes a word
as parameter and returns the number of vowels in the word.
- Write a function called is_palindrome that takes a word
as a parameter and returns a Boolean indicating whether or not the
word is a palindrome. A palindrome is a word that is spelled the same
way forwards and backwards (e.g., EYE, Anna, deleveled and
testset). There are a number of ways to answer this question, so think
about what would be the easiest to program.
- Write at least one other function.
Problem 3: Visualizing pi
In class on March 6 we discussed how to approximate the value of π
using Monte Carlo simulation. Here we explore this idea again,
this time by generating random points in a 2 x 2
square. If we count what percentage lies inside a circle with
radius one inside this square, one gets an approximation
Write a program that visualizes this approach using turtle
graphics. The program should draw the outer square, the inscribed
circle, and then generate random points within the square. If the point
lies within the circle it should be one color, and if it lies outside
the circle another color. I strongly encourage you to start from the
code we looked at in class and then add on from there.
I encourage you to define additional functions to structure your code
neatly. Be careful not to use variable or parameter names that are
also functions. For instance, "color" is one of the turtle functions.
Use the following code as the "main program" after your functions
- Implement a function called visualize_pi takes as input
two parameters, the radius of the circle and the number of samples to
be drawn. Besides defining the size of the circle, the radius should
also be used to define the dimensions of the outer bounding box and
the range of the random points.
- Implement a function called draw_circle that you can use
both for the small points, and the large circle. For full credit, the
function should take five parameters: the (x, y) coordinates of the
center of the circle, the radius of the circle, a string
representing the color, and a Boolean representing whether or not the
circle should be filled.
tracer(0, 0) # no animation
# call your function visualize_pi here
Here is a sample image resulting from a call to visualize_pi with
a radius of 150 and 300 points:
Possible program extensions: add some text to the turtle
printout (look at the documentation for how to do this), for example,
print out the approximation of pi or add a title; make the picture
In addition to your code for this problem, you must also submit a
screenshot of an example run. See the instructions for Lab 2 for a
reminder on how to get a screen capture.
When you're done
You should have three separate files/programs, one for each of the
programs above. Each program should run when you hit the green
triangle button, that is, after all of your function definitions you
should have a few lines of code (or just one) that calls your
main function(s) to set everything in motion.
As always, make sure that each program is properly commented:
- You should have comments at the very beginning of each file
stating your name, course (including section number), test project
number and problem number, and the date.
- Each function should have an appropriate docstring
- Other miscellaneous comments to make things clear
Submit each of your files digitally using the online submissions
script. Note that the submission script lets you upload one file for
each problem, and also a screenshot for problem 3. Be careful to
upload the correct files!
Style/comments (per program) 11 * 3
User input and program output 6
output format 3
start/end letter 2
additional function 3
outer square 3
large circle 2
random points 4
colored appropriately 5
Extra points (max 4 / program) 7
total 33 + 60 + 7 = 100