Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please write code in Newest version of Python (Data file not needed) A program t

ID: 3696677 • Letter: P

Question

Please write code in Newest version of Python

(Data file not needed)

A program that computes the following based on each data file:

The median value (Sort the grades in ascending order. If the # of students is odd you can take the middle of all the grades (i.e. [0, 50, 100] - the median is 50). If the # of students is even you can compute the mean by averaging the middle two values (i.e. [0, 50, 60, 100] - the median is 55)

The mode value (the grade that occurs most frequently - i.e. [0, 50, 50, 100] - the mode is 50). Hint: you will probably want to create two new lists to do this. One new list canhold the UNIQUE test scores found and the other can hold the # of times that score has been seen. For example:

Hint: use the "in" operator, the "append" method and the max function to help you solve this problem! If you need help look back at the "World Series Winner" program we wrote in class.Note that it is possible that multiple scores could qualify as the "mode". For example, say that you found a class to have the following scores:

Both 70 and 80 are represented twice in the list, so they are both considered the "mode" for this class. For extra credit identify all "mode" values for a given file.

Here is a sample running of your program for the first two data files. A complete listing of the expected output for all data files can be found in the downloadable package for this assignment.

Sample of Data file class1.txt:

N00000001,A,A,D,D,C,D,D,A,,C,D,B,C,,B,C,B,D,A,C,,A,,C,D
N00000002,,A,,D,,B,D,A,C,C,D,,A,A,A,C,B,D,C,C,A,A,B,,D
N00000003,B,A,,D,C,B,D,A,C,C,,B,A,B,A,C,B,D,A,,A,,B,D,D
N00000004,B,B,D,,,B,D,A,C,C,D,B,A,B,A,C,B,D,,C,A,D,B,C,D
N00000005,B,A,,D,,B,D,A,C,C,D,B,A,B,A,C,B,D,D,C,A,A,,D,D
N00000006,B,A,D,A,C,B,D,A,C,C,C,B,A,D,A,C,,C,A,C,A,C,B,D,A
N00000007,B,A,D,D,C,B,D,A,C,,D,B,A,B,A,C,B,D,,C,A,A,B,D,
N00000008,A,A,D,D,C,,D,A,C,C,B,C,A,B,A,A,B,D,A,C,A,,B,B,D
N00000009,B,A,,D,C,B,D,A,C,A,D,B,A,B,A,A,B,D,A,C,A,A,B,D,D
N00000010,,A,D,B,C,B,D,A,C,C,D,B,A,B,A,C,B,D,A,D,,,B,D,B
N00000011,B,A,D,,C,B,D,A,C,D,D,B,A,B,A,C,B,D,A,C,A,B,B,D,D
N00000012,B,,D,D,C,B,D,A,C,C,D,B,A,B,A,A,B,D,A,,A,A,A,,C
N00000013,,A,D,,B,B,,A,C,A,D,B,A,B,D,C,B,D,A,C,A,A,B,D,D
N00000014,B,B,D,D,C,B,,D,C,C,D,B,A,B,A,C,,D,A,C,A,A,,D,D
N00000015,A,C,C,D,C,B,,A,C,C,D,B,A,B,A,C,B,D,A,C,A,B,,D,D
N00000016,B,A,C,D,,B,D,A,C,C,D,B,A,B,A,C,B,D,A,C,A,A,B,D,D
N00000017,B,A,D,D,C,B,C,A,D,C,D,A,A,D,A,C,B,D,D,B,,A,B,D,D
N00000018,B,A,D,A,C,B,D,A,C,C,D,B,A,B,,,B,D,A,B,A,A,B,,D
N00000019,B,A,D,D,C,B,D,C,C,C,,B,,B,D,C,B,D,A,,A,A,B,D,D
N00000020,B,,D,D,C,B,D,A,C,B,,,A,C,A,D,B,D,B,C,A,A,B,D,D

Explanation / Answer


# get file with student answers

valid_file = False

while not valid_file:

file_to_open = input('Enter a class file to grade (i.e. class1 for class1.txt): ').strip()
grades_input = file_to_open + '.txt'

try:
    grades = open(grades_input, "r")
    print('Successfully opened {} '.format(file_to_open))
    break
except:
    print('File cannot be found. '.format(file_to_open))

# create new file to track individual student data
grades_output = file_to_open + '_grades.txt'
output_file = open(grades_output, "w")

answer_key = 'B,A,D,D,C,B,D,A,C,C,D,B,A,B,A,C,B,D,A,C,A,A,B,D,D'.split(',')

unusable_line_counter = 0
student_counter = 0
all_student_ids = list()
all_student_grades = list()

# calculate grades
for line in grades:
line_as_list = line.replace(' ','').split(',')
student_counter += 1
if len(line_as_list) != 26:
    unusable_line_counter += 1
else:
    student_id = line_as_list[0]
    student_answers = line_as_list[1:]

    student_grade = 0

    for x in range(0,len(student_answers)):
      if student_answers[x] == '' or student_answers[x] == ' ':
        student_grade += 0
      elif student_answers[x] == answer_key[x]:
        student_grade += 4
      else:
        student_grade -= 1

    all_student_ids.append(student_id)
    all_student_grades.append(student_grade)
    output_file.write(str(student_id) + ',' + '{:.2f}'.format(student_grade) + ' ')

output_file.close()

#calculate stats
all_student_grades_sorted = sorted(all_student_grades)

high_score = max(all_student_grades)
low_score = min(all_student_grades)
average_score = sum(all_student_grades) / len(all_student_grades)

num_valid_students = len(all_student_grades)

if num_valid_students % 2 == 0:
median_score = int((all_student_grades_sorted[(num_valid_students // 2)] + all_student_grades[(student_counter // 2) - 1]) / 2)
else:
median_score = int(all_student_grades_sorted[num_valid_students // 2])

unique_grades = list()
uniques_counter = list()

for grade in all_student_grades:
if grade not in unique_grades:
    unique_grades.append(grade)
    uniques_counter.append(1)
else:
    uniques_counter[unique_grades.index(grade)] += 1

mode = list()
for x in range(0,len(uniques_counter)):
if uniques_counter[x] == max(uniques_counter):
    mode.append(str(unique_grades[x]))

score_range = max(unique_grades) - min(unique_grades)

# print stats
print('Grade Summary: ')
print('Total Students: {}'.format(num_valid_students))
print('Unusable lines in the file: {}'.format(unusable_line_counter))
print('Highest score: {}'.format(high_score))
print('Lowest score: {}'.format(low_score))
print('Mean score: {:.2f}'.format(average_score))
print('Median score: {}'.format(median_score))
print('Mode: {}'.format(' '.join(mode)))
print('Range: {}'.format(score_range))
print(' ')

# curve if user chooses
valid_curve_response = False

while not valid_curve_response:
should_curve = input("Would you like to curve the exam? 'y' or 'n': ")

if should_curve == 'y' or 'n':
    valid_curve_response = True
else:
    print("Sorry, that's not a valid response. ")

if should_curve == 'y':
valid_mean = False

while not valid_mean:
    desired_mean = input('Enter a desired mean: ')

    if desired_mean.isnumeric() and int(desired_mean) > 0 and int(desired_mean) <= 100:
      valid_mean = True
      desired_mean = int(desired_mean)
    else:
      print("Invalid curve, try again")

if desired_mean > average_score:

    curve_by = desired_mean - average_score
  
    all_student_curved_grades = list()

    for x in range(0,len(all_student_grades)):
      curved_grade = all_student_grades[x] + curve_by
      all_student_curved_grades.append(curved_grade)

    update_file = open(grades_output, "w")

    for x in range(0,len(all_student_grades)):
      update_file.write('{},{:.2f},{:.2f} '.format(all_student_ids[x], all_student_grades[x], all_student_curved_grades[x]))
    update_file.close()
    
    
class1.txt

N00000001,A,A,D,D,C,D,D,A,,C,D,B,C,,B,C,B,D,A,C,,A,,C,D
N00000002,,A,,D,,B,D,A,C,C,D,,A,A,A,C,B,D,C,C,A,A,B,,D
N00000003,B,A,,D,C,B,D,A,C,C,,B,A,B,A,C,B,D,A,,A,,B,D,D
N00000004,B,B,D,,,B,D,A,C,C,D,B,A,B,A,C,B,D,,C,A,D,B,C,D
N00000005,B,A,,D,,B,D,A,C,C,D,B,A,B,A,C,B,D,D,C,A,A,,D,D
N00000006,B,A,D,A,C,B,D,A,C,C,C,B,A,D,A,C,,C,A,C,A,C,B,D,A
N00000007,B,A,D,D,C,B,D,A,C,,D,B,A,B,A,C,B,D,,C,A,A,B,D,
N00000008,A,A,D,D,C,,D,A,C,C,B,C,A,B,A,A,B,D,A,C,A,,B,B,D
N00000009,B,A,,D,C,B,D,A,C,A,D,B,A,B,A,A,B,D,A,C,A,A,B,D,D
N00000010,,A,D,B,C,B,D,A,C,C,D,B,A,B,A,C,B,D,A,D,,,B,D,B
N00000011,B,A,D,,C,B,D,A,C,D,D,B,A,B,A,C,B,D,A,C,A,B,B,D,D
N00000012,B,,D,D,C,B,D,A,C,C,D,B,A,B,A,A,B,D,A,,A,A,A,,C
N00000013,,A,D,,B,B,,A,C,A,D,B,A,B,D,C,B,D,A,C,A,A,B,D,D
N00000014,B,B,D,D,C,B,,D,C,C,D,B,A,B,A,C,,D,A,C,A,A,,D,D
N00000015,A,C,C,D,C,B,,A,C,C,D,B,A,B,A,C,B,D,A,C,A,B,,D,D
N00000016,B,A,C,D,,B,D,A,C,C,D,B,A,B,A,C,B,D,A,C,A,A,B,D,D
N00000017,B,A,D,D,C,B,C,A,D,C,D,A,A,D,A,C,B,D,D,B,,A,B,D,D
N00000018,B,A,D,A,C,B,D,A,C,C,D,B,A,B,,,B,D,A,B,A,A,B,,D
N00000019,B,A,D,D,C,B,D,C,C,C,,B,,B,D,C,B,D,A,,A,A,B,D,D
N00000020,B,,D,D,C,B,D,A,C,B,,,A,C,A,D,B,D,B,C,A,A,B,D,D