1# Calculating the mode when the list of numbers may have multiple modes
2from collections import Counter
3
4def calculate_mode(n):
5 c = Counter(n)
6 num_freq = c.most_common()
7 max_count = num_freq[0][1]
8
9 modes = []
10 for num in num_freq:
11 if num[1] == max_count:
12 modes.append(num[0])
13 return modes
14
15# Finding the Mode
16
17def calculate_mode(n):
18 c = Counter(n)
19 mode = c.most_common(1)
20 return mode[0][0]
21
22#src : Doing Math With Python.
1import random
2numbers = []
3limit = int(input("Please enter how many numbers you would like to compare:\n"))
4mode =0
5for i in range(0,limit):
6 numbers.append(random.randint(1,10))
7
8maxiumNum = max(numbers)
9j = maxiumNum + 1
10count = [0]*j
11for i in range(j):
12 count[i]=0
13
14for i in range(limit):
15 count[numbers[i]] +=1
16
17n = count[0]
18for i in range(1, j):
19 if (count[i] > n):
20 n = count[i]
21 mode = i
22
23print("This is the mode = "+str(mode))
24print("This is the array = "+str(numbers))
25
26