Compare commits

..

No commits in common. "dc403de240426c8c433e37646118ea3a2735469e" and "3ae519a6bd2c3ff4c4b4207c17b2a43077143da8" have entirely different histories.

1 changed files with 10 additions and 86 deletions

96
main.py
View File

@ -1,19 +1,17 @@
import ast import ast
import math
VERSION = "0.0.1" # Press Shift+F10 to execute it or replace it with your code.
# 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("stdev - get the standard deviation of the current dataset") print("stdd - 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")
@ -42,80 +40,21 @@ 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
sorted_state = state state.sort()
sorted_state.sort() n = len(state)
n = len(sorted_state)
median = 0 median = 0
if n % 2 == 0: if n % 2 == 0:
median = (sorted_state[n // 2 - 1] + sorted_state[n // 2]) / 2 median = (state[n // 2 - 1] + state[n // 2]) / 2
else: else:
median = sorted_state[n // 2] median = 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():
@ -136,21 +75,6 @@ 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}")