90 lines
2.2 KiB
Python
90 lines
2.2 KiB
Python
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:
|
||
return {
|
||
"count": 0,
|
||
"sum": 0,
|
||
"average": 0.0,
|
||
"min": None,
|
||
"max": None,
|
||
"variance": 0.0,
|
||
}
|
||
|
||
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) - 1) if len(numbers) > 1 else 0.0
|
||
|
||
return {
|
||
"count": len(numbers),
|
||
"sum": total,
|
||
"average": average,
|
||
"min": min_val,
|
||
"max": max_val,
|
||
"variance": variance,
|
||
}
|
||
|
||
|
||
def process_data(filename):
|
||
numbers = []
|
||
with open(filename) as file_handle:
|
||
for line in file_handle:
|
||
stripped = line.strip()
|
||
if not stripped:
|
||
continue
|
||
try:
|
||
numbers.append(float(stripped))
|
||
except ValueError:
|
||
continue
|
||
|
||
result = calculate_statistics(numbers)
|
||
print("Statistics:", result)
|
||
return result
|
||
|
||
|
||
def normalize(numbers, method="minmax"):
|
||
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:
|
||
raise ValueError(f"Unknown normalization method: {method}")
|
||
|
||
|
||
if __name__ == "__main__":
|
||
sample = [4, 8, 15, 16, 23, 42]
|
||
print(calculate_statistics(sample))
|
||
|