import math


def entropy(classes: list, instances: list):
    """
    function to calculate the entropy of the given instances

    :param classes: is a one-dimensional list containing the class names
    :param instances: is a two-dimensional list where each row respresents
            one attribute(in the order of 'attributes') and the possible values
    :return: entropy of our instances, classes with absolute frequency in instances
    """
    # extract last column containing the classes
    if len(instances) == 0:
        return 0, []
    instances = [row[-1] for row in instances]
    numclassinstances = []
    num_instances = len(instances)
    result = 0.0
    for dclass in classes:
        classfrequency = [dclass, sum(inst[0] == dclass[0] for inst in instances)]
        pfraction = classfrequency[-1]/num_instances
        if pfraction != 0:
            numclassinstances.append(classfrequency)
            result -= pfraction * math.log(pfraction, len(classes))
    return result, numclassinstances