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.
    """

    map_idf = {}

    # Loop through all documents
    for doc in documents:
        # Keep track of added words for each document
        added_words = []

        # Loop through all words
        for word in documents[doc]:

            # If word hasn't been added before, increase score by 1
            if word not in added_words:
                added_words.append(word)
                if word in map_idf:
                    map_idf[word] += 1
                else:
                    map_idf[word] = 1

    # Finally, take natural log of all results and add to new array
    final_idf = {}
    for word in map_idf:
        final_idf[word] = math.log(len(documents) / map_idf[word])

    # Return dictionary of IDF values
    return final_idf