This is an archived version of the course. Please find the latest version of the course on the main webpage.

Chapter 5: Applied problem solving

Smallest number divisible by 7

face Josiah Wang Joe Stacey

Next challenge! Write a function compute_min_divisible() that takes two input arguments: a list of integers numbers and an integer divisor. The function should return the smallest of these integers that is divisible by the given divisor. If none of the integers in the list are divisible by the given divisor, then return None.

You may use any built-in Python functions for this challenge, if needed. This includes min(). You can actually pass a list to min() as an input argument, for example min([4, 7, 6]).

You can also use the sorted() function to return a sorted list. For example, sorted([5, 2, 3, 6]) will return a new list [2, 3, 5, 6].

As usual, test your functions by writing test cases!

Sample inputs and outputs

Example 1

  • Sample function call: compute_min_divisible([66, 33, -55, 11], 11)
  • Returns: -55

Example 2

  • Sample function call: compute_min_divisible([7, 21, 11, 5, 2, 14], 7)
  • Returns: 7

Example 3

  • Sample function call: compute_min_divisible([34, 5, 10, 19, 28, 74], 3)
  • Returns: None

Example 4

  • Sample function call: compute_min_divisible([], 4)
  • Returns: None