AkeyProgramming

プログラミングの進捗を報告します。ほぼ自分用。プログラミング関係の仕事お待ちしております。

Pyrhonからはじめる数学入門の進捗2

72ページまで写経を進めた。

問題は難しかったから飛ばした。

'''
Calculating the mean
'''

def calculate_mean(numbers):
    s = sum(numbers)
    N = len(numbers)
    # calculate the mean
    mean = s/N

    return mean

if __name__ == '__main__':
    donations = [100,60,70,900,100,200,500,500,503,600,1000,1200]
    mean = calculate_mean(donations)
    N = len(donations)
    print('Mean donation over the last {0} days is {1}'.format(N,mean))
'''
Calculating the median
'''

def calculate_median(numbers):
    N = len(numbers)
    numbers.sort()

    #find the median
    if N % 2 == 0:
        #if N is even
        m1 = N/2
        m2 = (N/2) + 1
        #convert to integer, match position
        m1 = int(m1) - 1
        m2 = int(m2) - 1
        median = (numbers[m1] + numbers[m2])/2
    else:
        m = (N+1)/2
        #convert to integer, match position
        m = int(m) - 1
        median = numbers[m]

    return median

if __name__ == '__main__':
    donations = [100,60,70,900,100,200,500,500,503,600,1000,1200]
    median = calculate_median(donations)
    N = len(donations)
    print('Median donation over the last {0} days is {1}'.format(N,median))
'''
calculating the mode
'''

from collections import Counter

def calculate_mode(numbers):
    c = Counter(numbers)
    mode = c.most_common(1)
    return mode[0][0]

if __name__ == '__main__':
    scores = [7,8,9,2,10,9,9,9,9,4,5,6,1,5,6,7,8,6,1,10]
    mode = calculate_mode(scores)
    print('The mode of the list of numbers is : {0}'.format(mode))
    

まだ簡単ですね