import math


def compute_idfs(documents):
    """
    Given a dictionary of `documents` that maps names of documents to a list
    of words, return a dictionary that maps words to their IDF values.

    Any word that appears in at least one of the documents should be in the
    resulting dictionary.
    """
    word_idf = dict()

    # get all the words from all the documents
    all_words = []
    for words in documents.values():
        for word in words:
            all_words.append(word)

    # calculate idf of each word
    total_document = len(documents.keys())
    for word in all_words:
        n_document = 0
        for document in documents.keys():
            try:
                if word in documents[document]:
                    n_document += 1
            except KeyError:
                n_document = 1

        idf = math.log(total_document / n_document)
        word_idf[word] = idf

    return word_idf