Intro to Programming with Python - Midterm Programming
Intro to Programming with Python - Midterm Programming
Student ID:
Instructions:
• Please read each question carefully. You have a total of 120 minutes to complete the
programming part of the exam. You must stop working on the exam and submit your work
by 12:00PM. Otherwise, your test will not be graded. After 9:10AM, the Internet will be
shut down.
• Make sure you have your name on the submitted files as comment lines.
• The programming part of the exam is open book open note exam, no Internet access, no
personal computer.
• You’re not allowed to leave the exam room for any reason. If you need to use the bathroom,
notify the exam proctor by remaining seated and raising your hand. Your proctor will grant
you permission.
• Academic honesty is required in all work you submit to be graded. Any form of cheating
will result in a 0 (zero) for the midterm score.
• You are allowed to bring drafting paper and pencil. We will provide drafting papers.
• Late submission of exam without an approved extension will not be graded.
• You will conduct your exam on a lab computer. Your personal laptop is NOT allowed.
• You should upload your .py file(s) to the Canvas (under Assignments>Midterm Exam (Pro-
gramming Part)) before the end of the exam session. Submit separate .py file for each
problem with the following naming format, for example, M idterm_P 1.py. Note: The only
supported file extension is py.
• Late submissions without an approved extension will not graded at all. You can resubmit
the file multiple times. Your last submission will be graded.
• Good luck!
About the exam: There are three problems with 100 points total, and some have multiple parts.
• We highly recommend you look over the entire exam before getting started so that you can budget
your time well.
• Please type up your work and write down your ideas by coding, even if you get stuck on a prob-
lem and your program does not work. We can give you partial credit as long as we can understand
what you wrote.
• In this exam, you do NOT need to comment on your functions or every line of code. We will NOT
be grading on coding style.
• If you don’t remember how to use a function or method, try running help(functionName),
help(str.methodName) or help(list.methodName).
- (5-pts) Print out the number of characters and the number of words in this sentence on a single
line using the following format:
"Number of characters is _ and number of words is _"
- (10-pts) Use the function randrange in the module random to pick a random position in the sen-
tence. For example, if the sentence has 8 words, you can call: random.randrange(0,7) to generate
a random position in the sentence. Print "Random position picked: _"
Prompt user to input a string using the prompt: "Enter a string: ". Capitalize the string, and
insert it at the random word position in the sentence with quotation marks ("") around it. Print the
new sentence.
Hint: You can split the string into list of words, and use random.randrange(start,stop) to get a
random integer in that range.
- (10-pts) Remove the quotation marks. Print the final sentence with the ’-’ character replacing
spaces. Hint : You can use a string method str.replace() to replace a character with a specific
character of your choice. Use help(str.replace) to get help on how the function can be used.
Assume the input sentence and string contain only letters (a-z and A-Z), and spaces. The program
output should be similar to the following example:
The output needs to be formatted exactly as it is shown in the example; otherwise, up to 5 pts will be
deducted.
For this problem you need to write down a program for the grading system. Start by asking for the number
of students with the prompt: "Enter the number of students: ".
Using the number of students, ask for the user to input each student’s name and score with the
prompt:
"Enter the name for student number _: ".
"Enter the score for student number _: ".
Save the students into a Python dictionary name it grade_dict whose keys are students’name, values
are students’ letter grade. It should look something like this: ’Mike’:’A’, ’Minh’:’B’,’Lan’:’C’. Of
course, for a dictionary, we need dictionary’s keys to be unique, but we ignore that for now. Print out the
object grade_dict using the function print().
Finally, print out the number of students for each letter grade with the format:
"The number of students in Grade _ is: _".
Assume the user input will always be a number between 0 and 100. The program output should be similar
to the following example:
Part 3(b) (20pts): The Fibonacci Sequence is a series of integer numbers. The next number is found by
adding up the two numbers before it. The first two numbers are 0 and 1. For example: 0, 1, 1, 2, 3, 5,
8, 13. The next number in this series above is 8+13 = 21. Write a function named Fibonacci() that takes
an integer input n. The function computes the nth Fibonacci number in the sequence above.
Hint: The number swapping trick when we learn about tuple can be useful.