import ast import math VERSION = "0.0.0" 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("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 state.sort() n = len(state) median = 0 if n % 2 == 0: median = (state[n // 2 - 1] + state[n // 2]) / 2 else: median = 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_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(): global state state = [] print(f'Welcome to math-shell v{VERSION}') handle_input() 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 '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()