Functions¶
Here are function descriptions, from simple to more complex, to be used in practicing TDD.
is_zero
- Input: the value to be investigated
- Returns Trueif the input is zero
- Returns Falseif the input is not zero
- If the input is not one number, a TypeErroris raised.
is_zero example implementation
Copied from this tested implementation.
is_even
- Input: the value to be investigated
- Returns Trueif the input is even
- Returns Falseif the input is not even
- If the input is not one integer, a TypeErroris raised.
is_even example implementation
Copied from this tested implementation.
is_odd
- Input: the value to be investigated
- Returns Trueif the input is odd
- Returns Falseif the input is not odd
- If the input is not one integer, a TypeErroris raised.
is_odd example implementation
Copied from this tested implementation.
def is_odd(x):
    """Determine if `x` is odd.
    If `x` is not an integer number, a `TypeError` is raised.
    Returns `True` if `x` is odd
    """
    return not is_even(x)
If the function is_even is absent, here is a stand-alone implementation:
is_probability(p)
- Input: the value to be investigated
- Returns Trueif the input is in the range [0.0, 1.0], that is from and including zero to and including one
- Returns Falseif the input is not in that range
- If the input is not one floating point number, a TypeErroris raised.
is_probability example implementation
Copied from this tested implementation.
def is_probability(x):
    """Determine if `x` is a probability.
    Determine if `x` is a probability,
    i.e. a value between 0.0 and 1.0, including both 0.0 and 1.0.
    If `x` is not a floating point number, a `TypeError` is raised.
    Returns `True` if `x` is a probability
    """
    if not isinstance(x, float):
        msg = "'number' must be a floating point number. "
        raise TypeError(
            msg,
            "Actual type of 'number': ", type(x),
        )
    min_probability = 0.0
    max_probability = 1.0
    return x >= min_probability and x <= max_probability
is_number
- Input: the value to be investigated
- Returns Trueif the input is a number
- Returns Falseif the input is not a number
- If the input is not one element, a TypeErroris raised.
is_number example implementation
Copied from this tested implementation.
are_numbers(x)
- Input: one value (e.g. a list) to be investigated
- Returns Trueif the input is a list of numbers number
- Returns Falseif the input is not a list of numbers
- If the input is an empty list, a TypeErroris raised.
are_numbers example implementation
Copied from this tested implementation.
get_digits
- Input: one number to be split in digits
- Returns the number split into a list of digits, e.g. 314become[3, 1, 4]
- If the input is not one integer, or is negative, a TypeErroris raised.
get_digits example implementation
Copied from this tested implementation.
def get_digits(x):
    """Get the digits of an integer number.
    Get the digits of an integer number,
    for example, '123' becomes '[1, 2, 3]'
    and '0' becomes '[0]'.
    Negative numbers have only their digits collected,
    for example, '-123' becomes '[1, 2, 3]'.
    Will raise TypeError if `x` is not an integer.
    """
    if not isinstance(x, int):
        message = "'x' must be an integer"
        raise TypeError(message)
    zero = 0
    if x < zero:
        return get_digits(-x)
    digits = []
    digits_in_numbering_system = 10
    while True:
        digits.insert(0, x % digits_in_numbering_system)
        if x < digits_in_numbering_system:
            return digits
        x = x // digits_in_numbering_system
get_digits example implementation in a video?
is_roman_numeral
- Input: one string to be checked if it is a roman numeral
- Returns Trueif the input is a roman numeral, e.g. 'I', 'II', 'IV', 'XI', etc.
- Returns Falseif the input is not a roman numeral
- If the input is not one String, a TypeErroris raised.
sum_divisors(x)
- Returns the sum of the proper divisors of the input. For example, the proper divisors of 4 are 1 and 2, as 4 can be divided by both 1 and 2.
| Input | Expected output | 
|---|---|
| 1 | 0 | 
| 2 | 1 | 
| 3 | 1 | 
| 4 | 1+2=3 | 
| 5 | 1 | 
| 6 | 1+2+3=6 | 
| 7 | 1 | 
| 8 | 1+2+4=7 | 
- If the input is not one integer that is at least one, a TypeErroris raised.
is_prime
- Function name: is_prime
- Output:- Returns Trueif the input is a prime number.
- Returns Falseif the input is not a prime number.
- Gives an error when the input is not one number 
 
- Returns 
int_to_roman(x)
- Input: an integer
- Returns The a Roman numeral of the same value as a string, e.g. 'IV'
| Input | Expected output | 
|---|---|
| 0 | [an empty string] | 
| 1 | I | 
| 2 | II | 
| 3 | III | 
| 4 | IV | 
| 5 | V | 
| 6 | VI | 
| 7 | IX | 
- If the input is not one integer that is at least zero, a TypeErroris raised.
roman_to_int(x)
- Input: a Roman numeral,
  e.g. IV
- Returns: the integer value of this Roman numeral
| Input | Expected output | 
|---|---|
| I | 1 | 
| II | 2 | 
| III | 3 | 
| IV | 4 | 
| V | 5 | 
| VI | 6 | 
| IX | 9 | 
- If the input is not one valid roman numeral, a TypeErroris raised.
Too easy? Try Project Euler
Go ahead and write a function to solve a question at Project Euler.
Other example implementations¶
are_strings
- Function name: are_strings
- Output:- Returns Trueif the input is one or more strings.
- Returns Falseotherwise.
 
- Returns 
are_strings example implementation
Copied from this tested implementation.
check_are_strings
- Function name: check_are_strings
- Input: the value to be checked
- Output:- Returns nothing
- Raises an exception when the input is not one or more strings
 
check_are_strings example implementation
Copied from this tested implementation.
check_different
- Function name: check_different
- Input: the two values to be compared
- Output:- Returns nothing
- Raises an exception when the two input values are different
 
check_different example implementation
Copied from this tested implementation.
check_equal example implementation
Copied from this tested implementation.
check_is_number example implementation
Copied from this tested implementation.
check_is_probability example implementation
Copied from this tested implementation.
is_string example implementation
Copied from this tested implementation.
divide_safely example implementation
Copied from this tested implementation.
is_dividable_by_three example implementation
Copied from this tested implementation.
def is_dividable_by_three(x):
    """Determine if `x` is dividable by three.
    If `x` is not an integer number, a `TypeError` is raised.
    Returns `True` if `x` is dividable by three
    """
    if not isinstance(x, int):
        msg = "'number' must be a number. Actual type of 'number': "
        raise TypeError(
            msg, type(x),
        )
    return x % 3 == 0
is_string example implementation
Copied from this tested implementation.
flip_coin example implementation
Copied from this tested implementation.
roll_dice example implementation
Copied from this tested implementation.
sum_primes example implementation
Copied from this tested implementation.
def sum_primes(num):
    """Calculate the sum of all primes up to the given number."""
    if not isinstance(num, int):
        message = "'num' must be an integer"
        raise TypeError(message)
    primes = []
    two = 2
    if num > two:
        primes.append(two)
    elif num == two:
        return(two)
    else:
        return(0)
    for i in range(3, num+1, 2):
        not_prime = False
        for j in range(1, i):
            if i%j == 0 and j != 1:
                not_prime = True
                break
        if not not_prime:
            primes.append(i)
    return sum(primes)