AISE1_CLASS/Prompting Exercise/analyze_me_blind_fix.py

90 lines
2.3 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import sys
"""
analyze_me.py A data-processing script used in Exercise 2
==============================================================
This file contains several realistic bugs and style issues.
Do NOT fix them manually — in Exercise 2 the LLM will help you find them!
Can you spot the issues yourself before asking the LLM?
"""
def calculate_statistics(numbers):
if not numbers:
raise ValueError("Cannot calculate statistics for an empty list.")
total = 0
for n in numbers:
total = total + n
average = total / len(numbers)
min_val = numbers[0]
max_val = numbers[0]
for n in numbers:
if n < min_val:
min_val = n
if n > max_val:
max_val = n
variance = 0
for n in numbers:
variance = variance + (n - average) ** 2
variance = variance / len(numbers)
return {
"count": len(numbers),
"sum": total,
"average": average,
"min": min_val,
"max": max_val,
"variance": variance,
}
def process_data(filename):
numbers = []
try:
with open(filename, 'r') as file_handle:
for line in file_handle:
stripped_line = line.strip()
if stripped_line:
numbers.append(int(stripped_line))
except FileNotFoundError:
print(f"Error: File '{filename}' not found.")
raise
except ValueError as e:
print(f"Error: Invalid integer in file: {e}")
raise
result = calculate_statistics(numbers)
print("Statistics:", result)
return result
def normalize(numbers, method="minmax"):
if not numbers:
raise ValueError("Cannot normalize an empty list.")
if method == "minmax":
mn = min(numbers)
mx = max(numbers)
if mx == mn:
return [0.0 for _ in numbers]
return [(x - mn) / (mx - mn) for x in numbers]
elif method == "zscore":
stats = calculate_statistics(numbers)
std = stats["variance"] ** 0.5
if std == 0:
return [0.0 for _ in numbers]
return [(x - stats["average"]) / std for x in numbers]
else:
print("Unknown normalization method")
return []
if __name__ == "__main__":
sample = [4, 8, 15, 16, 23, 42]
print(calculate_statistics(sample))