Midterm Exam 1A for 15-110 Course at Carnegie Mellon University, Study notes of History

The midterm exam 1A for the 15-110 course at Carnegie Mellon University, which covers the basics of Python programming, data structures, and algorithms. The exam includes multiple-choice questions, short answer questions, and programming problems related to Python functions, binary search, and sorting algorithms.

Typology: Study notes

2021/2022

Uploaded on 08/05/2022

dirk88
dirk88 🇧🇪

4.4

(222)

3.1K documents

1 / 15

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Midterm Exam 1A Answer key
15110 Principles of Computing Spring 2015
February 18, 2015
Name:
Andrew ID: Lab section:
Instructions
Answer each question neatly in the space provided.
There are 7 questions totaling 34 subproblems on 15 pages. Not all problems are the same
size or difficulty.
Please read each question carefully. You have 50 minutes for this exam. No electronic devices
allowed. Good luck!
Max Score
1 6
2 16
3 20
4 21
5 22
6 10
7 5
Total: 100
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Midterm Exam 1A for 15-110 Course at Carnegie Mellon University and more Study notes History in PDF only on Docsity!

Midterm Exam 1A Answer key

15110 Principles of Computing Spring 2015

February 18, 2015

Name:

Andrew ID: Lab section:

Instructions

  • Answer each question neatly in the space provided.
  • There are 7 questions totaling 34 subproblems on 15 pages. Not all problems are the same size or difficulty.
  • Please read each question carefully. You have 50 minutes for this exam. No electronic devices allowed. Good luck!

Max Score 1 6 2 16 3 20 4 21 5 22 6 10 7 5 Total: 100

Questions

  1. This question deals with history of computing devices.

(a) (2 points) What is the significance of the invention of integrated circuits in the history of computing?

Solution: An integrated circuit is a collection of several circuits (transistors) packed on a single chip. Their invention led to increased reliability, reduced cost (because they could be mass manufactused) and reduced size of the computers.

(b) (2 points) Moore’s Law says that the number of integrated circuit chips in a computer doubles every 2 years, which implies that computers become twice as powerful every 2 years. According to Moore’s Law, how many years from now will computers be 32 times more powerful as they are now?

Solution: 32 = 2^5 , which implies 5 doublings, 10 years.

(c) (2 points) A Gigabyte (GB) is 2^30 Bytes and a Kilobyte (KB) is 2^10 Bytes. If you have a storage device with a capacity of 16 GB, how many 32KB files can you fit in that device? Express the result as a power of 2.

Solution: 16 GB = 2^4 * 2^30 32 KB = 2^5 * 2^10 Answer: 2^34 / 215 = 2^19

(c) (4 points) Assume the following list definition in Python.

letters = ["a", "b", "o", "c", "p"] What would be displayed in a Python shell for each of the following expressions if they are evaluated in the given order? If it would give an error then write error. letters[1]

________________

letters[len(letters)-2]

_________________

letters + ["x"]

__________________

letters

__________________

Solution: "b" "c" ["a", "b", "o", "c", "p","x"] ["a", "b", "o", "c", "p"]

(d) (2 points) Show how to create a list of every integer between 0 and 100 , inclusive, named nums1 using Python, sorted in increasing order.

nums1 = list(_______________________________________)

Solution: range(0,101) or range(101)

(e) (2 points) Let nums2 and nums3 be two non-empty lists. Write a Python command that will append the last element of nums3 to the end of nums. ________________.append(_____________________).

Solution: nums2.append(nums3[len(nums3)-1]) OR nums2.append(nums3[-1])

  1. This question focuses on the basics of Python functions and tracing.

(a) (3 points) In economics, the percentage rate of inflation for a period of time is calculated based on the final value F of a commodity and the initial value I of the commodity, using the formula ((F − I)/I) × 100. Write a Python function inflation rate(initial, final) to compute and return the inflation rate given the initial and final values of a commodity.

Solution: def inflation_rate(initial,final): return ((final - initial)/ initial) * 100

(b) (4 points) Using the function from part (a), write a Python function average inflation rate(), that computes and returns the average rate of inflation for the 3-year period represented in the table below: Year Initial value Final value 1 23.50 24. 2 24.00 24. 3 24.25 24. The function is required to call the function from part (a).

Solution: def average_inflation_rate(): year1 = inflation_rate(23.50,24.00) year2 = inflation_rate(24.00,24.25) year3 = inflation_rate(24.25,24.38) return (year1 + year2 + year3)/

(c) (3 points) Consider the simple function given below. def twice(n): print(2*n) When we compare twice(5) with 10 the Python interpreter would return False after printing 10 as shown below. Explain why it gives False as a result of the comparison.

twice(5) == 10 10 False

Solution: Since 2∗n is printed, not returned, twice(5) would return None. Comparing a None value to an integer would yield False.

(d) (6 points) Consider the following Python function where m and n are assumed to be a positive integers: def mystery(n, m): p = 0 e = 0 while e < m:

e = 0 while e < m: p = p + n e = e + 1 return p

Solution: 4

  1. This question focuses on searching.

(a) (5 points) Below is a Python function that takes a list of integers nums and an integer divisor as inputs, and searches for a number in the list that is evenly divisible by divisor. It returns the index of the first occurrence of such a number, or None if there is no such number. For example, when the function is called with [101, 4, 12, 24] for nums and 2 for divisor it should return 1. This is because 4 is the first item in the list that is divisible by 2. Complete the missing parts of the function. def first_divisible(nums, divisor):

for __________________________________:

if ____________________________________:

return ______________________________

return _______________

Solution: def first_divisible(nums, divisor): for i in range(len(nums)): (1 point) if nums[i] % divisor == 0: (2 points) return i (1 point) return None (1 point)

Assuming that the list vals is [100, 66, 55, 64, 41, 35, 18, 64], write the output from each of the following calls to first_divisible. (b) (2 points) first_divisible(vals, 9)

Solution: 6

(c) (2 points) first_divisible(vals, 101)

Solution: None

(d) (2 points) How many times would the for-loop iterate if we evaluated first_divisible(list(range(10, 0, -1)), 12)?

Solution: 10

(f) (2 points) Supposing the length of a list vals is fixed at n elements, what kind of list contents would result in the longest running time when evaluating first_divisible(vals, 2)?

Solution: A list containing only odd numbers.

(g) (2 points) Supposing the length of a list vals is fixed at n elements. What kind of list contents would result in the shortest running time when evaluating first_divisible(vals, 2)?

Solution: A list with an even number as its first element.

(h) (1 point) Assuming the input list has length n, give the big-O notation for the worst-case time complexity for first_divisible.

Solution: O(n)

  1. This question focuses on searching and sorting.

(a) (6 points) Recall that the recursive binary search algorithm we studied in class works with a range to search defined by two indexes: lower and upper. The value of lower is one less than the index of the first element in the range, and the value of upper is one greater than the index of the last element in the range. Finally, the algorithm also computes the midpoint mid = (lower + upper) // 2. Suppose we do a binary search in the list [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32] for the value 23. Complete the table below to show how the values of lower, upper, and mid change during the search, until and including the point where the base case is reached. (You should not need all the space provided.)

lower upper mid

-1 17 8

Solution: Final value of mid is optional. lower upper mid

-1 17 8 8 17 12 8 12 10 10 12 11 11 12 (11)

(b) (2 points) Give the big-O notation for the worst-case time complexity of binary search of a list of n elements.

Solution: O(log n)

(g) (2 points) Suppose we sort a list of 8 elements using mergesort. There will be eight single- element lists to be pairwise merged. List the other merge operations needed to complete the mergesort, giving the list sizes and number of lists of each size. For instance, you might say “five 3-element lists and two 5-element lists” (except that would be wrong!)

Solution: four 2-element lists and two 4-element lists

  1. This question deals with correctness of functions and testing.

(a) (2 points) Complete the following function definition so that it finds the first pair of adjacent items in the input list that are in descending order, and returns the index of the first element of the pair. If there is no such pair, it returns None. Example:

search_pair([1,3,4,6,4,7,6]) 3 Notice that 3 is the index of 6, which is the first element of the first pair in descending order. (It may be helpful to remember that range(n)is empty, if n is 0 or less, and a for-loop on an empty range skips the loop body.) def search_pair(items): for i in range(__________________________________): assert i+1 < len(items) if items[i] > items[i + 1]: return i return None

Solution: def search_pair(items): for i in range(len(items) - 1): assert i+1 < len(items) if items[i] > items[i + 1]: return i return None

(b) (2 points) Explain in one sentence what purpose the assert statement serves in this function.

Solution: The least informative acceptable answer is: to stop the program if i + 1 ≥ the length of the input list. The best answer is: to check that the list index i + 1 is a legal index, and to document for the reader that list index i + 1 is ok to use in the loop.

(c) (6 points) Below is a test function for search_pair. It only tests two kinds of input. Add three test cases: one for an empty list, one for a list containing only one element, and one for a list where the only two adjacent elements in descending order are the last two. def test_search_pair(): assert search_pair(list(range(10))) == None

assert search_pair([4, 5, 4, 5]) == 1

print("Test complete")

Solution:

assert search_pair([]) == None assert search_pair([1]) == None assert search_pair([5, 5, 4]) == 1