Compare commits
2 Commits
3ae519a6bd
...
dc403de240
| Author | SHA1 | Date |
|---|---|---|
|
|
dc403de240 | |
|
|
ad44e727b0 |
96
main.py
96
main.py
|
|
@ -1,17 +1,19 @@
|
||||||
import ast
|
import ast
|
||||||
|
import math
|
||||||
|
|
||||||
# Press Shift+F10 to execute it or replace it with your code.
|
VERSION = "0.0.1"
|
||||||
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.
|
|
||||||
VERSION = "0.0.0"
|
|
||||||
sContinue = True
|
sContinue = True
|
||||||
|
|
||||||
|
|
||||||
state = []
|
state = []
|
||||||
|
|
||||||
|
|
||||||
def show_help():
|
def show_help():
|
||||||
print("vector [5,6,7...] - set the current dataset to the given vector")
|
print("vector [5,6,7...] - set the current dataset to the given vector")
|
||||||
print("stdd - get the standard deviation of the current dataset")
|
print("stdev - get the standard deviation of the current dataset")
|
||||||
|
print("pstdev - get the population standard deviation of the current dataset")
|
||||||
|
print("variance - get the variance of the current dataset")
|
||||||
|
print("pvariance - get the population variance of the current dataset")
|
||||||
|
print("iqr - upper and lower interquartile range of the current dataset")
|
||||||
print("mean - get the mean of the current dataset")
|
print("mean - get the mean of the current dataset")
|
||||||
print("median - get the median of the current dataset")
|
print("median - get the median of the current dataset")
|
||||||
print("mode - get the mode of the current dataset")
|
print("mode - get the mode of the current dataset")
|
||||||
|
|
@ -40,21 +42,80 @@ def get_median():
|
||||||
:return: The median value of the sorted `state` list.
|
:return: The median value of the sorted `state` list.
|
||||||
"""
|
"""
|
||||||
global state
|
global state
|
||||||
state.sort()
|
sorted_state = state
|
||||||
n = len(state)
|
sorted_state.sort()
|
||||||
|
n = len(sorted_state)
|
||||||
median = 0
|
median = 0
|
||||||
if n % 2 == 0:
|
if n % 2 == 0:
|
||||||
median = (state[n // 2 - 1] + state[n // 2]) / 2
|
median = (sorted_state[n // 2 - 1] + sorted_state[n // 2]) / 2
|
||||||
else:
|
else:
|
||||||
median = state[n // 2]
|
median = sorted_state[n // 2]
|
||||||
return median
|
return median
|
||||||
|
|
||||||
|
|
||||||
|
def get_mode():
|
||||||
|
"""
|
||||||
|
:return: The most frequent element in the global variable 'state'.
|
||||||
|
"""
|
||||||
|
global state
|
||||||
|
from collections import Counter
|
||||||
|
mode = Counter(state).most_common(1)[0][0]
|
||||||
|
return mode
|
||||||
|
|
||||||
|
|
||||||
|
def get_iqr():
|
||||||
|
global state
|
||||||
|
sorted_state = state
|
||||||
|
sorted_state.sort()
|
||||||
|
middle = int(len(sorted_state) // 2)
|
||||||
|
lower = sorted_state[:middle]
|
||||||
|
upper = sorted_state[middle + 1 if len(sorted_state) % 2 == 1 else middle:len(sorted_state)]
|
||||||
|
if len(lower) % 2 == 0:
|
||||||
|
l_iqr = (lower[len(lower)//2-1] + lower[len(lower)//2])/2
|
||||||
|
u_iqr = (upper[len(upper)//2-1] + upper[len(upper)//2])/2
|
||||||
|
else:
|
||||||
|
l_iqr = lower[(len(lower) // 2)]
|
||||||
|
u_iqr = upper[(len(upper) // 2)]
|
||||||
|
r_iqr = u_iqr - l_iqr
|
||||||
|
return l_iqr, u_iqr, r_iqr
|
||||||
|
|
||||||
|
|
||||||
|
def get_variance():
|
||||||
|
global state
|
||||||
|
n = len(state)
|
||||||
|
|
||||||
|
mean = get_mean()
|
||||||
|
|
||||||
|
variance = sum((x - mean) ** 2 for x in state) / (n - 1)
|
||||||
|
return variance
|
||||||
|
|
||||||
|
|
||||||
|
def get_population_variance():
|
||||||
|
global state
|
||||||
|
n = len(state)
|
||||||
|
|
||||||
|
mean = get_mean()
|
||||||
|
|
||||||
|
variance = sum((x - mean) ** 2 for x in state) / n
|
||||||
|
return variance
|
||||||
|
|
||||||
|
|
||||||
|
def get_standard_deviation():
|
||||||
|
variance = get_variance()
|
||||||
|
std_dev = math.sqrt(variance)
|
||||||
|
return std_dev
|
||||||
|
|
||||||
|
|
||||||
|
def get_population_standard_deviation():
|
||||||
|
pvariance = get_population_variance()
|
||||||
|
std_dev = math.sqrt(pvariance)
|
||||||
|
return std_dev
|
||||||
|
|
||||||
|
|
||||||
def start_shell():
|
def start_shell():
|
||||||
global state
|
global state
|
||||||
state = []
|
state = []
|
||||||
print(f'Welcome to math-shell v{VERSION}')
|
print(f'Welcome to math-shell v{VERSION}')
|
||||||
handle_input()
|
|
||||||
|
|
||||||
|
|
||||||
def handle_input():
|
def handle_input():
|
||||||
|
|
@ -75,6 +136,21 @@ def handle_input():
|
||||||
print(f"Set state to: {value}")
|
print(f"Set state to: {value}")
|
||||||
case 'mean':
|
case 'mean':
|
||||||
print(get_mean())
|
print(get_mean())
|
||||||
|
case 'median':
|
||||||
|
print(get_median())
|
||||||
|
case 'mode':
|
||||||
|
print(get_mode())
|
||||||
|
case 'stdev':
|
||||||
|
print(get_standard_deviation())
|
||||||
|
case 'pstdev':
|
||||||
|
print(get_population_standard_deviation())
|
||||||
|
case 'variance':
|
||||||
|
print(get_variance())
|
||||||
|
case 'pvariance':
|
||||||
|
print(get_population_variance())
|
||||||
|
case 'iqr':
|
||||||
|
lower, upper, range = get_iqr()
|
||||||
|
print(f"Lower: {lower} - Upper: {upper} - Range: {range}")
|
||||||
case 'print':
|
case 'print':
|
||||||
if state is not None:
|
if state is not None:
|
||||||
print(f"{state}")
|
print(f"{state}")
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue