# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# # <center>Assignment 1</center>

# ### Initial Setup
# ---
# This sets up the code which reads the file and stores all the data each in its appropriate variable
#

# +
# === Variables =========================================
code = ''
title = ''
fieldCount = 0
f = []
studentCount = 0
students = []

# === Functions =========================================
def sortDATA(filename):
    '''Gets & reads the .txt file provided in the input and sorts its data into its relevant variables'''
    global code, title, fieldCount, f, studentCount, students
    with open(filename, 'rt') as sub:
        for line in sub.readlines():
            line = line.replace('\n','').replace(' ','').split('|')
            for entry in line:
                if entry == '':
                    line.pop(line.index(entry))
                try:
                    line[line.index(entry)] = int(entry)
                except:
                    try:
                        line[line.index(entry)] = float(entry)
                    except:
                        pass
            if line[0] == 'Code':
                code = line[1]
            elif line[0] == 'Title':
                title = line[1]
            elif line[0] == 'FieldCount':
                fieldCount = line[1]
            elif line[0] == 'F':
                for i in range(1,4):
                    f.append(line[i])
            elif line[0] == 'StudentCount':
                studentCount = line[1]
            elif line[0] == 'S':
                line.pop(0)
                stuDetails = []
                marks = []
                for entry in line:
                    if type(entry) == float:
                        marks.append(entry)
                    else:
                        stuDetails.append(entry)
                stuDetails.append(marks)
                students.append(tuple(stuDetails))
        students = tuple(students)
        
def printAllDATA():
    '''Prints all the sorted Data for the subject loaded through the sortDATA() funtion.'''
    print(code)
    print(title)
    print(fieldCount)
    print(f)
    print(studentCount)
    print(students)


# -

# Run the function
sortDATA('subject.txt')

# ### Assignment 1 (a)

for student in students:
    print(student)### Assignment 1 (a)

# ### Assignment 1 (b)

# +
with open('finalmarks.txt', 'wt') as fMark:
    fMark.write('') # Makes sure the 'finalmarks.txt' file is empty on each run of the script (won't replicate data)
    
for student in students:
    name = student[1]+" "+student[2]
    finMark = round(student[3][0]*(f[0]/100)) + round(student[3][1]*(f[1]/100)) + round(student[3][2]*(f[2]/100))
    with open('finalmarks.txt', 'at') as fMark:
        fMark.write(name+" "+str(finMark)+'\n')
# -

# ### Assignment 1 (c)

# +
assignment1 = []
assignment2 = []
final = []

def avg(num):
    sumOfNumbers = 0
    for t in num:
        sumOfNumbers = sumOfNumbers + t

    avg = sumOfNumbers / len(num)
    return round(avg, 2)

for student in students:
    assignment1.append(student[3][0])
    assignment2.append(student[3][1])
    final.append(student[3][2])
    
print('| Assignment 1 |', str(min(assignment1)), " |", str(max(assignment1)), "|", str(avg(assignment1)), "|")
print('| Assignment 2 |', str(min(assignment2)), " |", str(max(assignment2)), "|", str(avg(assignment2)), "|")
print('| Final        |', str(min(final)), "|", str(max(final)), "|", str(avg(final)), "|")
# -

# ---
# # <center>Q.E.D.</center>

StarcoderdataJupyterScriptsDedupFiltered