Spaces:
Running
Running
File size: 15,562 Bytes
65e0396 | 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 | // Python Code Tester Application
class PythonCodeTester {
constructor() {
this.tasks = [
{
id: 1,
title: "Створення змінних",
description: "Створіть змінні для збереження вашого імені (рядок), віку (ціле число) та зросту (дійсне число). Виведіть їх на екран.",
hint: "Використовуйте змінні name, age, height та функцію print()",
solution: "name = \"Іван\"\nage = 20\nheight = 1.75\nprint(\"Ім'я:\", name)\nprint(\"Вік:\", age)\nprint(\"Зріст:\", height)",
expected_output: "Ім'я: Іван\nВік: 20\nЗріст: 1.75"
},
{
id: 2,
title: "Арифметичні операції",
description: "Обчисліть площу прямокутника зі сторонами 15 та 25. Виведіть результат.",
hint: "Площа = довжина × ширина",
solution: "length = 15\nwidth = 25\narea = length * width\nprint(\"Площа прямокутника:\", area)",
expected_output: "Площа прямокутника: 375"
},
{
id: 3,
title: "Перетворення типів",
description: "Перетворіть рядок '42' у ціле число, додайте до нього 8 та виведіть результат.",
hint: "Використовуйте функцію int() для перетворення",
solution: "number_str = '42'\nnumber = int(number_str)\nresult = number + 8\nprint(\"Результат:\", result)",
expected_output: "Результат: 50"
},
{
id: 4,
title: "Робота з булевими значеннями",
description: "Створіть дві булеві змінні: is_student=True та has_id=False. Виведіть результат логічного AND та OR для цих змінних.",
hint: "Використовуйте оператори and та or",
solution: "is_student = True\nhas_id = False\nprint(\"AND результат:\", is_student and has_id)\nprint(\"OR результат:\", is_student or has_id)",
expected_output: "AND результат: False\nOR результат: True"
},
{
id: 5,
title: "Простий калькулятор",
description: "Створіть програму, яка додає два числа 15.5 та 24.3 та виводить результат з описом.",
hint: "Використовуйте оператор + та форматування виводу",
solution: "num1 = 15.5\nnum2 = 24.3\nsum_result = num1 + num2\nprint(f\"Сума {num1} + {num2} = {sum_result}\")",
expected_output: "Сума 15.5 + 24.3 = 39.8"
}
];
this.currentTask = null;
this.initializeApp();
}
initializeApp() {
this.renderTasks();
this.setupEventListeners();
}
renderTasks() {
const taskList = document.getElementById('taskList');
taskList.innerHTML = '';
this.tasks.forEach((task, index) => {
const taskItem = document.createElement('div');
taskItem.className = 'task-item';
taskItem.dataset.taskId = task.id;
taskItem.innerHTML = `
<h4>${index + 1}. ${task.title}</h4>
`;
taskItem.addEventListener('click', () => this.selectTask(task));
taskList.appendChild(taskItem);
});
}
selectTask(task) {
this.currentTask = task;
// Update active task styling
document.querySelectorAll('.task-item').forEach(item => {
item.classList.remove('active');
});
document.querySelector(`[data-task-id="${task.id}"]`).classList.add('active');
// Update task display
document.getElementById('currentTaskTitle').textContent = task.title;
document.getElementById('taskDescription').textContent = task.description;
// Show task action buttons
document.getElementById('hintBtn').style.display = 'inline-flex';
document.getElementById('solutionBtn').style.display = 'inline-flex';
// Clear previous output
document.getElementById('output').textContent = 'Натисніть "Запустити" для виконання коду...';
document.getElementById('outputStatus').textContent = '';
}
setupEventListeners() {
// Run button
document.getElementById('runBtn').addEventListener('click', () => this.runCode());
// Hint button
document.getElementById('hintBtn').addEventListener('click', () => this.showHint());
// Solution button
document.getElementById('solutionBtn').addEventListener('click', () => this.showSolution());
// Modal close buttons
document.getElementById('closeHintModal').addEventListener('click', () => this.closeModal('hintModal'));
document.getElementById('closeSolutionModal').addEventListener('click', () => this.closeModal('solutionModal'));
// Close modal on backdrop click
document.getElementById('hintModal').addEventListener('click', (e) => {
if (e.target.id === 'hintModal') this.closeModal('hintModal');
});
document.getElementById('solutionModal').addEventListener('click', (e) => {
if (e.target.id === 'solutionModal') this.closeModal('solutionModal');
});
// Code editor enhancements
const codeEditor = document.getElementById('codeEditor');
codeEditor.addEventListener('keydown', (e) => this.handleEditorKeydown(e));
// Example code click handlers
document.querySelectorAll('.example-section code').forEach(code => {
code.addEventListener('click', () => {
const codeText = code.textContent;
const editor = document.getElementById('codeEditor');
const currentCode = editor.value;
if (currentCode.trim() === '' || currentCode === codeEditor.placeholder) {
editor.value = codeText;
} else {
editor.value = currentCode + '\n' + codeText;
}
editor.focus();
});
});
}
handleEditorKeydown(e) {
// Handle Tab key for indentation
if (e.key === 'Tab') {
e.preventDefault();
const editor = e.target;
const start = editor.selectionStart;
const end = editor.selectionEnd;
editor.value = editor.value.substring(0, start) + ' ' + editor.value.substring(end);
editor.selectionStart = editor.selectionEnd = start + 4;
}
}
runCode() {
const code = document.getElementById('codeEditor').value.trim();
const output = document.getElementById('output');
const outputStatus = document.getElementById('outputStatus');
if (!code) {
output.textContent = 'Помилка: Введіть код для виконання';
outputStatus.innerHTML = '<span class="status-error">Помилка</span>';
return;
}
try {
const result = this.simulatePythonExecution(code);
output.textContent = result.output;
if (this.currentTask) {
const isCorrect = this.checkSolution(result.output, this.currentTask.expected_output);
if (isCorrect) {
outputStatus.innerHTML = '<span class="status-success">✓ Правильно!</span>';
} else {
outputStatus.innerHTML = '<span class="status-error">Не зовсім правильно</span>';
}
} else {
outputStatus.innerHTML = '<span class="status-success">Виконано</span>';
}
} catch (error) {
output.textContent = `Помилка: ${error.message}`;
outputStatus.innerHTML = '<span class="status-error">Помилка виконання</span>';
}
}
simulatePythonExecution(code) {
// Simple Python code simulator
let output = '';
let variables = {};
const lines = code.split('\n').filter(line => line.trim() && !line.trim().startsWith('#'));
for (let line of lines) {
line = line.trim();
try {
if (line.includes('print(')) {
const result = this.executePrintStatement(line, variables);
output += result + '\n';
} else if (line.includes('=') && !line.includes('==')) {
this.executeAssignment(line, variables);
}
} catch (error) {
throw new Error(`Помилка в рядку "${line}": ${error.message}`);
}
}
return { output: output.trim() };
}
executeAssignment(line, variables) {
const parts = line.split('=');
if (parts.length !== 2) return;
const varName = parts[0].trim();
const value = parts[1].trim();
variables[varName] = this.evaluateExpression(value, variables);
}
executePrintStatement(line, variables) {
const match = line.match(/print\((.*)\)/);
if (!match) return '';
const args = this.parseArguments(match[1]);
const values = args.map(arg => this.evaluateExpression(arg, variables));
return values.join(' ');
}
parseArguments(argsString) {
const args = [];
let current = '';
let inString = false;
let stringChar = '';
let depth = 0;
for (let i = 0; i < argsString.length; i++) {
const char = argsString[i];
if ((char === '"' || char === "'") && !inString) {
inString = true;
stringChar = char;
current += char;
} else if (char === stringChar && inString) {
inString = false;
current += char;
} else if (char === '(' && !inString) {
depth++;
current += char;
} else if (char === ')' && !inString) {
depth--;
current += char;
} else if (char === ',' && !inString && depth === 0) {
args.push(current.trim());
current = '';
} else {
current += char;
}
}
if (current.trim()) {
args.push(current.trim());
}
return args;
}
evaluateExpression(expr, variables) {
expr = expr.trim();
// String literals
if ((expr.startsWith('"') && expr.endsWith('"')) ||
(expr.startsWith("'") && expr.endsWith("'"))) {
return expr.slice(1, -1);
}
// f-strings (basic support)
if (expr.startsWith('f"') || expr.startsWith("f'")) {
let result = expr.slice(2, -1);
Object.keys(variables).forEach(varName => {
const regex = new RegExp(`{${varName}}`, 'g');
result = result.replace(regex, variables[varName]);
});
return result;
}
// Numbers
if (!isNaN(expr)) {
return expr.includes('.') ? parseFloat(expr) : parseInt(expr);
}
// Boolean values
if (expr === 'True') return true;
if (expr === 'False') return false;
// Variables
if (variables.hasOwnProperty(expr)) {
return variables[expr];
}
// Simple arithmetic expressions
if (expr.includes('+') || expr.includes('-') || expr.includes('*') || expr.includes('/')) {
return this.evaluateArithmetic(expr, variables);
}
// Boolean expressions
if (expr.includes(' and ') || expr.includes(' or ')) {
return this.evaluateBoolean(expr, variables);
}
// Type casting functions
if (expr.startsWith('int(')) {
const inner = expr.slice(4, -1);
const value = this.evaluateExpression(inner, variables);
return parseInt(value);
}
if (expr.startsWith('str(')) {
const inner = expr.slice(4, -1);
const value = this.evaluateExpression(inner, variables);
return String(value);
}
if (expr.startsWith('float(')) {
const inner = expr.slice(6, -1);
const value = this.evaluateExpression(inner, variables);
return parseFloat(value);
}
return expr;
}
evaluateArithmetic(expr, variables) {
// Simple arithmetic evaluation
let result = expr;
// Replace variables with their values
Object.keys(variables).forEach(varName => {
const regex = new RegExp(`\\b${varName}\\b`, 'g');
result = result.replace(regex, variables[varName]);
});
try {
return eval(result);
} catch {
return expr;
}
}
evaluateBoolean(expr, variables) {
let result = expr;
// Replace variables with their values
Object.keys(variables).forEach(varName => {
const regex = new RegExp(`\\b${varName}\\b`, 'g');
result = result.replace(regex, variables[varName]);
});
// Replace Python boolean operators with JavaScript ones
result = result.replace(/ and /g, ' && ');
result = result.replace(/ or /g, ' || ');
result = result.replace(/True/g, 'true');
result = result.replace(/False/g, 'false');
try {
return eval(result);
} catch {
return expr;
}
}
checkSolution(actualOutput, expectedOutput) {
const normalize = (str) => str.replace(/\s+/g, ' ').trim().toLowerCase();
return normalize(actualOutput) === normalize(expectedOutput);
}
showHint() {
if (!this.currentTask) return;
document.getElementById('hintText').textContent = this.currentTask.hint;
document.getElementById('hintModal').classList.remove('hidden');
}
showSolution() {
if (!this.currentTask) return;
document.getElementById('solutionCode').textContent = this.currentTask.solution;
document.getElementById('solutionModal').classList.remove('hidden');
}
closeModal(modalId) {
document.getElementById(modalId).classList.add('hidden');
}
}
// Initialize the application when the page loads
document.addEventListener('DOMContentLoaded', () => {
new PythonCodeTester();
}); |