Skip to content

Instantly share code, notes, and snippets.

@vinayakg
Last active May 13, 2024 12:57
Show Gist options
  • Save vinayakg/e001b5f2546afe0c17a0034de63ec847 to your computer and use it in GitHub Desktop.
Save vinayakg/e001b5f2546afe0c17a0034de63ec847 to your computer and use it in GitHub Desktop.
Mean-Variance-Standard Deviation Calculator
import numpy as np
def calculate(lst):
if len(lst) != 9:
raise ValueError("List must contain nine numbers.")
arr = np.array(lst)
reshapedarr = arr.reshape(3, 3)
result_dict = {}
result_dict["mean"] = [
reshapedarr.mean(axis=1).tolist(),
reshapedarr.mean(axis=0).tolist(),
reshapedarr.mean(),
]
result_dict["variance"] = [
reshapedarr.var(axis=1).tolist(),
reshapedarr.var(axis=0).tolist(),
reshapedarr.var(),
]
result_dict["standard deviation"] = [
reshapedarr.std(axis=1).tolist(),
reshapedarr.std(axis=0).tolist(),
reshapedarr.std(),
]
result_dict["max"] = [
reshapedarr.max(axis=1).tolist(),
reshapedarr.max(axis=0).tolist(),
reshapedarr.max(),
]
result_dict["min"] = [
reshapedarr.min(axis=1).tolist(),
reshapedarr.min(axis=0).tolist(),
reshapedarr.min(),
]
result_dict["sum"] = [
reshapedarr.sum(axis=1).tolist(),
reshapedarr.sum(axis=0).tolist(),
reshapedarr.sum(),
]
return result_dict
lst = [1, 2, 3, 4, 5, 6, 7, 8, 9]
print(calculate(lst))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment