Question: e ) Suppose I have a new query: Qe: For every department, display the min, median, and max graduation years. Use your . txt file

e) Suppose I have a new query: Qe: For every department, display the min, median, and max graduation years. Use your .txt file containing de-normalized data from part-b to answer Qe with python instead of SQL. Note that this solution should not use SQLite or SQL language. Your solution should work for any input, not just for specific department in your data.
My text file contains
(1, 'boars head', '10 main st',2022,'CSC101',1,'A','CSC101', 'Computer Science', 4)
(1, 'boars head', '10 main st',2022,'DSC550',1,'D','DSC550', 'Data Science', 4)
(2, 'taco king', '20 south st',2022,'CSC101',2,'B','CSC101', 'Computer Science', 4)
(2, 'taco king', '20 south st',2022,'DSC550',2,'B','DSC550', 'Data Science', 4)
(3, 'blue sky','30 east st',2020, 'ECON110',3,'B', 'ECON110', 'Economics', 4)
(3, 'blue sky','30 east st',2020, 'MATH220',3,'C', 'MATH220', 'Mathematics', 4)
(4, 'Blake eats', '40 north st',2021, 'ECON110',4,'A', 'ECON110', 'Economics', 4)
(5, 'mike cheddar', '50 west st',2020, 'MATH220',5,'A', 'MATH220', 'Mathematics', 4)
my code so far is below, but it wont execute - please advise the correct code
def compute_median(values):
sorted_values = sorted (values)
n = len(sorted_values)
if n%2==1:
return sorted_values[n//2]
else:
mid = n//2
return(sorted_values[mid -1]+ sorted_values[mid])/2.0
def run_data_file(data_file):
department_data ={}
with open(data_file,'r') as file:
lines = file.readlines()
lines = lines[1:]
for line in lines:
parts = line.strip().split('\t')
department,grad_year=parts[4],float(parts[3])
if department not in department_data:
department_data[department]=[]
department_data[department].append(grad_year)
for department, grad_years in department_data.items():
max_grad_year=max(grad_years)
median_grad_year=compute_median(grad_years)
min_grad_year = min(grad_year)
print(f"Department:{department}")
print(f"Max Graduation Year:{max_grad_year}")
print(f"Median Graduation Year:{median_grad_year}")
print(f"Min Graduation Year:{min_grad_year}")
print()
data_file = 'inputfile.txt'
run_data_file(data_file)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!