from math import log


def idf(term, corpus):
    """
    computes inverse document frequency. IDF is defined as the
    logarithm of the total number of documents in the corpus over the
    number of documents containing the search term:
    log(all documents/documents containing the search term)

    Note that if *no* document contains the search term, it would result
    in a division by zero. This is mitigated by adding 1 to the
    denominator in that case.

    Parameters:
        term: a string containing the search term
        corpus: a list of lists; the outer list is the corpus, while the
            inner lists should represent the document texts, split into
            tokens (make sure that punctuation is split, too!)

    Return Value: a float representing the idf value
    """
    documents_with_term = 0
    for document in corpus:
        for token in document:
            if token == term:
                documents_with_term += 1
                break
    try:
        return log(len(corpus)/documents_with_term)
    except ZeroDivisionError:
        return log(len(corpus) / 1 + documents_with_term)