diff --git a/main.py b/main.py index bba6e65..5862f4d 100644 --- a/main.py +++ b/main.py @@ -1,7 +1,7 @@ import ast import math -VERSION = "0.0.0" +VERSION = "0.0.1" sContinue = True state = [] @@ -13,6 +13,7 @@ def show_help(): 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") @@ -41,13 +42,14 @@ def get_median(): :return: The median value of the sorted `state` list. """ global state - state.sort() - n = len(state) + sorted_state = state + sorted_state.sort() + n = len(sorted_state) median = 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: - median = state[n // 2] + median = sorted_state[n // 2] return median @@ -61,6 +63,23 @@ def get_mode(): 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) @@ -97,7 +116,6 @@ def start_shell(): global state state = [] print(f'Welcome to math-shell v{VERSION}') - handle_input() def handle_input(): @@ -130,6 +148,9 @@ def handle_input(): 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': if state is not None: print(f"{state}")