Week 2#

Writing reliable and understandable code#

The goal of this weeks exercises is to give you some exercise with testing and documentation.

Exercise 1) Checking primes#

Write a function is_prime(n) that takes in a positive integer and returns a boolean saying if the input is a prime or not. The function should raise an error if the input is invalid.

Add a descriptive docstring to your function. Now write a few unit tests for your function using pytest. Your tests should test a few different numbers, especially 1, 2, and 3. Also write a test to verify that your function raises exceptions as expected (use pytest.raises, read more here under ‘Assertions about expected exceptions’).

Exercise 2) Writing Bubble Sort#

You will now write a function implementing the “bubble sort” algorithm, which is one of the simplest sorting algorithms. Assuming we have a list with elements we can compare using a “less than” operator. We want to sort the list in increasing order, so for example:

  • [4, 2, 3, 7, 1, 5] -> [1, 2, 3, 4, 5, 7]

The steps of bubble sort are as follows:

  1. Start with the first element in the list, compare it with the second.

    • If they are in the wrong order, exchange them.

  2. Now compare the second and third element

    • If they are in the wrong order, exchange them.

  3. Proceed in the same manner, comparing the third and fourth, then the fourth and fifth, etc

At this point, after comparing the second to last and the last elements and flipping if necessary, the largest element of the list will be at the end of the list (Why is this?).

Now, start again from the beginning, but stop once you reach the second-last element in the list. Then start again from the beginning, but stop once you reach the third-last element and so on, untill the whole list is sorted.

If you need a better description, you can read more about bubble sort on wikipedia where they also have pseudocode examples.

Write a bubble_sort function that takes in a tuple or list and returns a new list with the same data in sorted order. The function should not change the original list. Include a docstring with your function

Exercise 3) Testing Bubble Sort#

You should now write a few unit tests of your bubble sort function. Include at least these tests:

  • An empty list should return an empty list.

  • A list with just one element should return a list with just that element.

  • Try sorting a few example inputs and check the output is sorted.

  • Verify that the input list is unchanged.

If any tests fail, go back and improve your bubble sort.

Exercise 4) Testing and correcting a median function#

The following function finds the median of a given dataset:

def median(data):
    """Returns the median of a dataset."""
    data.sort()
    return data[len(data)//2]

print(median([11, 3, 1, 5, 3]))
3

The function returns the correct answer for the example given, but is the function any good? To find out, implement the following tests and run them. If any of the tests fails, improve the function so that it passes:

  • A test checking the output for a one-element list

  • A test checking the output for a two-element list

  • A test ensuring the original data is unchanged

  • A test checking that an error is raised if the input is an empty list/tuple.