-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathanswer.py
More file actions
58 lines (48 loc) · 2.41 KB
/
answer.py
File metadata and controls
58 lines (48 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
def student_log(text: str):
studentInfo: dict[int,dict] = {}
for lines in text:
studentID: int = int(lines[0])
actionCode: str = lines[1]
if not studentID in studentInfo.keys():
studentInfo[studentID] = {'lowestPageID': None, 'latestPageID': None, 'totalScores':0, 'totalSubmitted':0, 'avgSubmissionScore': 0}
match actionCode:
case 'P':
actionCode = 'P'
pageID = int(lines[2])
studentInfo[studentID]['latestPageID'] = pageID
if studentInfo[studentID]['lowestPageID']:
if pageID > studentInfo[studentID]['lowestPageID']:
continue
studentInfo[studentID]['lowestPageID'] = pageID
case 'S':
actionCode = 'S'
submissionScore = int(lines[2])
studentInfo[studentID]['totalScores'] += submissionScore
studentInfo[studentID]['totalSubmitted'] += 1
for studentID in list(studentInfo):
if studentInfo[studentID]['lowestPageID'] == None or studentInfo[studentID]['totalSubmitted'] == 0:
studentInfo.pop(studentID)
continue
studentInfo[studentID]['avgSubmissionScore'] = int(studentInfo[studentID]['totalScores'] / studentInfo[studentID]['totalSubmitted'])
sortedStudentInfo = sorted(studentInfo, key=lambda studentID: (studentInfo[studentID]['lowestPageID'],studentInfo[studentID]['latestPageID'],studentInfo[studentID]['avgSubmissionScore']))
return [studentInfo, sortedStudentInfo]
def main():
# Get the filename from stdin
filename = input()
# Open the file and read in its contents
with open(filename) as data_file:
text = data_file.readlines() #read the lines
for i in range(len(text)):
text[i] = text[i].replace('\n','').split(" ")
logs = text[0]
text = text[1:]
printableStudentInfo = student_log(text)
studentInfo:dict[int, dict] = printableStudentInfo[0]
sortedStudentInfo = printableStudentInfo[1]
for studentID in sortedStudentInfo:
lowestPageID = studentInfo[studentID]['lowestPageID']
latestPageID = studentInfo[studentID]['latestPageID']
avgSubmissionScore = studentInfo[studentID]['avgSubmissionScore']
print(f'{studentID} {lowestPageID} {latestPageID} {avgSubmissionScore}')
if __name__ == "__main__":
main()