-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatistic_py.py
More file actions
69 lines (54 loc) · 2.02 KB
/
statistic_py.py
File metadata and controls
69 lines (54 loc) · 2.02 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
class StatisticsCalculator:
"""Class for calculating basic statistics."""
def __init__(self, data):
self.data = data
def calculate_mean(self):
"""Calculate mean (average)."""
total = sum(self.data)
count = len(self.data)
return total / count
def calculate_median(self):
"""Calculate median (middle value)."""
sorted_data = sorted(self.data)
n = len(sorted_data)
if n % 2 == 0:
middle1 = sorted_data[n//2 - 1]
middle2 = sorted_data[n//2]
return (middle1 + middle2) / 2.0
else:
return sorted_data[n//2]
def calculate_mode(self):
"""Calculate mode (most frequent value)."""
# Count how many times each number appears
frequency = {}
for num in self.data:
if num in frequency:
frequency[num] = frequency[num] + 1
else:
frequency[num] = 1
# Find the highest frequency
max_count = 0
for count in frequency.values():
if count > max_count:
max_count = count
# Find all numbers with highest frequency
modes = []
for num, count in frequency.items():
if count == max_count:
modes.append(num)
return sorted(modes), max_count
def display_statistics(self):
"""Display all statistics."""
print("Input Data:", self.data)
print()
mean = self.calculate_mean()
print("Mean:", round(mean, 2))
median = self.calculate_median()
print("Median:", round(median, 2))
modes, frequency = self.calculate_mode()
print("Mode(s):", modes, "(appears", frequency, "time(s))")
# Main program
# Test case 1
data1 = [15, 23, 15, 42, 18, 23, 15, 30, 25, 18]
calc1 = StatisticsCalculator(data1)
calc1.display_statistics()