-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStatistics.py
More file actions
209 lines (141 loc) · 5.32 KB
/
Statistics.py
File metadata and controls
209 lines (141 loc) · 5.32 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#!/usr/bin/env python
# coding: utf-8
# In[3]:
def convert_to_number(inputVal):
isNumber = True
try:
return float(inputVal)
except ValueError:
isNumber = False
try:
return complex(inputVal)
except ValueError:
isNumber = False
try:
return int(inputVal)
except ValueError:
isNumber = False
if not isNumber:
return 'Not a Number'
# In[5]:
def calculate_mean():
#Calculation of Mean
#Sample Valid and Invalid Input dataset
#Valid Inputs:
# 2 8 9 11 15 3 6 8 1
# 2 8 9 11 15 3 6.5 8 1
# 2 8 9 11 15 3 64343.45454545 8 1
# 2 8 9 11 15 33423432432432432423434344 6.5 8 1
#Invalid Inputs:
# 2 8 9 11 15 3 6 8 1 sdsd
# 2 8 9 11 15s 3 6 8 1
#Start of Program
inputStr = input("Please provide input array of numbers : ")
print("\nThis is the input array provided by you : " + inputStr + "\n")
#Split input string into an array
inputStrArr = inputStr.strip().split(" ")
isOnlyNumber = True
#Input array which stores numbers
inputNumArr = []
for eachStr in inputStrArr:
eachNumber = convert_to_number(eachStr)
if eachNumber == 'Not a Number':
print("Please provide only numbers as input!")
isOnlyNumber = False
break
else:
inputNumArr.append(eachNumber)
#Execute next steps only if we have valid input data set
if isOnlyNumber and len(inputNumArr) > 0:
#find the count of numbers in array
countOfNumbers = len(inputNumArr)
#variable which holds total sum of input numbers
totalSum = 0
#loop though array to find the sum of numbers
for x in inputNumArr:
totalSum += x
#calculate mean using the total sum and count of numbers (totalSum/countOfNumbers)
mean = totalSum/countOfNumbers
print("Count of the input numbers = " + str(countOfNumbers))
print("Total sum of input numbers = " + str(totalSum))
print("Mean of the numbers given = " + str(mean))
# In[6]:
def calculate_median():
#Calculation of Median
#To calculate the middle most value in the series.
#Inputs
#1 2 3 4 5 6 7 8 9 10
#1 2 3 4 5 6 7 8 9
#Start of Program
inputStr = input("Please provide input array of numbers : ")
print("\nThis is the input array provided by you : " + inputStr + "\n")
#Split input string into an array
inputStrArr = inputStr.strip().split(" ")
isOnlyNumber = True
#Input array which stores numbers
inputNumArr = []
for eachStr in inputStrArr:
eachNumber = convert_to_number(eachStr)
if eachNumber == 'Not a Number':
print("Please provide only numbers as input!")
isOnlyNumber = False
break
else:
inputNumArr.append(eachNumber)
#Execute next steps only if we have valid input data set
if isOnlyNumber and len(inputNumArr) > 0:
countOfNumbers = len(inputNumArr)
inputNumArr.sort()
median = 0
if(countOfNumbers % 2 == 0):
median = (inputNumArr[int(countOfNumbers/2)] + inputNumArr[int(countOfNumbers/2) + 1])/2
else:
median = inputNumArr[int(countOfNumbers/2)]
print("Count of the input numbers = " + str(countOfNumbers))
print("Median of the numbers given = " + str(median))
# In[7]:
def calculate_mode():
#Calculation of Mode
#The number which appers most in the series
#Sample inputs
#1 2 2 2 3 5 4 3 5 6 5
#Start of Program
inputStr = input("Please provide input array of numbers : ")
print("\nThis is the input array provided by you : " + inputStr + "\n")
#Split input string into an array
inputStrArr = inputStr.strip().split(" ")
isOnlyNumber = True
#Input array which stores numbers
inputNumArr = []
for eachStr in inputStrArr:
eachNumber = convert_to_number(eachStr)
if eachNumber == 'Not a Number':
print("Please provide only numbers as input!")
isOnlyNumber = False
break
else:
inputNumArr.append(eachNumber)
#Execute next steps only if we have valid input data set
if isOnlyNumber and len(inputNumArr) > 0:
countOfNumbers = len(inputNumArr)
inputNumArr.sort()
mode = 0
map_num_occurance = {}
for number in inputNumArr:
if number in map_num_occurance:
map_num_occurance[number] = map_num_occurance[number] + 1
else:
map_num_occurance[number] = 1
list_most_occured_num = []
list_most_occured_num.append(inputNumArr[0])
highest_occurance = map_num_occurance[most_occured_num]
for number in map_num_occurance:
if(map_num_occurance[number] > highest_occurance):
highest_occurance = map_num_occurance[number]
list_most_occured_num[0] = number
elif(number != inputNumArr[0] and map_num_occurance[number] == highest_occurance):
list_most_occured_num.append(number)
print("Count of the input numbers = " + str(countOfNumbers))
print("Most occured time = " + str(highest_occurance))
print("Mode of the numbers given = " + str(list_most_occured_num))
# In[ ]: