forked from winner9871/the-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproject1.py
57 lines (47 loc) · 1.51 KB
/
project1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
def mean(dataList):
if len(dataList) == 0:
return 0
else:
theSum = 0
for dataValue in dataList:
theSum += dataValue
meanValue = theSum / len(dataList)
return meanValue
def median(dataList):
n = len(dataList)
if n == 0:
return 0
elif n % 2 == 1:
return dataList[n // 2]
else:
medianValue = (dataList[n // 2] + dataList[(n // 2) -1]) / 2
return medianValue
def mode(dataList):
if len(dataList) == 0:
return 0
else:
freqOfMostAppearing = 0
tmpCounter = 0
modeValue = None
alreadyVisited = []
for dataValue in dataList:
if dataValue not in alreadyVisited:
alreadyVisited.append(dataValue)
for element in dataList:
if element == dataValue:
tmpCounter += 1
if tmpCounter > freqOfMostAppearing:
modeValue = dataValue
freqOfMostAppearing = tmpCounter
elif tmpCounter == freqOfMostAppearing:
modeValue = (modeValue + dataValue) / 2
tmpCounter = 0
return modeValue
def main():
data = [1, 2, 3, 3, 4, 4, 5, 8, 9, 10]
print("Data is :", data)
print("Mean Of Data is : ", mean(data))
print("Median Of Data is : ", median(data))
print("Mode Of Data is : ", mode(data))
if __name__ == "__main__":
main()