-
Notifications
You must be signed in to change notification settings - Fork 0
/
statistics.py
213 lines (153 loc) · 4.35 KB
/
statistics.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
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
206
207
208
209
210
211
212
213
## Stephen Hoerner
## Assignment #4
## CSCD 110
# main program
def main():
## values: list, contains the values
## cmd: number, command index given by user
values = []
showMenu()
cmd = inputNum('Enter Command: ')
while cmd != 0:
print()
if cmd == 1:
showList(values)
elif cmd == 2:
values = addNum(values)
elif cmd == 3:
values = delNum(values)
elif cmd == 4:
values = purgeList(values)
elif cmd == 5:
print(getMean(values))
elif cmd == 6:
print(getMidRange(values))
elif cmd == 7:
print(getMedian(values))
elif cmd == 8:
showList(getMode(values))
else:
print('Invalid command!')
showMenu()
cmd = inputNum('Enter Command: ')
# displays a menu for commands
def showMenu():
print()
print('- - - - - - - - - - - -')
print('1. display numbers')
print('2. add number')
print('3. delete number')
print('4. purge all numbers')
print('5. find mean')
print('6. find mid-range value')
print('7. find median')
print('8. find mode')
print()
print('0. exit')
print('- - - - - - - - - - - -')
print()
# displays the contents of a list
def showList(group):
## group: list, contains values
## output: string, accumulator
## num: number, iterator
output = ''
for num in group:
if output == '':
output += str(num)
else:
output += ', ' + str(num)
print(output)
# adds a number to a list
def addNum(group):
## group: list, contains values
## ui: number, user input
ui = inputNum('Number to add: ')
if ui != None:
group.append(ui)
group.sort()
print('Number added.')
else:
print('Invalid number!')
return group
# removes a number from a list
def delNum(group):
## group: list, contains values
## orig: list, duplicate of 'list' in case of error
## ui: number, user input
orig = group
showList(group)
try:
ui = inputNum('Number to delete: ')
if ui != None:
group.remove(ui)
print('Number removed.')
else:
raise ValueError
except ValueError:
print('Not a valid number!')
else:
return orig
# purges (clears) a list
def purgeList(group):
## group: list, contains values
group = [] # clears actual list, rather than pointing to a new one
print('Purge successful.')
return group
# returns the mean of a list
def getMean(group):
## group: list, contains values
## output: accumulator, holds result
## num: number, iterator
output = 0
try:
for num in group:
output += num
output /= len(group)
return output
except:
return 'Cannot compute; list unsuitable'
# returns the midrange of a list
def getMidRange(group):
## group: list, contains values
if group == []:
return 'Cannot compute; list invalid'
return (min(group) + max(group)) / 2
# returns the median of a list
def getMedian(group):
## group: list, contains values
## val1: number, first median value
## val2: number, second median value
try:
if (len(group) - 1) % 2 == 0:
return group[(len(group)-1)//2]
else:
val1 = group[len(group)//2 - 1]
val2 = group[len(group)//2]
return (val1 + val2)/2
except IndexError:
return 'Cannot compute; list invalid'
# returns the mode(s) of a list
def getMode(group):
## group: list, contains values
## modes: list, accumulates modes for output
## counts: dictionary, holds the count for each list value
## i, j: numbers, iterators
if group == []:
return ['Cannot compute; list invalid']
modes = []
counts = {}
for i in group:
counts[i] = group.count(i)
for j in counts.keys():
if counts[j] == max(counts.values()) and j not in modes:
modes.append(j)
return modes
# gets number from user
def inputNum(prompt):
## prompt: string, countains user input prompt
try:
return eval(input(prompt))
except:
return None
main() # I wonder what happens if you comment this out?