def three_stats(N, X):
    """
    Return the mean, median, and mode of X
    """
    # sort X into ascending first (could have writen a sort function, but not the focus here)
    X_sorted = sorted(X)

    # calculate mean by summarizing all numbers
    sum = 0
    # use a dict to store number of appearance for each num
    X_dict = {}
    # store max number of appearance
    max_num_appearance = 0
    mode = X_sorted[0]
    for x in X_sorted:
        sum += x

        X_dict[x] = X_dict.get(x, 0) + 1

        # update mode and max_num_appearance
        if X_dict[x] > max_num_appearance:
            mode = x 
            max_num_appearance = X_dict[x]
        if X_dict[x] == max_num_appearance:
            if x < mode:
                mode = x 


    # calculate median
    if N % 2 == 0: # even
        median = (X_sorted[N//2 -1] + X_sorted[N//2])/2
    else:     # odd
        median = X_sorted[N//2]

    return round(float(sum/N), 1), round(float(median), 1), mode