File size: 58,650 Bytes
1113aa2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4023b62
 
 
667ca9b
 
1113aa2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a5a4d86
1113aa2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
086ef72
 
 
 
a24c4c2
cbaff5a
 
 
 
a24c4c2
cbaff5a
 
 
 
 
 
 
 
 
 
 
 
a24c4c2
 
 
 
 
 
 
 
 
 
1113aa2
 
 
 
6b6f0ae
a24c4c2
 
cbaff5a
a24c4c2
cbaff5a
 
 
2daf171
cbaff5a
a24c4c2
cbaff5a
1113aa2
 
a24c4c2
6b6f0ae
a5a4d86
 
 
 
 
 
 
 
 
 
 
 
 
1113aa2
a5a4d86
 
 
 
1113aa2
667ca9b
 
 
 
 
 
 
 
086ef72
667ca9b
 
a5a4d86
667ca9b
 
 
 
1113aa2
a24c4c2
1113aa2
 
a24c4c2
 
6b6f0ae
a5a4d86
1113aa2
a24c4c2
1113aa2
 
a24c4c2
 
6b6f0ae
1113aa2
c61080a
1113aa2
a24c4c2
1113aa2
 
a24c4c2
 
2daf171
a5a4d86
1113aa2
a5a4d86
eb9425b
1113aa2
eb9425b
 
1113aa2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a5a4d86
 
 
 
 
 
 
1113aa2
 
a24c4c2
1113aa2
 
a24c4c2
6b6f0ae
1113aa2
 
 
a24c4c2
cbaff5a
 
1113aa2
 
 
 
 
 
 
8cc8d07
1113aa2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a24c4c2
1113aa2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8cc8d07
1113aa2
a5a4d86
 
 
 
 
 
 
1113aa2
a5a4d86
8cc8d07
1113aa2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8cc8d07
1113aa2
 
 
 
 
 
086ef72
1113aa2
 
 
 
086ef72
1f3202d
086ef72
1113aa2
 
 
 
1f3202d
1113aa2
 
 
8cc8d07
1113aa2
 
 
 
 
 
 
eb9425b
1113aa2
 
 
 
 
 
 
8cc8d07
1113aa2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eb9425b
 
1113aa2
 
 
eb9425b
1113aa2
eb9425b
 
1113aa2
eb9425b
 
1113aa2
eb9425b
1113aa2
eb9425b
 
1113aa2
 
 
8cc8d07
1113aa2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eb9425b
 
1113aa2
a5a4d86
 
1113aa2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8cc8d07
 
21f8947
8cc8d07
 
 
 
 
 
 
 
 
 
 
 
eb9425b
8cc8d07
 
a5a4d86
 
eb9425b
8cc8d07
 
 
 
 
 
 
 
 
 
 
 
 
 
21f8947
8cc8d07
 
b89117c
 
 
 
 
8cc8d07
 
eb9425b
8cc8d07
 
a5a4d86
 
eb9425b
8cc8d07
 
21f8947
 
 
8cc8d07
 
21f8947
8cc8d07
 
 
 
 
 
 
 
 
a24c4c2
8cc8d07
 
 
 
a24c4c2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1113aa2
 
 
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Assignment 2 β€” Software Testing</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.16/codemirror.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.16/theme/material-darker.min.css">
<style>
  :root {
    --bg:#1e1e2e; --panel:#252535; --card:#22222f; --border:#353550;
    --accent:#89b4fa; --accent-hover:#74a0f0;
    --text:#cdd6f4; --muted:#7f849c; --err:#f38ba8; --ok:#a6e3a1; --warn:#f9e2af;
  }
  * { box-sizing:border-box; }
  html,body { margin:0; }
  body {
    font-family:-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,sans-serif;
    background:var(--bg); color:var(--text); line-height:1.55; font-size:14px;
  }
  .wrap { max-width:980px; margin:0 auto; padding:24px 28px 120px; }
  h1 { font-size:22px; margin:0 0 4px; }
  h2 { font-size:18px; margin:34px 0 6px; padding-top:14px; border-top:1px solid var(--border); }
  h2 .pts { font-size:12px; color:var(--muted); font-weight:500; }
  h3 { font-size:14px; color:var(--accent); margin:18px 0 6px; }
  p { margin:8px 0; }
  code, .mono { font-family:"SF Mono",Menlo,Consolas,monospace; }
  pre { background:#181825; border:1px solid var(--border); border-radius:8px; padding:12px 14px; overflow:auto; font-size:13px; }
  .muted { color:var(--muted); }
  .rules { color:var(--text); font-size:15px; line-height:1.7; background:var(--panel);
           border:1px solid var(--border); border-left:3px solid var(--accent);
           border-radius:8px; padding:10px 14px; margin:8px 0; }
  ol.rulelist { font-size:14px; line-height:1.7; padding-left:22px; margin:8px 0; }
  ol.rulelist li { margin:6px 0; }
  a { color:var(--accent); }
  table.dt { border-collapse:collapse; font-size:12px; font-family:"SF Mono",Menlo,Consolas,monospace; margin:4px 0; }
  table.dt th, table.dt td { border:1px solid var(--border); padding:3px 8px; text-align:center; }
  table.dt th { background:var(--panel); color:var(--muted); font-weight:600; }
  table.spec { border-collapse:collapse; font-size:13px; margin:8px 0; }
  table.spec th, table.spec td { border:1px solid var(--border); padding:5px 10px; text-align:left; }
  table.spec th { background:var(--panel); }

  /* option card with radio */
  .opt {
    border:1px solid var(--border); border-radius:10px; padding:10px 14px; margin:10px 0;
    background:var(--card); transition:border-color .12s, background .12s;
  }
  .opt.sel { border-color:var(--accent); background:#262a3f; }
  .opt-head { display:flex; align-items:center; gap:10px; cursor:pointer; font-weight:600; }
  .opt-head input { width:16px; height:16px; accent-color:var(--accent); cursor:pointer; }
  .opt-body { margin-top:8px; overflow:auto; }
  .opt-body img { max-width:100%; background:#fff; border-radius:6px; }
  .opt-body svg { display:block; width:100%; height:auto; background:#fff; border-radius:6px; }

  /* fill-in blanks */
  input.blank {
    width:64px; text-align:center; background:#181825; color:var(--ok);
    border:1px solid var(--border); border-radius:5px; padding:2px 4px;
    font-family:"SF Mono",Menlo,Consolas,monospace; font-size:13px; font-weight:600;
  }
  input.blank:focus { outline:none; border-color:var(--accent); }
  input.txt {
    background:#181825; color:var(--text); border:1px solid var(--border);
    border-radius:6px; padding:7px 10px; font-size:13px;
    font-family:"SF Mono",Menlo,Consolas,monospace;
  }
  input.txt:focus { outline:none; border-color:var(--accent); }
  .circ { display:inline-block; min-width:18px; color:var(--accent); font-weight:700; }

  button {
    background:var(--accent); color:#11111b; border:none; padding:8px 16px;
    border-radius:7px; font-size:13px; font-weight:700; cursor:pointer; transition:background .15s;
  }
  button:hover:not(:disabled){ background:var(--accent-hover); }
  button:disabled { opacity:.5; cursor:not-allowed; }
  button.ghost { background:transparent; color:var(--muted); border:1px solid var(--border); }
  button.ghost:hover:not(:disabled){ color:var(--text); }

  /* Q4 playground */
  .pg { border:1px solid var(--border); border-radius:10px; overflow:hidden; margin:12px 0; background:var(--card); }
  .pg-head { display:flex; align-items:center; gap:10px; padding:8px 12px; background:var(--panel); border-bottom:1px solid var(--border); flex-wrap:wrap; }
  .pg-head .status { font-size:12px; color:var(--muted); margin-left:auto; }
  .pg-head .status.ready { color:var(--ok); }
  .pg-head .status.error { color:var(--err); }
  .CodeMirror { height:auto; min-height:230px; font-size:13px; font-family:"SF Mono",Menlo,Consolas,monospace; }
  .pg-row { display:flex; gap:14px; flex-wrap:wrap; align-items:flex-end; padding:10px 12px; border-top:1px solid var(--border); }
  .pg-row label { font-size:12px; color:var(--muted); display:block; margin-bottom:4px; }
  #q4out { margin:0; padding:12px 14px; border-top:1px solid var(--border); background:#181825; font-size:13px; white-space:pre-wrap; min-height:40px; }
  #q4out .ok{color:var(--ok);} #q4out .err{color:var(--err);} #q4out .warn{color:var(--warn);} #q4out .meta{color:var(--muted);}

  /* sticky export bar */
  .exportbar {
    position:fixed; left:0; right:0; bottom:0; background:var(--panel);
    border-top:1px solid var(--border); padding:10px 28px;
    display:flex; align-items:center; gap:14px; z-index:10;
  }
  .exportbar .sid { display:flex; align-items:center; gap:8px; font-size:12px; color:var(--muted); }
  .exportbar .spacer { flex:1; }
  .toast { font-size:12px; color:var(--ok); }
  /* copy-swap: what is shown (CSS ::after, never copied) differs from what is copied
     (a zero-size real text node). Display and grading are unaffected. */
  .swap::after { content: attr(data-seen); }
  .swap .cp { font-size:0; line-height:0; }

  /* ===== left vertical tabs (Ant style) ===== */
  .layout { display:flex; gap:20px; align-items:flex-start; margin-top:16px; }
  .tabs { flex:0 0 190px; display:flex; flex-direction:column; gap:5px;
          position:sticky; top:16px; }
  .tabs button { background:transparent; color:var(--muted); border:none;
          border-left:3px solid transparent; border-radius:8px;
          padding:11px 14px; font-size:14px; font-weight:600; cursor:pointer;
          text-align:left; width:100%; white-space:nowrap;
          transition:color .15s, background .15s, border-color .15s; }
  .tabs button:hover:not(:disabled) { background:var(--panel); color:var(--text); }
  .tabs button.active { color:var(--accent); background:var(--card);
          border-left-color:var(--accent); border-radius:0 8px 8px 0; }
  .panes { flex:1; min-width:0; }
  .pane { display:none; }
  .pane.active { display:block; animation:tabfade .22s ease;
          background:var(--panel); border:1px solid var(--border);
          border-radius:12px; padding:20px 24px; }
  @keyframes tabfade { from{opacity:0; transform:translateY(4px)} to{opacity:1; transform:none} }
  .pane h2:first-child { margin-top:0; border-top:none; padding-top:0; }
  /* collapsible reference function */
  details.refbox { margin:0 0 16px; border:1px solid var(--border); border-radius:8px; background:var(--card); }
  details.refbox > summary { cursor:pointer; padding:9px 13px; font-weight:600; font-size:13px;
          color:var(--accent); list-style:none; user-select:none; }
  details.refbox > summary::-webkit-details-marker { display:none; }
  details.refbox > summary::before { content:"β–Έ "; }
  details.refbox[open] > summary::before { content:"β–Ύ "; }
  details.refbox pre { margin:0; border:none; border-top:1px solid var(--border); border-radius:0 0 8px 8px; }
</style>
</head>
<body>
<div class="wrap">
  <h1>Software Testing β€” Assignment 2 <span class="pts" style="font-size:13px">[15 marks total]</span></h1>
  <p class="muted">Answer all questions below β€” one tab per question. Q4 has an interactive playground. Your answers auto-save in this browser; export them as JSON at the bottom when done.</p>

  <div class="layout">
  <div class="tabs" id="tabbar">
    <button data-tab="q1" class="active">Q1 Β· Decision Table</button>
    <button data-tab="q2">Q2 Β· Control-Flow Graph</button>
    <button data-tab="q3">Q3 Β· Loop Boundary</button>
    <button data-tab="q4">Q4 Β· Coverage and Bug</button>
    <button data-tab="q5">Q5 Β· Halstead</button>
  </div>
  <div class="panes">

  <!-- ============ Q1 ============ -->
  <section class="pane active" data-tab="q1">
  <h2>Q1 &nbsp;Extended Entry Decision Table <span class="pts">[3 pts] Β· single choice</span></h2>
  <p>Test the UNSW Lecture-Theatre Booking system with Decision-Table testing. The system evaluates multi-valued conditions across several inputs to decide whether a booking is Booked, Wait-listed, or Rejected. Rules are checked one by one and the <b>first matching rule</b> applies.</p>

  <h3>Input specification</h3>
  <table class="spec">
    <tr><th>Input</th><th>Domain values</th><th>In your submission</th></tr>
    <tr><td>room_size</td><td>Small, Medium, Large</td><td>1=Small, 2=Medium, 3=Large</td></tr>
    <tr><td>time_slot</td><td>Morning, Afternoon, Evening</td><td>1=Morning, 2=Afternoon, 3=Evening</td></tr>
    <tr><td>week</td><td>1 to 12</td><td>Numeric as is</td></tr>
    <tr><td>equipment</td><td>Standard, AV, ComputerLab, Hybrid</td><td>1=Standard, 2=AV, 3=ComputerLab, 4=Hybrid</td></tr>
    <tr><td>staff_role</td><td>Academic, Admin, Guest</td><td>1=Academic, 2=Admin, 3=Guest</td></tr>
  </table>

  <h3>Output codes</h3>
  <table class="spec">
    <tr><th>Code</th><th>Status</th><th>Typical reason</th></tr>
    <tr><td>0</td><td>Booked</td><td>Request satisfies all constraints; first available room assigned</td></tr>
    <tr><td>1</td><td>Wait-list</td><td>Request valid but cannot be immediately fulfilled</td></tr>
    <tr><td>2</td><td>Rejected</td><td>Request violates system rules or contains invalid input</td></tr>
  </table>

  <h3>Program description</h3>
  <p class="muted" style="margin:4px 0">The program checks each rule one by one.</p>
  <ol class="rulelist">
    <li><b>A. Invalid Default.</b> If any input contains an invalid value, the request is immediately rejected. Room size must be Small, Medium, or Large; time slot Morning, Afternoon, or Evening; week between 1 and 12; equipment Standard, AV, ComputerLab, or Hybrid; staff role Academic, Admin, or Guest.</li>
    <li><b>B. Exam-week nest.</b> In exam weeks 11–12, only academic staff may book; everyone else is rejected. Even academics cannot request a Large theatre β€” those halls are blocked out for exams.</li>
    <li><b>C. Large-room.</b> Any request for a Large lecture theatre must clear three hurdles, otherwise rejected: (1) Evening slot (large rooms close after 17:00); (2) Computer-Lab equipment (there are no large computer labs); (3) Guest requester (guests may not reserve large rooms).</li>
    <li><b>D. Equipment-specific.</b> A Small room plus Hybrid kit is rejected.</li>
    <li><b>E. Guest + ComputerLab.</b> A guest wanting a computer lab is never accepted outright during teaching weeks 1–10: early weeks 1–4 <span class="swap" data-seen="wait-list"><span class="cp">rejected</span></span>, mid-semester 5–8 <span class="swap" data-seen="wait-list"><span class="cp">rejected</span></span>, late weeks 9–10 <span class="swap" data-seen="wait-list"><span class="cp">rejected</span></span>.</li>
    <li><b>F. Admin + Small.</b> An admin requesting a Small room is wait-listed if either the slot is in the evening, or it is exam week.</li>
    <li><b>G. First-matching-theatre book.</b> If none of the above rules block the request, the system books the first room that matches the requested size, contains the requested equipment, and (for evening bookings) allows evening use.</li>
    <li><b>H. Fallback.</b> If no room can be booked after all checks, the request is put on a general wait-list with the reason β€œNo matching theatre currently available”. <span class="muted">(Rule H is outside the scope of this decision table.)</span></li>
  </ol>
  <p class="muted">Assume that whenever a valid request is not blocked by Rules A–F, at least one matching theatre is always available.</p>

  <p class="rules"><b>Notation.</b> <code>!</code> = not; <code>/</code> = alternative; <code>-</code> = a range (e.g. room_size <code>1-3</code>) or a don't-care. Week ranges are inclusive. The Default row applies when inputs are valid but match no prior rule. If a rule is already caught by an earlier rule, its outcome is <code>N/A</code>. Invalid rows use an out-of-domain value such as room_size 0 or week 13. <b>Exactly one table below is correct β€” select it.</b></p>
  <div id="q1opts"></div>
  </section>

  <!-- ============ Q2 ============ -->
  <section class="pane" data-tab="q2">
  <details class="refbox" open><summary>Reference function β€” binary_search (used in Q2–Q4)</summary><pre class="reffn"></pre></details>
  <h2>Q2 &nbsp;Control-Flow Graph <span class="pts">[2 pts] Β· single choice</span></h2>
  <p>Draw the control-flow graph for <code>binary_search</code>. Below are four candidate CFGs, written as complete DOT graphs and rendered. Exactly one is correct β€” select it.</p>
  <div id="q2opts"></div>
  </section>

  <!-- ============ Q3 ============ -->
  <section class="pane" data-tab="q3">
  <details class="refbox" open><summary>Reference function β€” binary_search (used in Q2–Q4)</summary><pre class="reffn"></pre></details>
  <h2>Q3 &nbsp;Loop-Boundary Test Cases <span class="pts">[2 pts] Β· fill in the blanks</span></h2>
  <p>Develop a test suite achieving loop-boundary coverage (loop runs 0, 1, and 2 times). Fill each circled blank. Recall <code>mid = int(round((top+bott)/2.0))</code>.</p>
  <p class="muted">Marking: each wrong blank βˆ’0.5; four or more wrong blanks = 0.</p>
  <pre id="q3block"></pre>
  </section>

  <!-- ============ Q4 ============ -->
  <section class="pane" data-tab="q4">
  <details class="refbox" open><summary>Reference function β€” binary_search (used in Q2–Q4)</summary><pre class="reffn"></pre></details>
  <h2>Q4 &nbsp;Coverage and Bug <span class="pts">[6 pts] Β· playground</span></h2>
  <p class="muted">The Q3 suite reaches loop-boundary coverage, but coverage does not guarantee fault detection. This question asks you to demonstrate that gap on the <code>binary_search</code> function above.</p>
  <p>(a) Introduce <b>exactly one</b> single-line bug into <code>binary_search</code> so that <b>all three Q3 tests still pass</b> (the bug is invisible to the Q3 suite).
     (b) Provide <b>one</b> extra test case β€” with <code>target</code>, <code>key</code>, the <b>original output</b>, and the <b>variant output</b> β€” that <b>distinguishes</b> the variant from the original (original passes it, variant fails).</p>
  <p class="muted">Use the playground: edit the function to your variant, enter your distinguishing test, and press <b>Run check</b>. It reports whether your variant still passes the Q3 suite and whether your test separates the two.</p>

  <p style="margin-top:14px"><b>(a) Describe your one-line change</b> (form <code>from &rarr; to</code>):</p>
  <input class="txt" id="q4change" style="width:100%" placeholder="e.g.  top = mid - 1   ->   top = mid - 2">

  <div class="pg">
    <div class="pg-head">
      <b>Q4 Playground</b>
      <button id="q4run" disabled>β–Ά Run check</button>
      <button id="q4reset" class="ghost">Reset to original</button>
      <span class="status" id="q4status">Loading Python runtime…</span>
    </div>
    <textarea id="q4code"></textarea>
    <div class="pg-row">
      <div><label>(b) Distinguishing test β€” target (Python list)</label>
        <input class="txt" id="q4target" placeholder="[1, 3, 5, 7]" style="width:220px"></div>
      <div><label>key</label>
        <input class="txt" id="q4key" placeholder="3" style="width:90px"></div>
    </div>
    <div class="pg-row">
      <div><label>original output</label>
        <input class="txt" id="q4origout" placeholder="(found=True, location=1)" style="width:220px"></div>
      <div><label>variant output</label>
        <input class="txt" id="q4varout" placeholder="(found=False, location=0)" style="width:220px"></div>
      <div style="font-size:11px;color:var(--muted);max-width:230px">Fill these in yourself. Run check also prints the actual outputs so you can confirm.</div>
    </div>
    <pre id="q4out"><span class="meta">(edit the function, fill the test, then Run check)</span></pre>
  </div>
  </section>

  <!-- ============ Q5 ============ -->
  <section class="pane" data-tab="q5">
  <h2>Q5 &nbsp;Halstead Programming Effort <span class="pts">[2 pts] Β· single choice</span></h2>
  <p>Calculate the Halstead <b>programming effort</b> for the following code (parentheses are not counted as operators):</p>
  <pre id="q5code"></pre>
  <div id="q5opts"></div>
  </section>
  </div><!-- /.panes -->
  </div><!-- /.layout -->
</div>

<!-- ============ sticky export bar ============ -->
<div class="exportbar">
  <div class="sid">Student ID <input class="txt" id="studentId" placeholder="z1234567" style="width:130px"></div>
  <div class="spacer"></div>
  <span class="toast" id="toast"></span>
  <button class="ghost" id="clearBtn">Clear saved</button>
  <button class="ghost" id="copyBtn">Copy JSON</button>
  <button id="exportBtn">– Export JSON</button>
</div>

<script src="https://cdn.jsdelivr.net/pyodide/v0.26.2/full/pyodide.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.16/codemirror.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.16/mode/python/python.min.js"></script>
<script>
// ============ canonical reference function ============
const REF_SRC = `def binary_search(key: int, target: list[int]):
    size = len(target)
    bott = 0
    top = size - 1
    location = 0
    found = False
    if target[location] == key:
        found = True
    else:
        found = False
    while bott <= top and not found:
        mid = int(round((top + bott) / 2.0))
        if target[mid] == key:
            found = True
            location = mid
        else:
            if target[mid] < key:
                bott = mid + 1
            else:
                top = mid - 1
    return found, location`;
document.querySelectorAll(".reffn").forEach(el => el.textContent = REF_SRC);

// ============ Q1: six decision tables ============
const Q1_COLS = ["rule","room_size","time_slot","week","equipment","staff_role","outcome"];
const Q1 = {
  1: [["A1","0","-","-","-","-","2"],["A2","-","0","-","-","-","2"],["A3","-","-","13","-","-","2"],["A4","-","-","-","5","-","2"],["A5","-","-","-","-","4","2"],["B1","-","-","11-12","-","2/3","2"],["B2","3","-","11-12","-","1","2"],["C1","3","3","1-10","-","-","2"],["C3","3","-","1-10","-","3","2"],["D1","1","-","-","4","-","2"],["E1","1/2","-","1-10","3","3","1"],["F1","1","3","1-10","-","2","1"],["F2","1","-","11-12","-","2","N/A"],["G","-","-","-","-","-","0"]],
  2: [["A1","0","-","-","-","-","2"],["A2","-","0","-","-","-","2"],["A3","-","-","13","-","-","2"],["A4","-","-","-","5","-","2"],["A5","-","-","-","-","4","2"],["B1","-","-","11-12","-","2/3","2"],["B2","3","-","11-12","-","1","2"],["C1","3","3","1-10","-","-","2"],["C2","3","-","1-10","3","-","2"],["C3","3","-","1-10","-","3","2"],["D1","1","-","-","4","-","2"],["E1","1/2","-","1-10","3","3","2"],["F1","1","3","1-10","-","2","1"],["F2","1","-","11-12","-","2","N/A"],["G","-","-","-","-","-","0"]],
  3: [["A1","0","-","-","-","-","2"],["A2","-","0","-","-","-","2"],["A3","-","-","13","-","-","2"],["A4","-","-","-","5","-","2"],["A5","-","-","-","-","4","2"],["B1","-","-","11-12","-","2/3","2"],["B2","3","-","11-12","-","1","2"],["C1","3","3","1-10","-","-","2"],["C2","3","-","1-10","3","-","2"],["C3","-","-","1-10","-","3","2"],["D1","1","-","-","4","-","2"],["E1","1/2","-","1-10","3","3","1"],["F1","1","3","1-10","-","2","1"],["F2","1","-","11-12","-","2","N/A"],["G","-","-","-","-","-","0"]],
  4: [["A1","0","-","-","-","-","2"],["A2","-","0","-","-","-","2"],["A3","-","-","13","-","-","2"],["A4","-","-","-","5","-","2"],["A5","-","-","-","-","4","2"],["B1","-","-","10-12","-","2/3","2"],["B2","3","-","10-12","-","1","2"],["C1","3","3","1-9","-","-","2"],["C2","3","-","1-9","3","-","2"],["C3","3","-","1-9","-","3","2"],["D1","1","-","-","4","-","2"],["E1","1/2","-","1-9","3","3","1"],["F1","1","3","1-9","-","2","1"],["F2","1","-","10-12","-","2","N/A"],["G","-","-","-","-","-","0"]],
  5: [["A1","0","-","-","-","-","2"],["A2","-","0","-","-","-","2"],["A3","-","-","13","-","-","2"],["A4","-","-","-","5","-","2"],["A5","-","-","-","-","4","2"],["B1","-","-","11-12","-","2/3","2"],["B2","3","-","11-12","-","1","2"],["C1","3","3","1-10","-","-","2"],["C2","3","-","1-10","3","-","2"],["C3","3","-","1-10","-","3","2"],["D1","1","-","-","4","-","2"],["E1","1/2","-","1-10","3","3","1"],["F1","1","3","1-10","-","2","1"],["F2","1","-","11-12","-","2","N/A"],["G","-","-","-","-","-","0"]],
  6: [["A1","0","-","-","-","-","2"],["A2","-","0","-","-","-","2"],["A3","-","-","13","-","-","2"],["A4","-","-","-","5","-","2"],["A5","-","-","-","-","4","2"],["B1","-","-","11-12","-","3","2"],["B2","3","-","11-12","-","1","2"],["C1","3","3","1-10","-","-","2"],["C2","3","-","1-10","3","-","2"],["C3","3","-","1-10","-","3","2"],["D1","1","-","-","4","-","2"],["E1","1/2","-","1-10","3","3","1"],["F1","1","3","1-10","-","2","1"],["F2","1","-","11-12","-","2","N/A"],["G","-","-","-","-","-","0"]],
};
function dtTable(rows){
  let h = "<table class='dt'><tr>" + Q1_COLS.map(c=>`<th>${c}</th>`).join("") + "</tr>";
  for (const r of rows) h += "<tr>" + r.map(x=>`<td>${x}</td>`).join("") + "</tr>";
  return h + "</table>";
}
const answers = { q1:null, q2:null, q3:{}, q5:null };
function buildChoice(host, name, items, onSel){
  host.innerHTML = "";
  for (const it of items){
    const card = document.createElement("div"); card.className = "opt";
    card.innerHTML = `<label class="opt-head"><input type="radio" name="${name}" value="${it.val}"><span>${it.label}</span></label><div class="opt-body">${it.body}</div>`;
    const radio = card.querySelector("input");
    radio.onchange = () => {
      [...host.querySelectorAll(".opt")].forEach(o=>o.classList.remove("sel"));
      card.classList.add("sel"); onSel(it.val);
    };
    host.appendChild(card);
  }
}
buildChoice(document.getElementById("q1opts"), "q1",
  [1,2,3,4,5,6].map(n=>({val:String(n), label:"Option "+n, body:dtTable(Q1[n])})),
  v => { answers.q1 = v; saveSession(); });

// ============ Q2: four CFG images (inline SVG β€” no extra request, no decode) ============
const Q2_SVG = [
`<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 680 700" font-family="Helvetica,Arial,sans-serif"><rect width="680" height="700" fill="#fff"/><defs><marker id="arr" markerWidth="9" markerHeight="9" refX="7.5" refY="3" orient="auto" markerUnits="userSpaceOnUse"><path d="M0,0 L8,3 L0,6 Z" fill="#222"/></marker></defs><path d="M340.0,76.0 L340.0,94.0" fill="none" stroke="#222" stroke-width="1.2" marker-end="url(#arr)"/><path d="M316.7,138.7 L274.0,172.8" fill="none" stroke="#222" stroke-width="1.2" marker-end="url(#arr)"/><rect x="291.5" y="145.0" width="13" height="14" fill="#fff" opacity="0.9"/><text x="297.5" y="157.0" text-anchor="middle" font-size="12" font-style="italic" fill="#333">T</text><path d="M365.1,138.1 L413.6,173.0" fill="none" stroke="#222" stroke-width="1.2" marker-end="url(#arr)"/><rect x="381.0" y="144.8" width="13" height="14" fill="#fff" opacity="0.9"/><text x="387.0" y="156.8" text-anchor="middle" font-size="12" font-style="italic" fill="#333">F</text><path d="M272.1,211.6 L316.8,251.4" fill="none" stroke="#222" stroke-width="1.2" marker-end="url(#arr)"/><path d="M415.7,211.5 L365.2,251.8" fill="none" stroke="#222" stroke-width="1.2" marker-end="url(#arr)"/><path d="M340.0,298.0 L340.0,326.0" fill="none" stroke="#222" stroke-width="1.2" marker-end="url(#arr)"/><rect x="334.0" y="301.6" width="13" height="14" fill="#fff" opacity="0.9"/><text x="340.0" y="313.6" text-anchor="middle" font-size="12" font-style="italic" fill="#333">T</text><path d="M340.0,370.0 L340.0,398.0" fill="none" stroke="#222" stroke-width="1.2" marker-end="url(#arr)"/><path d="M304.1,439.6 L209.7,480.6" fill="none" stroke="#222" stroke-width="1.2" marker-end="url(#arr)"/><rect x="255.7" y="449.0" width="13" height="14" fill="#fff" opacity="0.9"/><text x="261.7" y="461.0" text-anchor="middle" font-size="12" font-style="italic" fill="#333">T</text><path d="M353.9,446.0 L374.2,478.2" fill="none" stroke="#222" stroke-width="1.2" marker-end="url(#arr)"/><rect x="357.0" y="451.5" width="13" height="14" fill="#fff" opacity="0.9"/><text x="363.0" y="463.5" text-anchor="middle" font-size="12" font-style="italic" fill="#333">F</text><path d="M372.0,521.2 L335.7,569.2" fill="none" stroke="#222" stroke-width="1.2" marker-end="url(#arr)"/><rect x="349.7" y="533.8" width="13" height="14" fill="#fff" opacity="0.9"/><text x="355.7" y="545.8" text-anchor="middle" font-size="12" font-style="italic" fill="#333">T</text><path d="M406.6,520.4 L451.6,569.9" fill="none" stroke="#222" stroke-width="1.2" marker-end="url(#arr)"/><rect x="420.9" y="533.6" width="13" height="14" fill="#fff" opacity="0.9"/><text x="426.9" y="545.6" text-anchor="middle" font-size="12" font-style="italic" fill="#333">F</text><path d="M371.6,290.7 Q668.0,466.0 337.8,640.1" fill="none" stroke="#222" stroke-width="1.2" marker-end="url(#arr)"/><rect x="505.3" y="456.7" width="13" height="14" fill="#fff" opacity="0.9"/><text x="511.3" y="468.7" text-anchor="middle" font-size="12" font-style="italic" fill="#333">F</text><path d="M183.2,521.6 L281.8,638.5" fill="none" stroke="#222" stroke-width="1.2" marker-end="url(#arr)"/><path d="M313.8,611.8 L306.3,638.1" fill="none" stroke="#222" stroke-width="1.2" marker-end="url(#arr)"/><path d="M435.3,604.3 L345.8,641.2" fill="none" stroke="#222" stroke-width="1.2" marker-end="url(#arr)"/><ellipse cx="340" cy="42" rx="99.5" ry="34" fill="#fff" stroke="#222" stroke-width="1.3"/><text x="340" y="38" text-anchor="middle" font-size="13">size; bott=0; top=size-1;</text><text x="340" y="54" text-anchor="middle" font-size="13">location=0; found=False</text><polygon points="340,94 422.8,120 340,146 257.2,120" fill="#fff" stroke="#222" stroke-width="1.3"/><text x="340" y="124" text-anchor="middle" font-size="13">target[0]==key ?</text><ellipse cx="250" cy="192" rx="49.0" ry="22" fill="#fff" stroke="#222" stroke-width="1.3"/><text x="250" y="196" text-anchor="middle" font-size="13">found=True</text><ellipse cx="440" cy="192" rx="52.3" ry="22" fill="#fff" stroke="#222" stroke-width="1.3"/><text x="440" y="196" text-anchor="middle" font-size="13">found=False</text><polygon points="340,246 452.5,272 340,298 227.5,272" fill="#fff" stroke="#222" stroke-width="1.3"/><text x="340" y="276" text-anchor="middle" font-size="13">bott&lt;=top and not found ?</text><ellipse cx="340" cy="348" rx="115.0" ry="22" fill="#fff" stroke="#222" stroke-width="1.3"/><text x="340" y="352" text-anchor="middle" font-size="13">mid=int(round((top+bott)/2.0))</text><polygon points="340,398 429.4,424 340,450 250.6,424" fill="#fff" stroke="#222" stroke-width="1.3"/><text x="340" y="428" text-anchor="middle" font-size="13">target[mid]==key ?</text><ellipse cx="165" cy="500" rx="95.19999999999999" ry="22" fill="#fff" stroke="#222" stroke-width="1.3"/><text x="165" y="504" text-anchor="middle" font-size="13">found=True; location=mid</text><polygon points="388,474 474.1,500 388,526 301.9,500" fill="#fff" stroke="#222" stroke-width="1.3"/><text x="388" y="504" text-anchor="middle" font-size="13">target[mid]&lt;key ?</text><ellipse cx="320" cy="590" rx="49.0" ry="22" fill="#fff" stroke="#222" stroke-width="1.3"/><text x="320" y="594" text-anchor="middle" font-size="13">bott=mid+1</text><ellipse cx="470" cy="590" rx="45.7" ry="22" fill="#fff" stroke="#222" stroke-width="1.3"/><text x="470" y="594" text-anchor="middle" font-size="13">top=mid-1</text><ellipse cx="300" cy="660" rx="88.6" ry="22" fill="#fff" stroke="#222" stroke-width="1.3"/><text x="300" y="664" text-anchor="middle" font-size="13">return found, location</text></svg>`,
`<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 680 700" font-family="Helvetica,Arial,sans-serif"><rect width="680" height="700" fill="#fff"/><defs><marker id="arr" markerWidth="9" markerHeight="9" refX="7.5" refY="3" orient="auto" markerUnits="userSpaceOnUse"><path d="M0,0 L8,3 L0,6 Z" fill="#222"/></marker></defs><path d="M340.0,76.0 L340.0,94.0" fill="none" stroke="#222" stroke-width="1.2" marker-end="url(#arr)"/><path d="M316.7,138.7 L274.0,172.8" fill="none" stroke="#222" stroke-width="1.2" marker-end="url(#arr)"/><rect x="291.5" y="145.0" width="13" height="14" fill="#fff" opacity="0.9"/><text x="297.5" y="157.0" text-anchor="middle" font-size="12" font-style="italic" fill="#333">T</text><path d="M365.1,138.1 L413.6,173.0" fill="none" stroke="#222" stroke-width="1.2" marker-end="url(#arr)"/><rect x="381.0" y="144.8" width="13" height="14" fill="#fff" opacity="0.9"/><text x="387.0" y="156.8" text-anchor="middle" font-size="12" font-style="italic" fill="#333">F</text><path d="M272.1,211.6 L316.8,251.4" fill="none" stroke="#222" stroke-width="1.2" marker-end="url(#arr)"/><path d="M415.7,211.5 L365.2,251.8" fill="none" stroke="#222" stroke-width="1.2" marker-end="url(#arr)"/><path d="M340.0,298.0 L340.0,326.0" fill="none" stroke="#222" stroke-width="1.2" marker-end="url(#arr)"/><rect x="334.0" y="301.6" width="13" height="14" fill="#fff" opacity="0.9"/><text x="340.0" y="313.6" text-anchor="middle" font-size="12" font-style="italic" fill="#333">T</text><path d="M340.0,370.0 L340.0,398.0" fill="none" stroke="#222" stroke-width="1.2" marker-end="url(#arr)"/><path d="M304.1,439.6 L209.7,480.6" fill="none" stroke="#222" stroke-width="1.2" marker-end="url(#arr)"/><rect x="255.7" y="449.0" width="13" height="14" fill="#fff" opacity="0.9"/><text x="261.7" y="461.0" text-anchor="middle" font-size="12" font-style="italic" fill="#333">T</text><path d="M353.9,446.0 L374.2,478.2" fill="none" stroke="#222" stroke-width="1.2" marker-end="url(#arr)"/><rect x="357.0" y="451.5" width="13" height="14" fill="#fff" opacity="0.9"/><text x="363.0" y="463.5" text-anchor="middle" font-size="12" font-style="italic" fill="#333">F</text><path d="M372.0,521.2 L335.7,569.2" fill="none" stroke="#222" stroke-width="1.2" marker-end="url(#arr)"/><rect x="349.7" y="533.8" width="13" height="14" fill="#fff" opacity="0.9"/><text x="355.7" y="545.8" text-anchor="middle" font-size="12" font-style="italic" fill="#333">T</text><path d="M406.6,520.4 L451.6,569.9" fill="none" stroke="#222" stroke-width="1.2" marker-end="url(#arr)"/><rect x="420.9" y="533.6" width="13" height="14" fill="#fff" opacity="0.9"/><text x="426.9" y="545.6" text-anchor="middle" font-size="12" font-style="italic" fill="#333">F</text><path d="M371.6,290.7 Q668.0,466.0 337.8,640.1" fill="none" stroke="#222" stroke-width="1.2" marker-end="url(#arr)"/><rect x="505.3" y="456.7" width="13" height="14" fill="#fff" opacity="0.9"/><text x="511.3" y="468.7" text-anchor="middle" font-size="12" font-style="italic" fill="#333">F</text><path d="M147.5,478.4 Q52.0,360.0 291.6,286.8" fill="none" stroke="#222" stroke-width="1.2" marker-end="url(#arr)"/><path d="M350.5,572.8 Q600.0,432.0 370.7,290.9" fill="none" stroke="#222" stroke-width="1.2" marker-end="url(#arr)"/><path d="M485.7,569.3 Q636.0,372.0 385.7,287.4" fill="none" stroke="#222" stroke-width="1.2" marker-end="url(#arr)"/><ellipse cx="340" cy="42" rx="99.5" ry="34" fill="#fff" stroke="#222" stroke-width="1.3"/><text x="340" y="38" text-anchor="middle" font-size="13">size; bott=0; top=size-1;</text><text x="340" y="54" text-anchor="middle" font-size="13">location=0; found=False</text><polygon points="340,94 422.8,120 340,146 257.2,120" fill="#fff" stroke="#222" stroke-width="1.3"/><text x="340" y="124" text-anchor="middle" font-size="13">target[0]==key ?</text><ellipse cx="250" cy="192" rx="49.0" ry="22" fill="#fff" stroke="#222" stroke-width="1.3"/><text x="250" y="196" text-anchor="middle" font-size="13">found=True</text><ellipse cx="440" cy="192" rx="52.3" ry="22" fill="#fff" stroke="#222" stroke-width="1.3"/><text x="440" y="196" text-anchor="middle" font-size="13">found=False</text><polygon points="340,246 452.5,272 340,298 227.5,272" fill="#fff" stroke="#222" stroke-width="1.3"/><text x="340" y="276" text-anchor="middle" font-size="13">bott&lt;=top and not found ?</text><ellipse cx="340" cy="348" rx="115.0" ry="22" fill="#fff" stroke="#222" stroke-width="1.3"/><text x="340" y="352" text-anchor="middle" font-size="13">mid=int(round((top+bott)/2.0))</text><polygon points="340,398 429.4,424 340,450 250.6,424" fill="#fff" stroke="#222" stroke-width="1.3"/><text x="340" y="428" text-anchor="middle" font-size="13">target[mid]==key ?</text><ellipse cx="165" cy="500" rx="95.19999999999999" ry="22" fill="#fff" stroke="#222" stroke-width="1.3"/><text x="165" y="504" text-anchor="middle" font-size="13">found=True; location=mid</text><polygon points="388,474 474.1,500 388,526 301.9,500" fill="#fff" stroke="#222" stroke-width="1.3"/><text x="388" y="504" text-anchor="middle" font-size="13">target[mid]&lt;key ?</text><ellipse cx="320" cy="590" rx="49.0" ry="22" fill="#fff" stroke="#222" stroke-width="1.3"/><text x="320" y="594" text-anchor="middle" font-size="13">bott=mid+1</text><ellipse cx="470" cy="590" rx="45.7" ry="22" fill="#fff" stroke="#222" stroke-width="1.3"/><text x="470" y="594" text-anchor="middle" font-size="13">top=mid-1</text><ellipse cx="300" cy="660" rx="88.6" ry="22" fill="#fff" stroke="#222" stroke-width="1.3"/><text x="300" y="664" text-anchor="middle" font-size="13">return found, location</text></svg>`,
`<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 680 700" font-family="Helvetica,Arial,sans-serif"><rect width="680" height="700" fill="#fff"/><defs><marker id="arr" markerWidth="9" markerHeight="9" refX="7.5" refY="3" orient="auto" markerUnits="userSpaceOnUse"><path d="M0,0 L8,3 L0,6 Z" fill="#222"/></marker></defs><path d="M340.0,76.0 L340.0,94.0" fill="none" stroke="#222" stroke-width="1.2" marker-end="url(#arr)"/><path d="M316.7,138.7 L274.0,172.8" fill="none" stroke="#222" stroke-width="1.2" marker-end="url(#arr)"/><rect x="291.5" y="145.0" width="13" height="14" fill="#fff" opacity="0.9"/><text x="297.5" y="157.0" text-anchor="middle" font-size="12" font-style="italic" fill="#333">T</text><path d="M365.1,138.1 L413.6,173.0" fill="none" stroke="#222" stroke-width="1.2" marker-end="url(#arr)"/><rect x="381.0" y="144.8" width="13" height="14" fill="#fff" opacity="0.9"/><text x="387.0" y="156.8" text-anchor="middle" font-size="12" font-style="italic" fill="#333">F</text><path d="M272.1,211.6 L316.8,251.4" fill="none" stroke="#222" stroke-width="1.2" marker-end="url(#arr)"/><path d="M415.7,211.5 L365.2,251.8" fill="none" stroke="#222" stroke-width="1.2" marker-end="url(#arr)"/><path d="M340.0,298.0 L340.0,326.0" fill="none" stroke="#222" stroke-width="1.2" marker-end="url(#arr)"/><rect x="334.0" y="301.6" width="13" height="14" fill="#fff" opacity="0.9"/><text x="340.0" y="313.6" text-anchor="middle" font-size="12" font-style="italic" fill="#333">T</text><path d="M340.0,370.0 L340.0,398.0" fill="none" stroke="#222" stroke-width="1.2" marker-end="url(#arr)"/><path d="M304.1,439.6 L209.7,480.6" fill="none" stroke="#222" stroke-width="1.2" marker-end="url(#arr)"/><rect x="255.7" y="449.0" width="13" height="14" fill="#fff" opacity="0.9"/><text x="261.7" y="461.0" text-anchor="middle" font-size="12" font-style="italic" fill="#333">T</text><path d="M353.9,446.0 L374.2,478.2" fill="none" stroke="#222" stroke-width="1.2" marker-end="url(#arr)"/><rect x="357.0" y="451.5" width="13" height="14" fill="#fff" opacity="0.9"/><text x="363.0" y="463.5" text-anchor="middle" font-size="12" font-style="italic" fill="#333">F</text><path d="M372.0,521.2 L335.7,569.2" fill="none" stroke="#222" stroke-width="1.2" marker-end="url(#arr)"/><rect x="349.7" y="533.8" width="13" height="14" fill="#fff" opacity="0.9"/><text x="355.7" y="545.8" text-anchor="middle" font-size="12" font-style="italic" fill="#333">T</text><path d="M406.6,520.4 L451.6,569.9" fill="none" stroke="#222" stroke-width="1.2" marker-end="url(#arr)"/><rect x="420.9" y="533.6" width="13" height="14" fill="#fff" opacity="0.9"/><text x="426.9" y="545.6" text-anchor="middle" font-size="12" font-style="italic" fill="#333">F</text><path d="M371.6,290.7 Q668.0,466.0 337.8,640.1" fill="none" stroke="#222" stroke-width="1.2" marker-end="url(#arr)"/><rect x="505.3" y="456.7" width="13" height="14" fill="#fff" opacity="0.9"/><text x="511.3" y="468.7" text-anchor="middle" font-size="12" font-style="italic" fill="#333">F</text><path d="M260.2,500.0 L301.9,500.0" fill="none" stroke="#222" stroke-width="1.2" marker-end="url(#arr)"/><path d="M350.5,572.8 Q600.0,432.0 370.7,290.9" fill="none" stroke="#222" stroke-width="1.2" marker-end="url(#arr)"/><path d="M485.7,569.3 Q636.0,372.0 385.7,287.4" fill="none" stroke="#222" stroke-width="1.2" marker-end="url(#arr)"/><ellipse cx="340" cy="42" rx="99.5" ry="34" fill="#fff" stroke="#222" stroke-width="1.3"/><text x="340" y="38" text-anchor="middle" font-size="13">size; bott=0; top=size-1;</text><text x="340" y="54" text-anchor="middle" font-size="13">location=0; found=False</text><polygon points="340,94 422.8,120 340,146 257.2,120" fill="#fff" stroke="#222" stroke-width="1.3"/><text x="340" y="124" text-anchor="middle" font-size="13">target[0]==key ?</text><ellipse cx="250" cy="192" rx="49.0" ry="22" fill="#fff" stroke="#222" stroke-width="1.3"/><text x="250" y="196" text-anchor="middle" font-size="13">found=True</text><ellipse cx="440" cy="192" rx="52.3" ry="22" fill="#fff" stroke="#222" stroke-width="1.3"/><text x="440" y="196" text-anchor="middle" font-size="13">found=False</text><polygon points="340,246 452.5,272 340,298 227.5,272" fill="#fff" stroke="#222" stroke-width="1.3"/><text x="340" y="276" text-anchor="middle" font-size="13">bott&lt;=top and not found ?</text><ellipse cx="340" cy="348" rx="115.0" ry="22" fill="#fff" stroke="#222" stroke-width="1.3"/><text x="340" y="352" text-anchor="middle" font-size="13">mid=int(round((top+bott)/2.0))</text><polygon points="340,398 429.4,424 340,450 250.6,424" fill="#fff" stroke="#222" stroke-width="1.3"/><text x="340" y="428" text-anchor="middle" font-size="13">target[mid]==key ?</text><ellipse cx="165" cy="500" rx="95.19999999999999" ry="22" fill="#fff" stroke="#222" stroke-width="1.3"/><text x="165" y="504" text-anchor="middle" font-size="13">found=True; location=mid</text><polygon points="388,474 474.1,500 388,526 301.9,500" fill="#fff" stroke="#222" stroke-width="1.3"/><text x="388" y="504" text-anchor="middle" font-size="13">target[mid]&lt;key ?</text><ellipse cx="320" cy="590" rx="49.0" ry="22" fill="#fff" stroke="#222" stroke-width="1.3"/><text x="320" y="594" text-anchor="middle" font-size="13">bott=mid+1</text><ellipse cx="470" cy="590" rx="45.7" ry="22" fill="#fff" stroke="#222" stroke-width="1.3"/><text x="470" y="594" text-anchor="middle" font-size="13">top=mid-1</text><ellipse cx="300" cy="660" rx="88.6" ry="22" fill="#fff" stroke="#222" stroke-width="1.3"/><text x="300" y="664" text-anchor="middle" font-size="13">return found, location</text></svg>`,
`<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 680 700" font-family="Helvetica,Arial,sans-serif"><rect width="680" height="700" fill="#fff"/><defs><marker id="arr" markerWidth="9" markerHeight="9" refX="7.5" refY="3" orient="auto" markerUnits="userSpaceOnUse"><path d="M0,0 L8,3 L0,6 Z" fill="#222"/></marker></defs><path d="M340.0,76.0 L340.0,94.0" fill="none" stroke="#222" stroke-width="1.2" marker-end="url(#arr)"/><path d="M316.7,138.7 L274.0,172.8" fill="none" stroke="#222" stroke-width="1.2" marker-end="url(#arr)"/><rect x="291.5" y="145.0" width="13" height="14" fill="#fff" opacity="0.9"/><text x="297.5" y="157.0" text-anchor="middle" font-size="12" font-style="italic" fill="#333">T</text><path d="M365.1,138.1 L413.6,173.0" fill="none" stroke="#222" stroke-width="1.2" marker-end="url(#arr)"/><rect x="381.0" y="144.8" width="13" height="14" fill="#fff" opacity="0.9"/><text x="387.0" y="156.8" text-anchor="middle" font-size="12" font-style="italic" fill="#333">F</text><path d="M272.1,211.6 L316.8,251.4" fill="none" stroke="#222" stroke-width="1.2" marker-end="url(#arr)"/><path d="M415.7,211.5 L365.2,251.8" fill="none" stroke="#222" stroke-width="1.2" marker-end="url(#arr)"/><path d="M340.0,298.0 L340.0,326.0" fill="none" stroke="#222" stroke-width="1.2" marker-end="url(#arr)"/><rect x="334.0" y="301.6" width="13" height="14" fill="#fff" opacity="0.9"/><text x="340.0" y="313.6" text-anchor="middle" font-size="12" font-style="italic" fill="#333">T</text><path d="M340.0,370.0 L340.0,398.0" fill="none" stroke="#222" stroke-width="1.2" marker-end="url(#arr)"/><path d="M304.1,439.6 L209.7,480.6" fill="none" stroke="#222" stroke-width="1.2" marker-end="url(#arr)"/><rect x="255.7" y="449.0" width="13" height="14" fill="#fff" opacity="0.9"/><text x="261.7" y="461.0" text-anchor="middle" font-size="12" font-style="italic" fill="#333">T</text><path d="M353.9,446.0 L374.2,478.2" fill="none" stroke="#222" stroke-width="1.2" marker-end="url(#arr)"/><rect x="357.0" y="451.5" width="13" height="14" fill="#fff" opacity="0.9"/><text x="363.0" y="463.5" text-anchor="middle" font-size="12" font-style="italic" fill="#333">F</text><path d="M372.0,521.2 L335.7,569.2" fill="none" stroke="#222" stroke-width="1.2" marker-end="url(#arr)"/><rect x="349.7" y="533.8" width="13" height="14" fill="#fff" opacity="0.9"/><text x="355.7" y="545.8" text-anchor="middle" font-size="12" font-style="italic" fill="#333">T</text><path d="M406.6,520.4 L451.6,569.9" fill="none" stroke="#222" stroke-width="1.2" marker-end="url(#arr)"/><rect x="420.9" y="533.6" width="13" height="14" fill="#fff" opacity="0.9"/><text x="426.9" y="545.6" text-anchor="middle" font-size="12" font-style="italic" fill="#333">F</text><path d="M371.6,290.7 Q668.0,466.0 337.8,640.1" fill="none" stroke="#222" stroke-width="1.2" marker-end="url(#arr)"/><rect x="505.3" y="456.7" width="13" height="14" fill="#fff" opacity="0.9"/><text x="511.3" y="468.7" text-anchor="middle" font-size="12" font-style="italic" fill="#333">F</text><path d="M183.2,521.6 L281.8,638.5" fill="none" stroke="#222" stroke-width="1.2" marker-end="url(#arr)"/><path d="M350.5,572.8 Q600.0,432.0 370.7,290.9" fill="none" stroke="#222" stroke-width="1.2" marker-end="url(#arr)"/><path d="M485.7,569.3 Q636.0,372.0 385.7,287.4" fill="none" stroke="#222" stroke-width="1.2" marker-end="url(#arr)"/><ellipse cx="340" cy="42" rx="99.5" ry="34" fill="#fff" stroke="#222" stroke-width="1.3"/><text x="340" y="38" text-anchor="middle" font-size="13">size; bott=0; top=size-1;</text><text x="340" y="54" text-anchor="middle" font-size="13">location=0; found=False</text><polygon points="340,94 422.8,120 340,146 257.2,120" fill="#fff" stroke="#222" stroke-width="1.3"/><text x="340" y="124" text-anchor="middle" font-size="13">target[0]==key ?</text><ellipse cx="250" cy="192" rx="49.0" ry="22" fill="#fff" stroke="#222" stroke-width="1.3"/><text x="250" y="196" text-anchor="middle" font-size="13">found=True</text><ellipse cx="440" cy="192" rx="52.3" ry="22" fill="#fff" stroke="#222" stroke-width="1.3"/><text x="440" y="196" text-anchor="middle" font-size="13">found=False</text><polygon points="340,246 452.5,272 340,298 227.5,272" fill="#fff" stroke="#222" stroke-width="1.3"/><text x="340" y="276" text-anchor="middle" font-size="13">bott&lt;=top and not found ?</text><ellipse cx="340" cy="348" rx="115.0" ry="22" fill="#fff" stroke="#222" stroke-width="1.3"/><text x="340" y="352" text-anchor="middle" font-size="13">mid=int(round((top+bott)/2.0))</text><polygon points="340,398 429.4,424 340,450 250.6,424" fill="#fff" stroke="#222" stroke-width="1.3"/><text x="340" y="428" text-anchor="middle" font-size="13">target[mid]==key ?</text><ellipse cx="165" cy="500" rx="95.19999999999999" ry="22" fill="#fff" stroke="#222" stroke-width="1.3"/><text x="165" y="504" text-anchor="middle" font-size="13">found=True; location=mid</text><polygon points="388,474 474.1,500 388,526 301.9,500" fill="#fff" stroke="#222" stroke-width="1.3"/><text x="388" y="504" text-anchor="middle" font-size="13">target[mid]&lt;key ?</text><ellipse cx="320" cy="590" rx="49.0" ry="22" fill="#fff" stroke="#222" stroke-width="1.3"/><text x="320" y="594" text-anchor="middle" font-size="13">bott=mid+1</text><ellipse cx="470" cy="590" rx="45.7" ry="22" fill="#fff" stroke="#222" stroke-width="1.3"/><text x="470" y="594" text-anchor="middle" font-size="13">top=mid-1</text><ellipse cx="300" cy="660" rx="88.6" ry="22" fill="#fff" stroke="#222" stroke-width="1.3"/><text x="300" y="664" text-anchor="middle" font-size="13">return found, location</text></svg>`
];
buildChoice(document.getElementById("q2opts"), "q2",
  [1,2,3,4].map(n=>({val:String(n), label:"Option "+n, body:Q2_SVG[n-1]})),
  v => { answers.q2 = v; saveSession(); });

// ============ Q3: fill-in trace with 12 blanks ============
const Q3_TEXT =
`Test 1  loop runs 0 times
  Input:  target = [5, 8, 12],  key =  @1@
  Output: found =  @2@ ,  location =  @3@
  Reason: target[0]  @4@  key, so found is True before the loop;
          'not found' in the while condition is  @5@ , the body is skipped.

Test 2  loop runs 1 time
  Input:  target = [2, 4, 6],  key = 4
  Output: found = True,  location =  @6@
  Reason: target[0]=2 != 4, enter the loop. First mid =  @7@ ,
          target[mid] =  @8@  = key, found=True, location set, loop exits.

Test 3  loop runs 2 times
  Input:  target = [1, 3, 5, 7, 9],  key =  @9@
  Output: found = True,  location =  @10@
  Reason: first mid=2, target[2]=5 < 9, bott becomes  @11@ ;
          second mid =  @12@ , where round(3.5)=4, target[4]=9 = key, found.`;
(function buildQ3(){
  const host = document.getElementById("q3block");
  host.textContent = ""; host.style.whiteSpace = "pre-wrap";
  const parts = Q3_TEXT.split(/@(\d+)@/);
  for (let i=0;i<parts.length;i++){
    if (i % 2 === 0){ host.appendChild(document.createTextNode(parts[i])); }
    else {
      const n = parts[i];
      const wrap = document.createElement("span");
      const circ = document.createElement("span"); circ.className="circ"; circ.textContent = "β“ͺ".replace("β“ͺ","")+"";
      const inp = document.createElement("input");
      inp.className = "blank"; inp.dataset.n = n; inp.title = "blank "+n;
      inp.placeholder = n;
      inp.oninput = () => { answers.q3[n] = inp.value; saveSession(); };
      host.appendChild(inp);
    }
  }
})();

// ============ Q5: code + options ============
document.getElementById("q5code").innerHTML =
`d = a + b            # addition
e = b * c            # multiplication
f = d / (e + 1)      # division and addition

if f &gt; 0:
    g = f + d <span class="swap" data-seen="- "></span>e    # operations in conditional branch
if f &lt;= 0:
    g = d - e / (c + 1)   # different operations without else

result = g * a + b - c    # final operation

return result`;
buildChoice(document.getElementById("q5opts"), "q5",
  [["a","(a) 2729.72"],["b","(b) 2827.21"],["c","(c) 2882.64"],["d","(d) 2938.08"]]
    .map(([v,l])=>({val:v, label:l, body:""})),
  v => { answers.q5 = v; saveSession(); });

// ============ Q4 playground (Pyodide) ============
const q4status = document.getElementById("q4status");
const q4run = document.getElementById("q4run");
const q4out = document.getElementById("q4out");
const q4target = document.getElementById("q4target");
const q4key = document.getElementById("q4key");
let lastQ4 = { original_output:"", variant_output:"" };

const editor = CodeMirror.fromTextArea(document.getElementById("q4code"), {
  mode:"python", theme:"material-darker", lineNumbers:true, indentUnit:4, tabSize:4,
  extraKeys:{ "Cmd-Enter":runQ4, "Ctrl-Enter":runQ4, Tab:cm=>cm.replaceSelection("    ") },
});
editor.setValue(REF_SRC);
document.getElementById("q4reset").onclick = () => editor.setValue(REF_SRC);
editor.on("change", () => saveSession());

function q4append(t, cls){ const s=document.createElement("span"); if(cls)s.className=cls; s.textContent=t; q4out.appendChild(s); }

const HARNESS = `
import json, ast
def __make_orig():
    ns = {}
    exec(__REF, ns)
    return ns["binary_search"]
def __run():
    res = {}
    try:
        orig = __make_orig()
    except Exception as e:
        return json.dumps({"error":"reference error: "+str(e)})
    ns = {}
    try:
        exec(__MUT, ns)
    except Exception as e:
        return json.dumps({"error":"Your code has an error: "+str(e)})
    mut = ns.get("binary_search")
    if mut is None:
        return json.dumps({"error":"Your code must define a function named binary_search."})
    q3 = [([5,8,12],5), ([2,4,6],4), ([1,3,5,7,9],9)]
    rows = []; allpass = True
    for tgt,k in q3:
        try: o = orig(k, list(tgt))
        except Exception as e: o = "ERR: "+str(e)
        try: m = mut(k, list(tgt))
        except Exception as e: m = "ERR: "+str(e)
        same = (str(o)==str(m))
        if not same: allpass = False
        rows.append({"target":str(list(tgt)),"key":k,"orig":str(o),"mut":str(m),"same":same})
    dist = None
    ts = __TGT.strip(); ks = __KEY.strip()
    if ts and ks:
        try:
            tgt = ast.literal_eval(ts); k = ast.literal_eval(ks)
            try: o = orig(k, tgt)
            except Exception as e: o = "ERR: "+str(e)
            try: m = mut(k, tgt)
            except Exception as e: m = "ERR: "+str(e)
            dist = {"target":str(tgt),"key":str(k),"orig":str(o),"mut":str(m),"differs":(str(o)!=str(m))}
        except Exception as e:
            dist = {"error":"could not parse target/key: "+str(e)}
    return json.dumps({"rows":rows,"allpass":allpass,"dist":dist})
__run()
`;

let pyodide = null;
async function runQ4(){
  if (!pyodide || q4run.disabled) return;
  q4run.disabled = true; q4out.innerHTML = "";
  try {
    pyodide.globals.set("__REF", REF_SRC);
    pyodide.globals.set("__MUT", editor.getValue());
    pyodide.globals.set("__TGT", q4target.value || "");
    pyodide.globals.set("__KEY", q4key.value || "");
    const rep = JSON.parse(pyodide.runPython(HARNESS));
    if (rep.error){ q4append(rep.error+"\n","err"); lastQ4={original_output:"",variant_output:""}; return; }
    q4append("Q3 suite on your variant (must all pass / match):\n","meta");
    for (const r of rep.rows){
      const tag = r.same ? "  PASS " : "  FAIL ";
      q4append(tag, r.same?"ok":"err");
      q4append(`target=${r.target}, key=${r.key}  original=${r.orig}  variant=${r.mut}\n`, "meta");
    }
    if (rep.allpass) q4append("(a) βœ“ Variant is invisible to the Q3 suite β€” all three match.\n","ok");
    else q4append("(a) βœ— Variant changes a Q3 result β€” the Q3 suite would catch it. Pick a bug on an unexercised path.\n","warn");
    q4append("\n");
    if (!rep.dist){ q4append("(b) Fill target + key above to test your distinguishing case.\n","meta"); lastQ4={original_output:"",variant_output:""}; }
    else if (rep.dist.error){ q4append("(b) "+rep.dist.error+"\n","err"); lastQ4={original_output:"",variant_output:""}; }
    else {
      lastQ4 = { original_output: rep.dist.orig, variant_output: rep.dist.mut };
      q4append(`(b) Distinguishing test  target=${rep.dist.target}, key=${rep.dist.key}\n`,"meta");
      q4append(`    original = ${rep.dist.orig}\n    variant   = ${rep.dist.mut}\n`,"meta");
      if (rep.dist.differs) q4append("    βœ“ This test distinguishes the variant from the original.\n","ok");
      else q4append("    βœ— Same output on both β€” this test does NOT distinguish them yet.\n","warn");
    }
  } catch(e){ q4append("Runtime error: "+e+"\n","err"); }
  finally { q4run.disabled = false; saveSession(); }
}
q4run.onclick = runQ4;
(async () => {
  try {
    pyodide = await loadPyodide({});
    q4status.textContent = "βœ“ Ready"; q4status.className = "status ready"; q4run.disabled = false;
  } catch(e){ q4status.textContent = "Failed to load"; q4status.className = "status error"; }
})();

// ============ JSON export (fixed format) ============
function buildJSON(){
  const q3 = {};
  for (let i=1;i<=12;i++) q3[i] = (answers.q3[i] || "");
  return {
    assignment: "Software Testing β€” Assignment 2",
    student_id: document.getElementById("studentId").value.trim(),
    answers: {
      q1: answers.q1 || "",
      q2: answers.q2 || "",
      q3: q3,
      q4: {
        change: document.getElementById("q4change").value.trim(),
        variant_code: editor.getValue(),
        test: { target: q4target.value.trim(), key: q4key.value.trim() },
        original_output: document.getElementById("q4origout").value.trim() || lastQ4.original_output,
        variant_output: document.getElementById("q4varout").value.trim() || lastQ4.variant_output
      },
      q5: answers.q5 || ""
    }
  };
}
function toast(msg){ const t=document.getElementById("toast"); t.textContent=msg; setTimeout(()=>t.textContent="",2500); }
document.getElementById("exportBtn").onclick = () => {
  const data = JSON.stringify(buildJSON(), null, 2);
  const blob = new Blob([data], {type:"application/json"});
  const a = document.createElement("a");
  const sid = document.getElementById("studentId").value.trim() || "submission";
  a.href = URL.createObjectURL(blob); a.download = `assignment2_${sid}.json`;
  a.click(); URL.revokeObjectURL(a.href); toast("Exported βœ“");
};
document.getElementById("copyBtn").onclick = async () => {
  try { await navigator.clipboard.writeText(JSON.stringify(buildJSON(), null, 2)); toast("Copied βœ“"); }
  catch(_){ toast("Copy failed"); }
};

// ============ 24h local session (auto-save / restore) ============
const SESSION_KEY = "st_a2_session_v1";   // persists in this browser until "Clear saved"
let __saveTimer = null;
function saveSession(){
  // debounce rapid edits (typing in the editor / blanks)
  clearTimeout(__saveTimer);
  __saveTimer = setTimeout(() => {
    try {
      const d = {
        ts: Date.now(),
        studentId: document.getElementById("studentId").value,
        q1: answers.q1, q2: answers.q2, q5: answers.q5,
        q3: answers.q3,
        q4: {
          change: document.getElementById("q4change").value,
          code: editor.getValue(),
          target: q4target.value, key: q4key.value,
          origout: document.getElementById("q4origout").value,
          varout: document.getElementById("q4varout").value,
          original_output: lastQ4.original_output, variant_output: lastQ4.variant_output
        }
      };
      localStorage.setItem(SESSION_KEY, JSON.stringify(d));
    } catch(_){}
  }, 250);
}
function restoreRadio(name, val){
  if (!val) return;
  const r = document.querySelector(`input[name="${name}"][value="${val}"]`);
  if (r){ r.checked = true; const opt = r.closest(".opt"); if (opt) opt.classList.add("sel"); answers[name] = val; }
}
function loadSession(){
  let d;
  try { d = JSON.parse(localStorage.getItem(SESSION_KEY)); } catch(_){ return; }
  if (!d) return;
  document.getElementById("studentId").value = d.studentId || "";
  restoreRadio("q1", d.q1); restoreRadio("q2", d.q2); restoreRadio("q5", d.q5);
  if (d.q3){ Object.assign(answers.q3, d.q3);
    // Mutate the EXISTING answers.q3 object in place rather than reassigning it --
    // the blank inputs' oninput handlers closed over the original object by
    // reference, so reassigning here would silently orphan that reference and
    // freeze every future keystroke out of answers.q3 (and thus buildJSON()).
    document.querySelectorAll("input.blank").forEach(inp => { const v = d.q3[inp.dataset.n]; if (v != null) inp.value = v; }); }
  if (d.q4){
    document.getElementById("q4change").value = d.q4.change || "";
    if (d.q4.code) editor.setValue(d.q4.code);
    q4target.value = d.q4.target || ""; q4key.value = d.q4.key || "";
    document.getElementById("q4origout").value = d.q4.origout || "";
    document.getElementById("q4varout").value = d.q4.varout || "";
    lastQ4 = { original_output: d.q4.original_output || "", variant_output: d.q4.variant_output || "" };
  }
  // banner
  let ago = "earlier";
  if (d.ts){ const mins = Math.round((Date.now() - d.ts) / 60000);
    ago = mins < 1 ? "just now" : mins < 60 ? mins + " min ago" : mins < 1440 ? Math.round(mins/60) + " h ago" : Math.round(mins/1440) + " d ago"; }
  const b = document.createElement("div");
  b.style.cssText = "margin:10px 0;padding:8px 12px;border:1px solid var(--border);border-radius:8px;background:#262a3f;font-size:12px;color:var(--muted)";
  b.innerHTML = `β†Ί Restored your answers from this browser (saved ${ago}). They stay saved in this browser until you press Clear. `;
  const x = document.createElement("button"); x.textContent = "Clear & start fresh"; x.className = "ghost";
  x.style.cssText = "padding:3px 10px;font-size:11px;margin-left:6px";
  x.onclick = clearSession;
  b.appendChild(x);
  const wrap = document.querySelector(".wrap");
  wrap.insertBefore(b, wrap.children[2]);
}
function clearSession(){ localStorage.removeItem(SESSION_KEY); location.reload(); }
// save on the free-text fields too
["studentId","q4change","q4origout","q4varout"].forEach(id => document.getElementById(id).addEventListener("input", saveSession));
[q4target, q4key].forEach(el => el.addEventListener("input", saveSession));
document.getElementById("clearBtn").onclick = clearSession;
// restore once everything above is defined
loadSession();

// ============ tabs (one per question) ============
(function initTabs(){
  const TAB_KEY = "st_a2_tab";
  const btns  = [...document.querySelectorAll("#tabbar button")];
  const panes = [...document.querySelectorAll(".pane")];
  function show(id){
    if (!btns.some(b => b.dataset.tab === id)) id = "q1";
    btns.forEach(b => b.classList.toggle("active", b.dataset.tab === id));
    panes.forEach(p => p.classList.toggle("active", p.dataset.tab === id));
    try { localStorage.setItem(TAB_KEY, id); } catch(_){}
    // CodeMirror needs a refresh when it becomes visible
    if (id === "q4" && typeof editor !== "undefined" && editor) setTimeout(() => editor.refresh(), 0);
    window.scrollTo(0, 0);
  }
  btns.forEach(b => b.onclick = () => show(b.dataset.tab));
  show(localStorage.getItem(TAB_KEY) || "q1");
})();
</script>
</body>
</html>