math-shell/main.py

181 lines
4.5 KiB
Python

import ast
import math
VERSION = "0.0.1"
sContinue = True
state = []
def show_help():
print("vector [5,6,7...] - set the current dataset to the given vector")
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("median - get the median of the current dataset")
print("mode - get the mode of the current dataset")
print("var - get the variance of the current dataset")
print("print - prints the current dataset")
print("clear - clears the current dataset")
print("clr - clears the screen")
def get_mean():
"""
Get the mean value of the state vector.
:return: The average value of the state vector.
"""
global state
mean = sum(state) / len(state)
return mean
def get_median():
"""
This function calculates the median of the state vector.
The implementation uses the algorithm based on sorting.
:return: The median value of the sorted `state` list.
"""
global state
sorted_state = state
sorted_state.sort()
n = len(sorted_state)
if n % 2 == 0:
median = (sorted_state[n // 2 - 1] + sorted_state[n // 2]) / 2
else:
median = sorted_state[n // 2]
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 get_mean_average_deviation():
global state
mean = get_mean()
n = len(state)
mad = sum(abs(x - mean) for x in state) / n
return mad
def start_shell():
global state
state = []
print(f'Welcome to math-shell v{VERSION}')
def handle_input():
global state
value = []
print('>>>', end=' ')
i = input()
should_split = i.__contains__(' ')
if should_split:
command, value = i.split(sep=' ', maxsplit=1)
value = ast.literal_eval(value)
else:
command = i
match command.lower():
case 'vector':
state = value
print(f"Set state to: {value}")
case '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, iqr = get_iqr()
print(f"Lower: {lower} - Upper: {upper} - Range: {iqr}")
case 'mad':
print(get_mean_average_deviation())
case 'print':
if state is not None:
print(f"{state}")
else:
print("No state")
case 'exit':
return False
case other:
show_help()
return True
if __name__ == '__main__':
start_shell()
while sContinue:
sContinue = handle_input()