-
Notifications
You must be signed in to change notification settings - Fork 0
/
randomize-questions.py
433 lines (338 loc) · 11.6 KB
/
randomize-questions.py
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
#----------------------------------------------------------
# Will Bowman
# 1/24/2018
#
# Randomize order of exam questions
#
# Return new exam file and answer key
# New exam file (*.tex) can be compiled to pdf
#
#----------------------------------------------------------
#
# assumes input exam files have suffix *.dat and are of format
#
# QQ question text
# AA (1) correct response...
# (2) resonse 2 ...
#
# assumes response (1) is correct response
# assumes at least two responses per question
# assumes incorrect responses of form (#), i.e.,
# no characters before "(", and # element of set {1,2,3,4,5,...}
# assumes no empty lines within a question, i.e.,
# after start of question line --> end of last response
# assumes at least one EMPTY line between questions, i.e.,
# no whitespace!
# question or response can span multiple lines
#
# lines before / after exam questions will be preserved
#
#
#----------
# USAGE
#----------
#
# run as `python randomize_questions.py -i <input-file>
# option: `-o <name-modification-for-new-file>
# default: *_shuffled.dat
#
left_header = 'Astro 140, Spring 2018'
right_header = 'Unit Quiz 4'
### string to appear at beginning of exam
intro_statement = 'For each multiple choice question, select the best answer and bubble the corresponding letter on your scantron sheet. For the final free response question, write your answer on the quiz booklet. For multiple choice questions NVA means “Not a valid answer” and is never the correct choice. If you have questions, you may raise your hand and ask the instructor or quiz proctor. Otherwise, students are not allowed to give or receive information related to its contents during the in class unit quizzes. Students will be asked to sign a statement confirming this on the final page of the quiz. Finally, remember to write your name and student identification number on the first and last page of the quiz booklet and to bubble them into the scantron form. '
### list of all statements to appear at end of exam
#final_lines = []
final_lines = ['\\noindent While taking this quiz, I did not give or receive information related to its contents (with the possible exception of clarifications by the instructor or exam proctor).',
'\\bigskip',
'\\noindent Signature $\\rule{6cm}{0.2mm}$ ']
# if False, responses are (1), (2), (3), ...
alphabetical_responses = True # False
# if False, leave AA prefix on correct responses
remove_AA = True # False
# if True, remove lines *beginning* with #
remove_comments = True
#!!! NOTE: TO EXTEND TO ALL COMMENTS, USE `line.replace('#', '%#') --> comment out of TeX
# if True, change QQ --> Q1, Q2, ..., Q10, ...
number_questions = True # False
#------------------------------------------------------------------------------
import numpy as np
import argparse as ap
def set_response(rindex, upper=True):
rletter = chr(ord('a')+rindex)
if upper:
rletter = rletter.upper()
return rletter
def parse_args(argv=None):
"""Parse the command line arguments
Parameters
----------
argv : list of string
arguments to parse; if ``None``, ``sys.argv`` is used
Returns
-------
Namespace
parsed arguments
"""
parser = ap.ArgumentParser(description='shuffle exam questions',
formatter_class=ap.RawTextHelpFormatter)
parser.add_argument("-i","--finput", type=str, nargs='?',
help='''<input-file.dat>''',
default=None)
parser.add_argument("-o","--output", type=str, nargs='?',
help='''output-file-suffix''',
default='_shuffled')
args = parser.parse_args(args=argv)
if args.finput is None:
msg = 'Please fill in the input file'
parser.error(msg)
return args
def set_output(infile, suffix='_shuffled'):
return infile[0:-4]+suffix+'.tex'
def set_respfile(outfile):
return outfile[0:-4]+'-responses.dat'
def is_eof(f):
'if no more questions, return TRUE'
# get current position
pos0 = f.tell()
lines = f.readlines()
eof = True
for l in lines:
if len(l)>2:
if l[0:2]=='QQ':
eof = False
f.seek(pos0)
return eof
def get_full_question(f):
'''f is the test file object
return a list of [Q, A1, A2,...]
note: each item of list is a list containing each line of component'''
header = []
q = []
resp = []
line = '9753124680 get header lines'
while line[0:2] != 'QQ':
if line != '9753124680 get header lines':
header.append(line)
line = f.readline()
while line[0:2] != 'AA':
q.append(line)
line = f.readline()
resp1 = []
while line[0:3] != '(2)':
resp1.append(line)
line = f.readline()
resp.append(resp1)
nline=2
while line !='\n':
resp_new = []
while (line[0:2] != '('+str(nline+1)) & (line!='\n'):
resp_new.append( line )
line = f.readline()
resp.append( resp_new )
if line !='\n':
nline += 1
item = [header, q, resp]
return item
def random_correct_index(qitem, resparr):
'''qitem: list of question item, [header, question, response list]
resparr: list of correct responses'''
adjusted_list = []
nresp = len(qitem[2])
new_index = int(np.random.choice( np.linspace(0,nresp-1,nresp) ))
# print(new_index)
if new_index != 0:
adjusted_list.append( new_index )
qcopy = qitem[2].copy()
trade = qcopy[new_index]
trade[0] = '(1)'+trade[0][3:]
adjusted_correct = qcopy[0]
resp_line = adjusted_correct[0]
# numerical or alphabetical responses?
if alphabetical_responses:
resp = set_response( new_index )
else:
resp = str(new_index+1)
# print([new_index, resp])
# identify where correct response begins, i.e., (1)
i=0
char = 'A'
while char != ')':
i+=1
char = resp_line[i]
if remove_AA:
adjusted_correct[0] = '('+resp+')'+adjusted_correct[0][i+1:]
else:
adjusted_correct[0] = adjusted_correct[0][0:i-2]+'('+resp+')'+adjusted_correct[0][i+1:]
qitem[2][new_index] = adjusted_correct
qitem[2][0] = trade
else:
adjusted_list.append( new_index )
qcopy = qitem[2].copy()
adjusted_correct = qcopy[0]
resp_line = adjusted_correct[0]
if alphabetical_responses:
resp = set_response( new_index )
else:
resp = str(new_index+1)
# identify where correct response begins, i.e., (1)
i=0
char = 'A'
while char != ')':
i+=1
char = resp_line[i]
if remove_AA:
adjusted_correct[0] = '('+resp+')'+adjusted_correct[0][i+1:]
else:
adjusted_correct[0] = adjusted_correct[0][0:i-2]+'('+resp+')'+adjusted_correct[0][i+1:]
qitem[2][new_index] = adjusted_correct
qlist = qitem[2]
# set desired response identifiers
for q in qlist:
if qlist.index(q) not in adjusted_list:
if alphabetical_responses:
resp = set_response( qlist.index(q) )
else:
resp = str(qlist.index(q)+1)
q[0] = '('+resp+')'+q[0][3:]
# update answer key
if alphabetical_responses:
resp_correct = set_response( new_index )
else:
resp_correct = str(new_index+1)
resparr.append([len(resparr)+1, resp_correct])
def add_suffix(line, suffix= ' \\\\'):
# print(repr(line))
line += suffix
# print(repr(line))
return line
def write_question(qitem, fout, remove_comments=remove_comments):
fout.write('\n')
fout.write('\\begin{absolutelynopagebreak}')
for part in qitem:
for p in part:
if type(p) is list:
for q in p:
q = add_suffix(q)
if remove_comments:
if q[0]!='#':
fout.write(q)
else:
fout.write(q)
else:
p = add_suffix(p)
if remove_comments:
if p[0]!='#':
fout.write(p)
else:
fout.write(p)
# fout.write(p)
fout.write('\\end{absolutelynopagebreak}')
fout.write('\n')
def write_final_remarks(f, fout, remove_comments=remove_comments):
'''write last items to new file
fout: file object'''
final = f.readlines()
for l in final:
l = add_suffix(l)
if remove_comments:
if l[0]!='#':
fout.write(l)
else:
fout.write(l)
def write_tex_end(fout, lines=None):
if not lines:
line_list = ['\end{document}']
else:
line_list = ['','\\vfill']
for l in lines:
l = add_suffix(l)
line_list.append(l)
line_list.append('\end{document}')
for l in line_list:
fout.write('\n'+l)
def write_tex_header(fout, header=True):
line_list = ['\documentclass[11pt]{article}',
'\\usepackage[margin=1in]{geometry}',
'\\usepackage[]{amsmath}',
'\\newenvironment{absolutelynopagebreak}',
' {\par\\nobreak\\vfil\penalty0\\vfilneg',
' \\vtop\\bgroup}',
' {\par\\xdef\\tpd{\\the\prevdepth}\egroup',
' \prevdepth=\\tpd}', '']
if header:
line_list.append('\\usepackage{fancyhdr}')
line_list.append('\pagestyle{fancy}')
line_list.append('\\renewcommand{\headrulewidth}{1pt}')
line_list.append('\\fancyhead[L]{'+left_header+'}')
line_list.append('\\fancyhead[R]{'+right_header+'}')
line_list.append('\\begin{document} \n')
else:
line_list.append('\\begin{document} \n')
for l in line_list:
fout.write('\n'+l)
def write_file_header(fout, more_text=None, length1=5, length2=3.5):
head_list = ['\\noindent Name $\\rule{'+str(length1)+'cm}{0.2mm}$ '+\
'\hfill Student ID $\\rule{'+str(length2)+'cm}{0.2mm}$']
if more_text:
addlines = ['', '\\bigskip', more_text,'','\\bigskip',''] #,'','\\bigskip']
else:
addlines = ['', '\\bigskip','']
for l in addlines:
head_list.append(l)
for l in head_list:
fout.write('\n'+l)
def print_listarr_to_file(arr, head='', fileout='',remove_par=False):
"output a list of lists (2d array) to a file"
np.set_printoptions(suppress=True, linewidth=20000)
if fileout=='':
f = input('Enter name of output file:\n')
else:
f = fileout
if remove_par==False:
resp_par = input("Remove apostrophes from strings? (y to accept)\n")
if resp_par=='y':
remove_par=True
with open(f,'w') as infile:
if head != '':
infile.write('# ' + head + '\n#\n')
for i in range(0,np.shape(arr)[0]):
line = str(arr[i])
line = line.replace("["," ")
line = line.replace("]"," ")
line = line.replace("("," ")
line = line.replace(")"," ")
line = line.replace(","," ")
if remove_par==True:
line = line.replace("'"," ")
infile.write(line+'\n')
def write_response_file(resparr, respfout):
'''resparr: list of responses
respfout: name of output file'''
print_listarr_to_file(resparr, head='QQ correct', \
fileout=respfout,remove_par=True)
def main(argv=None):
args = parse_args(argv)
fnamein = args.finput
fnameout = set_output(fnamein, suffix=args.output)
respfout = set_respfile(fnameout)
with open(fnamein,'r') as fin:
with open(fnameout,'w') as fout:
write_tex_header(fout)
write_file_header(fout, more_text=intro_statement)
eof=False
resparr = []
while not eof:
qitem = get_full_question(fin)
random_correct_index(qitem, resparr)
if number_questions:
nquestion = len(resparr)
qitem[1][0] = '\\noindent \\textbf{Question '+str(nquestion)+':} '+qitem[1][0][2:]
write_question(qitem, fout)
eof = is_eof(fin)
fout.write('\n\pagebreak\n')
# write_file_header(fout,more_text='\\textbf{Free Response Question:}')
write_final_remarks(fin, fout)
write_tex_end(fout, lines=final_lines)
write_response_file(resparr, respfout)
if __name__ == '__main__':
main()