Skip to content

Instantly share code, notes, and snippets.

@akhilesh-kumar-verma
Last active March 1, 2025 18:56
Show Gist options
  • Save akhilesh-kumar-verma/d4e53e210c8875f5ce0d3014d8092646 to your computer and use it in GitHub Desktop.
Save akhilesh-kumar-verma/d4e53e210c8875f5ce0d3014d8092646 to your computer and use it in GitHub Desktop.
#! /usr/bin/python3
with open('resp.txt') as fil: lines=fil.readlines()
qids={int(line.split()[-1]) for line in lines if
line.startswith('Question ID')}
i,offset=0,min(qids)
print('Calculated offset')
data=[None]*65
while i<len(lines):
if (lines[i].startswith('Question Type')):
qid=int(lines[i+1].split()[-1])-offset
qtype=lines[i].split()[-1]
ans=lines[i+3].split()[-1]
elif (lines[i].startswith('Given Answer')):
qid=int(lines[i+2].split()[-1])-offset
qtype='NAT'
ans=lines[i].split()[-1]
else:
i+=1
continue
i+=4
data[qid]=(qtype, ans)
print('Extracted responses')
with open('key.txt') as fil: lines=fil.readlines()
key=[None]*65 # elements will be (type, ans, marks)
i=0
for line in lines:
if line.startswith(str(i+1)):
temp=line.split()
if temp[2]=='NAT':
key[i]=('NAT',(float(temp[4]),
float(temp[6])),int(temp[7]))
else:
key[i]=(temp[2],temp[4],int(temp[5]))
i+=1
print('Extracted Keys')
def points(key,ans):
if key[0]!=ans[0]:
Exception('Question type Mismatch.')
multiplier=0
if ('--'==ans[1]):
multiplier=0
elif 'MCQ'==key[0]:
multiplier=1 if key[1]==ans[1] else -1/3
elif 'MSQ'==key[0]:
multiplier=1 if key[1].split(';')==\
ans[1].split(',') else 0
elif 'NAT'==key[0]:
tval=float(ans[1])# typed value
multiplier=1 if tval>=key[1][0] and\
tval<=key[1][1] else 0
else:
Exception(f'{key[0]} is not a valid Question type.')
return multiplier*key[2]
total=0
for i in range(65):
total+=points(key[i],data[i])
print(f'Marks: {total:.2f}/100.00')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment