# Portfolio Project : Optimizing Code With Generative AI

Hi guys!

I just created my first portfolio project with ChatGPT! Check it out and if you have any suggestions to enhance this code, I will happily receive it!

class StatsCalculator: @staticmethod def calculate_average(data: list[float]) -> float: if not data: raise ValueError("Input data is empty") data_sum = sum(data) return data_sum / len(data) @staticmethod def calculate_variance(data: list[float]) -> float: if not data: raise ValueError("Input data is empty") mean = StatsCalculator.calculate_average(data) squared_diffs = [(xi - mean) ** 2 for xi in data] variance = sum(squared_diffs) / len(data) return variance @staticmethod def calculate_standard_deviation(data: list[float]) -> float: if not data: raise ValueError("Input data is empty") variance = StatsCalculator.calculate_variance(data) std_deviation = variance ** 0.5 return std_deviation @staticmethod def calculate_median(data: list[float]) -> float: if not data: raise ValueError("Input data is empty") sorted_data = sorted(data) n = len(sorted_data) middle = n // 2 if n % 2 == 0: middle1, middle2 = sorted_data[middle], sorted_data[middle - 1] return (middle1 + middle2) / 2 else: return sorted_data[middle]

it has errors, have you checked its completely bug free?

Hereโs My First Portfolio Project Also with Chat GPT : D : P

from typing import List class StatsCalculator: def calculate(self, name: str, data: List[float]) -> float: if name == "average": return sum(data) / len(data) elif name == "variance": mean = sum(data) / len(data) return sum((x - mean) ** 2 for x in data) elif name == "standard dev": mean = sum(data) / len(data) variance = sum((x - mean) ** 2 for x in data) return variance ** 0.5 elif name == "median": sorted_data = sorted(data) n = len(sorted_data) if n % 2 == 0: return (sorted_data[n // 2 - 1] + sorted_data[n // 2]) / 2 else: return sorted_data[n // 2] else: print("Invalid function name. Please choose from 'average', 'variance', 'standard dev', or 'median'.") return -1 def get_user_input(): function_mapping = { '1': 'average', '2': 'variance', '3': 'standard dev', '4': 'median', } while True: print("Select a statistical function:") for key, value in function_mapping.items(): print(f"{key}. {value.capitalize()}") choice = input("Enter the number of your choice: ") function_name = function_mapping.get(choice) if function_name: return function_name else: print("Invalid choice. Please enter a valid number.") if __name__ == "__main__": calculator = StatsCalculator() function_name = get_user_input() if function_name is not None: data = [float(x) for x in input("Enter a list of numbers separated by spaces: ").split()] result = calculator.calculate(function_name, data) print(f"Result: {result}") else: print("Invalid choice. Please select a valid option.")