Appendix M — Midterm Exam Questions


Instructions


M.1 Question 1 (2 Marks)

Write a function which computes the roots of a 2nd-order polynomial of the form \(ax^2 + bx + c = 0\), and returns the two values as tuple (the order does not matter). The function should accept the three coefficients as a, b and c, and it must be able to handle the case of complex roots. The quadratic formula can be used to find both roots:

\[ x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a} \]

Hint: A square root can be found using \(b^0.5\).

Here are some examples of the expected behavior:

Q1(a=1, b=1, c=0)  # Should return (0.0, -1.0)
Q1(a=2, b=3, c=4) # Should return ((-0.75 + 1.2j), (-0.75 - 1.2j))

Notes:

  • Python puts round brackets around complex numbers for clarity, but this does not mean it’s a tuple
  • Do not worry about the number of significant figures on the returned answers

M.2 Question 2 (2 Marks)

Someone has collected data which tracks the peak temperature each day for a month. This data is stored in a python dictionary (dict) with the date as the “key” in the format "MonthName Date, Year" and the temperature as the “value” in units of C. Write a function which accepts this dictionary and computes the average of the values. Assume that the dictionary contains one entry per day for the month.

Here is an example of the expected behavior:

data = {'January 21, 2025': -2, 'January 22, 2025': -5}
Q1(data)  # Should return -3.5

M.3 Question 3 (4 Marks)

In Question 2 a dictionary containing peak temperature values for a given day was used. The dates are in the format "January 10, 2022", but you want them to be in the form "10-01-2022". Write a function which accepts a key from such a dictionary in the form of a str, and returns the date as a str in the format of "DD-MM-YYYY".

Hint: The months, in order are: ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]

date = "July 10, 2025"
Q3(date)  # Should return "10-07-2025"

M.4 Question 4 (3 Marks)

Write a function which accepts two names (as strings) and returns them in alphabetical order. Ensure the names are capitalized before comparing them, since upper and lower case letters are treated differently.

Below are some examples of expected behavior:

Q4('Jimmy Page', 'jimi Hendrix')  # Should return ('Jimi Hendrix', 'Jimmy Page')
Q4('john maclaughlin', 'John Mayer')  # Should return ('John Maclaughlin', 'John Mayer')

M.5 Question 5 (3 Marks)

Write a function which generates a random integer with a specified number of digits. The numbers should not have any leading zeros but trailing zeros are OK.

Q5(4)  # Should return something like 1301 or 9830
Q5(6)  # Should return something like 102338 or 939489

M.6 Question 6 (4 Marks)

Your co-op job is to help develop an autonomous pizza delivery robot. The computer on the robot logs its current speed, but lacks the ability to track the total distance traveled on a delivery. Write a function which computes the total distance traveled (in m) by using the speed values, assuming the speeds was approximately constant between each reading. The speeds are stored in a Python list (in units of m/s) and readings are taken every 1 minute exactly, starting after the first minute has elapsed.

speeds = [3.2, 3.3, 0, 2.4, 3.5, 3.4, 2.2]
Q6(speeds)  # Should return 1080.0, in units of m

M.7 Question 7 (2 Marks)

Write a function with accepts a file name and a string of text, and writes the text to the file with each word on new line.

For instance, the following:

Q7('file.txt', 'here is some text')

Should produce a file like:

here
is
some
text