File size: 10,311 Bytes
73f2a57
 
 
c3ae09d
 
73f2a57
 
c3ae09d
 
 
 
 
 
 
 
 
 
 
 
 
 
73f2a57
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6f3693c
c3ae09d
73f2a57
 
 
 
c3ae09d
73f2a57
c3ae09d
 
 
73f2a57
c3ae09d
73f2a57
 
c3ae09d
 
 
73f2a57
 
c3ae09d
 
73f2a57
 
 
 
 
 
c3ae09d
 
73f2a57
 
 
c3ae09d
 
73f2a57
 
 
c3ae09d
 
 
73f2a57
 
 
 
 
c3ae09d
73f2a57
c3ae09d
 
 
73f2a57
 
 
 
c3ae09d
 
73f2a57
 
 
c3ae09d
 
 
73f2a57
 
 
 
 
c3ae09d
73f2a57
c3ae09d
 
 
73f2a57
c3ae09d
73f2a57
 
c3ae09d
 
73f2a57
 
 
c3ae09d
 
 
73f2a57
 
 
 
 
c3ae09d
73f2a57
c3ae09d
 
 
73f2a57
c3ae09d
73f2a57
 
c3ae09d
 
 
73f2a57
 
c3ae09d
73f2a57
c3ae09d
 
 
73f2a57
c3ae09d
73f2a57
 
 
 
 
6f3693c
73f2a57
 
 
 
 
c3ae09d
6f3693c
c3ae09d
6f3693c
 
 
 
 
 
 
c3ae09d
 
6f3693c
c3ae09d
 
6f3693c
 
73f2a57
 
 
 
 
c3ae09d
73f2a57
 
 
 
 
 
c3ae09d
6f3693c
 
 
 
c3ae09d
 
6f3693c
c3ae09d
 
 
6f3693c
c3ae09d
6f3693c
 
c3ae09d
 
 
 
 
 
 
 
6f3693c
 
c3ae09d
 
 
 
6f3693c
 
 
 
73f2a57
 
6f3693c
 
c3ae09d
6f3693c
 
c3ae09d
 
73f2a57
 
6f3693c
73f2a57
 
c3ae09d
73f2a57
6f3693c
73f2a57
 
 
 
 
6f3693c
 
 
 
73f2a57
6f3693c
c3ae09d
73f2a57
 
 
6f3693c
73f2a57
 
c3ae09d
73f2a57
 
c3ae09d
 
73f2a57
6f3693c
73f2a57
6f3693c
73f2a57
 
 
 
 
 
6f3693c
73f2a57
 
 
0b4e513
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
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
  <title>Week 1 Python Questions</title>
  <style>
    body{font-family:Arial,sans-serif;display:flex;flex-direction:column;align-items:center;background:#f0f0f0;margin:0}
    h1{color:#0078D4}
    #game-board{display:grid;grid-template-columns:repeat(5,1fr);grid-auto-rows:100px;gap:2px;margin:20px 0;background:#000;border:4px solid #000;width:90%}
    .category{background:#005a9e;color:#fff;font-weight:bold;display:flex;justify-content:center;align-items:center;text-align:center;font-size:18px;border:1px solid #000}
    .card{background:#0078D4;color:#fff;display:flex;justify-content:center;align-items:center;font-size:16px;font-weight:bold;cursor:pointer;border:1px solid #000;text-align:center;transition:background-color .3s}
    .card:hover{background:#005a9e}
    .card.disabled{background:#ccc;cursor:default}
    #question-display{width:80%;text-align:center;margin-bottom:20px}
    #score{font-size:24px;font-weight:bold;color:#0078D4}
    .answer-container{display:flex;flex-wrap:wrap;justify-content:center;margin-top:20px}
    .answer-btn{margin:5px;padding:10px 15px;font-size:18px;cursor:pointer;text-align:center;border:2px solid #005a9e;border-radius:5px;background:#0078D4;color:#fff;font-weight:bold;transition:background-color .3s,color .3s}
    .answer-btn:hover{background:#005a9e;color:#f0f0f0}
    .answer-btn.disabled{background:#ccc;color:#666;cursor:not-allowed}
    .feedback{margin-top:10px;font-size:18px;font-weight:bold}
  </style>
</head>
<body>
  <h1>Week 1 Python Questions</h1>
  <div id="game-board">
    <div class="category">Variables & Data Structures</div>
    <div class="category">Operators</div>
    <div class="category">Conditional Statements</div>
    <div class="category">Looping Statements</div>
    <div class="category">Functions</div>
  </div>
  <div id="question-display"></div>
  <div id="score">Score: 0</div>

  <script>
    const categories = [
      "Variables & Data Structures",
      "Operators",
      "Conditional Statements",
      "Looping Statements",
      "Functions"
    ];

    // ✅ Cleaned questions: Python-correct code, unique correct answers, 3 options each
    const questions = [
      // Variables & Data Structures
      [
        {
          q: "Which statement best describes Python variables?",
          a: [
            "They are names bound to objects; names can be rebound",
            "They must be declared with a type keyword before use",
            "They can only reference numbers and strings"
          ],
          correct: 0
        },
        {
          q: "Which Python data structures are mutable?",
          a: ["List and Dictionary", "Tuple and String", "Set and Tuple"],
          correct: 0
        },
        {
          q: "Which slice returns ['four', 5] from my_list = [1, 'two', 3.5, 'four', 5, 6.0, 'seven', 8.2]?",
          a: ["my_list[3:5]", "my_list[2:4]", "my_list[-4:-2]"],
          correct: 0
        }
      ],
      // Operators
      [
        {
          q: "Which pairing correctly matches Python operators with operations?",
          a: ["+ for addition, - for subtraction", "** for modulus, % for exponentiation", "% for division, / for multiplication"],
          correct: 0
        },
        {
          q: "What is the result of 2 + 3 * 4 in Python?",
          a: ["14", "20", "24"],
          correct: 0
        },
        {
          q: "Which operator performs exponentiation in Python?",
          a: ["**", "^", "exp()"],
          correct: 0
        }
      ],
      // Conditional Statements
      [
        {
          q: "What does 'elif' do in Python?",
          a: [
            "Tests another condition if previous tests were False",
            "Runs when the preceding 'if' was True",
            "Is mandatory after every 'if'"
          ],
          correct: 0
        },
        {
          q: "Which keyword begins the fallback block when no prior condition was True?",
          a: ["else", "otherwise", "default"],
          correct: 0
        },
        {
          q: "Given: if x > 10: print('Greater')\nelif x == 10: print('Equal')\nelse: print('Less')\nWhat prints when x == 10?",
          a: ["Equal", "Greater", "Less"],
          correct: 0
        }
      ],
      // Looping Statements
      [
        {
          q: "Which is true of Python's 'for' loop?",
          a: [
            "It iterates over items of an iterable",
            "It repeats while a condition is True",
            "It requires a manual counter only"
          ],
          correct: 0
        },
        {
          q: "What does this print?\nfor i in range(1, 4):\n    print(i, end=' ')",
          a: ["1 2 3 ", "0 1 2 ", "1 2 3 4 "],
          correct: 0
        },
        {
          q: "Best loop for iterating a list when the number of passes is known?",
          a: ["for loop", "while loop", "do-while loop"],
          correct: 0
        }
      ],
      // Functions
      [
        {
          q: "Primary purpose of functions in Python?",
          a: [
            "Encapsulate reusable logic into named blocks",
            "Declare compile-time constants",
            "Only execute once per program"
          ],
          correct: 0
        },
        {
          q: "How do you define a function?",
          a: ["def my_func(params):", "function my_func(params):", "define my_func(params):"],
          correct: 0
        },
        {
          q: "Which statement about 'return' is false?",
          a: [
            "Every function must include a 'return' statement",
            "A function can return multiple values",
            "A function may have multiple 'return' paths"
          ],
          correct: 0
        }
      ]
    ];

    let score = 0;
    const totalCards = 15;
    let answeredCards = 0;
    const gameBoard = document.getElementById("game-board");
    const questionDisplay = document.getElementById("question-display");
    const scoreDisplay = document.getElementById("score");

    // Even distribution of correct positions (0..2) across 15 questions
    const correctPositions = (() => {
      const arr = Array.from({ length: totalCards }, (_, i) => i % 3); // five of each
      for (let i = arr.length - 1; i > 0; i--) {
        const j = Math.floor(Math.random() * (i + 1));
        [arr[i], arr[j]] = [arr[j], arr[i]];
      }
      return arr;
    })();

    // Memo for per-question permuted choices
    const questionMeta = new Map(); // "c-d" -> { choices:[...3], correctIndex }

    function qIndex(category, difficulty) {
      return category * 3 + difficulty; // 0..14
    }

    function createBoard() {
      for (let row = 0; row < 3; row++) {
        for (let col = 0; col < 5; col++) {
          const card = document.createElement("div");
          card.className = "card";
          card.textContent = "$" + (row + 1) * 100;
          card.onclick = () => showQuestion(col, row, card);
          gameBoard.appendChild(card);
        }
      }
    }

    function prepareChoices(category, difficulty) {
      const key = `${category}-${difficulty}`;
      if (questionMeta.has(key)) return questionMeta.get(key);

      const q = questions[category][difficulty];
      const baseChoices = q.a.slice(); // already length 3
      const origCorrect = q.correct;   // index in baseChoices

      // Start with a random permutation
      const idxs = [0, 1, 2];
      for (let i = idxs.length - 1; i > 0; i--) {
        const j = Math.floor(Math.random() * (i + 1));
        [idxs[i], idxs[j]] = [idxs[j], idxs[i]];
      }

      // Ensure the correct answer ends up at the prescribed position
      const targetPos = correctPositions[qIndex(category, difficulty)];
      const currentPosOfCorrect = idxs.indexOf(origCorrect);
      if (currentPosOfCorrect !== targetPos) {
        // swap positions
        const swapIdx = idxs[targetPos];
        idxs[targetPos] = origCorrect;
        idxs[currentPosOfCorrect] = swapIdx;
      }

      const choices = idxs.map(i => baseChoices[i]);
      const correctIndex = targetPos;

      const meta = { choices, correctIndex };
      questionMeta.set(key, meta);
      return meta;
    }

    function showQuestion(category, difficulty, cardElement) {
      if (cardElement.classList.contains("disabled")) return;

      const q = questions[category][difficulty];
      const { choices } = prepareChoices(category, difficulty);

      const answerHtml = choices
        .map((answer, idx) =>
          `<button class="answer-btn" onclick="checkAnswer(${category}, ${difficulty}, ${idx})">${answer}</button>`
        )
        .join("");

      questionDisplay.innerHTML = `
        <h2>${categories[category]} for $${(difficulty + 1) * 100}</h2>
        <pre style="white-space:pre-wrap;text-align:left;background:#fff;border:1px solid #ddd;padding:10px;border-radius:6px">${q.q}</pre>
        <div class="answer-container">${answerHtml}</div>`;

      cardElement.classList.add("disabled");
      cardElement.style.backgroundColor = "#cccccc";
      answeredCards++;
    }

    function checkAnswer(category, difficulty, selectedIndex) {
      const key = `${category}-${difficulty}`;
      const meta = questionMeta.get(key);
      const isCorrect = selectedIndex === meta.correctIndex;
      const value = (difficulty + 1) * 100;

      document.querySelectorAll(".answer-btn").forEach(btn => {
        btn.disabled = true;
        btn.classList.add("disabled");
      });

      if (isCorrect) {
        score += value;
        questionDisplay.innerHTML += `<p class="feedback" style="color:green">Correct! You earned $${value}.</p>`;
      } else {
        score -= value;
        const correctText = meta.choices[meta.correctIndex];
        questionDisplay.innerHTML += `<p class="feedback" style="color:red">Wrong! You lost $${value}. Correct answer: ${correctText}</p>`;
      }

      scoreDisplay.textContent = "Score: " + score;
      if (answeredCards === totalCards) endGame();
    }

    function endGame() {
      questionDisplay.innerHTML = `<h2>Game Over!</h2><p>Your final score is $${score}.</p>`;
    }

    window.checkAnswer = checkAnswer;
    createBoard();
  </script>
</body>
</html>