import math


def item_based_sim(mean_data, movie_data, m1, m2):
    """Returns a similarity using adjusted cosine similarity.

        Parameters
        ----------
        mean_data : dict
            Dictionary of the users mean of ratings

        movie_data : dict
            Dictionary of the data set.

        m1 : str
            Id of movie to be used for similarity measure.

        m2 : str
            Id of movie to be used for similarity measure.

        Returns
        -------
        similarity value : float
            Returns adjusted cosine similarity of m1 and m2.
    """
    joint = set([u1 for u1 in movie_data[m1]]) & set([u2 for u2 in movie_data[m2]])

    if len(joint) == 0:
        return 0

    numer = 0
    denom1 = 0
    denom2 = 0

    for u in joint:
        ra = movie_data[m1][u]
        rb = movie_data[m2][u]

        mean = mean_data[u]

        numer += (ra - mean) * (rb - mean)
        denom1 += math.pow(ra - mean, 2)
        denom2 += math.pow(rb - mean, 2)

    if denom1 == 0 or denom2 == 0:
        return 0

    return round(numer / (math.sqrt(denom1) * math.sqrt(denom2)), 15)