zajebis
This commit is contained in:
1
src/__init__.py
Normal file
1
src/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
# This file marks the directory as a Python package.
|
||||
131
src/app.py
Normal file
131
src/app.py
Normal file
@@ -0,0 +1,131 @@
|
||||
from markupsafe import Markup
|
||||
from flask import Flask, render_template, request, redirect, url_for, send_from_directory
|
||||
import markdown as md
|
||||
import ast
|
||||
from src.models import db, Problem, Solution
|
||||
from src.utils import run_code_against_tests
|
||||
from src.leaderboard import create_leaderboard_table, log_leaderboard, get_leaderboard
|
||||
|
||||
|
||||
import os
|
||||
## from problem_loader import load_problems_from_json, schedule_problem_reload
|
||||
from src.problem_scanner import start_problem_scanner
|
||||
import sqlite3
|
||||
from pathlib import Path
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
BASE_DIR = Path(__file__).parent
|
||||
app.config['SQLALCHEMY_DATABASE_URI'] = f"sqlite:///{BASE_DIR / 'database' / 'db.sqlite3'}"
|
||||
|
||||
print(f">>>>>>>>>>>>>>>>>>>>< Using database URI: {app.config['SQLALCHEMY_DATABASE_URI']}")
|
||||
|
||||
db.init_app(app)
|
||||
|
||||
@app.before_request
|
||||
def setup():
|
||||
db.create_all()
|
||||
create_leaderboard_table() # Ensure leaderboard table exists
|
||||
# Problems are now loaded from manifests by the background scanner. No need to load problems.json.
|
||||
|
||||
# Start the background thread to scan problems
|
||||
start_problem_scanner()
|
||||
|
||||
@app.route("/script.js")
|
||||
def script():
|
||||
return send_from_directory("templates", "script.js")
|
||||
|
||||
@app.route('/favicon.ico')
|
||||
def favicon():
|
||||
return send_from_directory("templates", "favicon", "favicon.ico")
|
||||
|
||||
@app.route('/')
|
||||
def index():
|
||||
db_path = Path(__file__).parent / 'database/problems.sqlite3'
|
||||
conn = sqlite3.connect(db_path)
|
||||
c = conn.cursor()
|
||||
#<!-- The query was fucked up so it fetched the fucking testcode -->
|
||||
c.execute('SELECT folder, description, test_code, difficulty FROM problems')
|
||||
problems = c.fetchall()
|
||||
conn.close()
|
||||
# Get leaderboard entries
|
||||
leaderboard = get_leaderboard()
|
||||
# Map folder to title for display
|
||||
problem_titles = {folder: folder.replace('_', ' ').title() for folder, _, _, _ in problems}
|
||||
return render_template('index.html', problems=problems, leaderboard=leaderboard, problem_titles=problem_titles)
|
||||
|
||||
@app.route('/problem/new', methods=['GET', 'POST'])
|
||||
def new_problem():
|
||||
if request.method == 'POST':
|
||||
title = request.form['title']
|
||||
description = request.form['description']
|
||||
test_code = request.form['test_code']
|
||||
problem = Problem(title=title, description=description, test_code=test_code)
|
||||
db.session.add(problem)
|
||||
db.session.commit()
|
||||
return redirect(url_for('index'))
|
||||
return render_template('new_problem.html')
|
||||
|
||||
@app.route('/problem/<folder>', methods=['GET', 'POST'])
|
||||
def view_problem(folder):
|
||||
db_path = Path(__file__).parent / 'database/problems.sqlite3'
|
||||
conn = sqlite3.connect(db_path)
|
||||
c = conn.cursor()
|
||||
c.execute('SELECT folder, description,test_code , difficulty FROM problems WHERE folder = ?', (folder,))
|
||||
row = c.fetchone()
|
||||
conn.close()
|
||||
|
||||
if not row:
|
||||
return 'Problem not found', 404
|
||||
|
||||
problem = {
|
||||
'folder': row[0],
|
||||
'description': row[1],
|
||||
'difficulty': row[3], # now correct
|
||||
'test_code': row[2], # now correct
|
||||
}
|
||||
|
||||
|
||||
result = None
|
||||
if request.method == 'POST':
|
||||
user_code = request.form['user_code']
|
||||
username = request.form.get('username', '').strip() or 'Anonymous'
|
||||
import tracemalloc
|
||||
tracemalloc.start()
|
||||
run_result = run_code_against_tests(user_code, problem['test_code'])
|
||||
current, peak = tracemalloc.get_traced_memory()
|
||||
tracemalloc.stop()
|
||||
memory_used = peak // 1024 # in KB
|
||||
# Try to get the last line number executed (even for successful runs)
|
||||
line_number = None
|
||||
try:
|
||||
tree = ast.parse(user_code)
|
||||
# Find the highest line number in the AST (for multi-function/user code)
|
||||
def get_max_lineno(node):
|
||||
max_lineno = getattr(node, 'lineno', 0)
|
||||
for child in ast.iter_child_nodes(node):
|
||||
max_lineno = max(max_lineno, get_max_lineno(child))
|
||||
return max_lineno
|
||||
line_number = get_max_lineno(tree)
|
||||
except Exception:
|
||||
pass
|
||||
# If there was an error, try to get the error line number from the traceback
|
||||
if run_result['error']:
|
||||
tb = run_result['error']
|
||||
import traceback
|
||||
try:
|
||||
tb_lines = traceback.extract_tb(traceback.TracebackException.from_string(tb).stack)
|
||||
if tb_lines:
|
||||
line_number = tb_lines[-1].lineno
|
||||
except Exception:
|
||||
pass
|
||||
log_leaderboard(username, problem['folder'], run_result['runtime'], memory_used, line_number)
|
||||
result = run_result
|
||||
return render_template('problem.html', problem=problem, result=result)
|
||||
|
||||
@app.template_filter('markdown')
|
||||
def markdown_filter(text):
|
||||
return Markup(md.markdown(text or '', extensions=['extra', 'sane_lists']))
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run(debug=True)
|
||||
48
src/leaderboard.py
Normal file
48
src/leaderboard.py
Normal file
@@ -0,0 +1,48 @@
|
||||
from flask_sqlalchemy import SQLAlchemy
|
||||
from flask import g
|
||||
import os
|
||||
import sqlite3
|
||||
from pathlib import Path
|
||||
|
||||
def get_db():
|
||||
if 'db' not in g:
|
||||
db_path = Path(__file__).parent / 'database' / 'db.sqlite3'
|
||||
db_path.parent.mkdir(exist_ok=True) # Ensure /database folder exists
|
||||
g.db = sqlite3.connect(db_path)
|
||||
return g.db
|
||||
|
||||
def create_leaderboard_table():
|
||||
db = get_db()
|
||||
cursor = db.cursor()
|
||||
cursor.execute('''
|
||||
CREATE TABLE IF NOT EXISTS leaderboard (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
username TEXT,
|
||||
problem_id INTEGER,
|
||||
runtime REAL,
|
||||
memory_used INTEGER,
|
||||
line_number INTEGER,
|
||||
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP
|
||||
)
|
||||
''')
|
||||
db.commit()
|
||||
|
||||
def log_leaderboard(username, problem_id, runtime, memory_used, line_number):
|
||||
db = get_db()
|
||||
cursor = db.cursor()
|
||||
cursor.execute('''
|
||||
INSERT INTO leaderboard (username, problem_id, runtime, memory_used, line_number)
|
||||
VALUES (?, ?, ?, ?, ?)
|
||||
''', (username, problem_id, runtime, memory_used, line_number))
|
||||
db.commit()
|
||||
|
||||
def get_leaderboard():
|
||||
db = get_db()
|
||||
cursor = db.cursor()
|
||||
cursor.execute('''
|
||||
SELECT username, problem_id, runtime, memory_used, line_number, timestamp
|
||||
FROM leaderboard
|
||||
ORDER BY runtime ASC, memory_used ASC
|
||||
LIMIT 20
|
||||
''')
|
||||
return cursor.fetchall()
|
||||
17
src/models.py
Normal file
17
src/models.py
Normal file
@@ -0,0 +1,17 @@
|
||||
from flask_sqlalchemy import SQLAlchemy
|
||||
|
||||
db = SQLAlchemy()
|
||||
|
||||
class Problem(db.Model):
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
title = db.Column(db.String(100), nullable=False)
|
||||
description = db.Column(db.Text, nullable=False)
|
||||
test_code = db.Column(db.Text, nullable=False) # Python code to test solution
|
||||
|
||||
class Solution(db.Model):
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
problem_id = db.Column(db.Integer, db.ForeignKey('problem.id'), nullable=False)
|
||||
user_code = db.Column(db.Text, nullable=False)
|
||||
passed = db.Column(db.Boolean, default=False)
|
||||
output = db.Column(db.Text)
|
||||
problem = db.relationship('Problem', backref=db.backref('solutions', lazy=True))
|
||||
37
src/problem_loader.py
Normal file
37
src/problem_loader.py
Normal file
@@ -0,0 +1,37 @@
|
||||
import json
|
||||
import os
|
||||
import threading
|
||||
import time
|
||||
from models import db, Problem
|
||||
from flask import current_app
|
||||
|
||||
def load_problems_from_json(json_path):
|
||||
if not os.path.exists(json_path):
|
||||
print(f"Problem JSON file not found: {json_path}")
|
||||
return
|
||||
with open(json_path, 'r') as f:
|
||||
problems = json.load(f)
|
||||
for p in problems:
|
||||
# Check if problem already exists by title
|
||||
existing = Problem.query.filter_by(title=p['title']).first()
|
||||
# Load test code from solution file if provided
|
||||
test_code = ''
|
||||
if 'solution' in p and os.path.exists(p['solution']):
|
||||
with open(p['solution'], 'r') as sf:
|
||||
test_code = sf.read()
|
||||
if existing:
|
||||
existing.description = p['description']
|
||||
existing.test_code = test_code
|
||||
else:
|
||||
new_problem = Problem(title=p['title'], description=p['description'], test_code=test_code)
|
||||
db.session.add(new_problem)
|
||||
db.session.commit()
|
||||
|
||||
def schedule_problem_reload(app, json_path, interval_hours=10):
|
||||
def reload_loop():
|
||||
while True:
|
||||
with app.app_context():
|
||||
load_problems_from_json(json_path)
|
||||
time.sleep(interval_hours * 3600)
|
||||
t = threading.Thread(target=reload_loop, daemon=True)
|
||||
t.start()
|
||||
384
src/problem_scanner.py
Normal file
384
src/problem_scanner.py
Normal file
@@ -0,0 +1,384 @@
|
||||
import os
|
||||
import time
|
||||
import json
|
||||
import sqlite3
|
||||
import threading
|
||||
import random
|
||||
import tempfile
|
||||
import subprocess
|
||||
import sys
|
||||
import traceback
|
||||
import io
|
||||
from pathlib import Path
|
||||
|
||||
try:
|
||||
from watchdog.observers import Observer
|
||||
from watchdog.events import FileSystemEventHandler
|
||||
WATCHDOG_AVAILABLE = True
|
||||
except ImportError:
|
||||
WATCHDOG_AVAILABLE = False
|
||||
|
||||
PROBLEMS_DIR = Path(__file__).parent / 'problems'
|
||||
DB_PATH = Path(__file__).parent / 'database/problems.sqlite3'
|
||||
|
||||
class ProblemScannerThread(threading.Thread):
|
||||
def __init__(self, scan_interval=2):
|
||||
super().__init__(daemon=True)
|
||||
self.scan_interval = scan_interval
|
||||
self.last_state = {}
|
||||
self.observer = None
|
||||
|
||||
def create_table(self, conn):
|
||||
c = conn.cursor()
|
||||
c.execute('PRAGMA journal_mode=WAL;')
|
||||
c.execute('''CREATE TABLE IF NOT EXISTS problems (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
folder TEXT,
|
||||
description TEXT,
|
||||
difficulty TEXT,
|
||||
test_code TEXT
|
||||
)''')
|
||||
conn.commit()
|
||||
|
||||
def scan(self):
|
||||
problems = []
|
||||
if not PROBLEMS_DIR.exists():
|
||||
print(f"Problems directory does not exist: {PROBLEMS_DIR}")
|
||||
return problems
|
||||
|
||||
for folder in PROBLEMS_DIR.iterdir():
|
||||
if folder.is_dir():
|
||||
# Dynamically find manifest file (manifest.json or manifets.json)
|
||||
manifest_path = None
|
||||
for candidate in ["manifest.json", "manifets.json"]:
|
||||
candidate_path = folder / candidate
|
||||
if candidate_path.exists():
|
||||
manifest_path = candidate_path
|
||||
break
|
||||
|
||||
desc_path = folder / 'description.md'
|
||||
test_path = folder / 'test.py'
|
||||
|
||||
# Check if required files exist
|
||||
if manifest_path and desc_path.exists() and test_path.exists():
|
||||
try:
|
||||
with open(desc_path, 'r', encoding='utf-8') as f:
|
||||
description = f.read()
|
||||
with open(test_path, 'r', encoding='utf-8') as f:
|
||||
test_code = f.read()
|
||||
with open(manifest_path, 'r', encoding='utf-8') as f:
|
||||
manifest = json.load(f)
|
||||
|
||||
difficulty = manifest.get('difficulty', 'unknown')
|
||||
|
||||
problems.append({
|
||||
'folder': folder.name,
|
||||
'description': description,
|
||||
'test_code': test_code,
|
||||
'difficulty': difficulty
|
||||
})
|
||||
print(f"Found problem: {folder.name} ; Difficulty: {difficulty}")
|
||||
except Exception as e:
|
||||
print(f"Error reading problem files for {folder.name}: {e}")
|
||||
else:
|
||||
missing_files = []
|
||||
if not manifest_path:
|
||||
missing_files.append("manifest.json/manifets.json")
|
||||
if not desc_path.exists():
|
||||
missing_files.append("description.md")
|
||||
if not test_path.exists():
|
||||
missing_files.append("test.py")
|
||||
print(f"Skipping {folder.name}: missing {', '.join(missing_files)}")
|
||||
|
||||
print(f"Total problems found: {len(problems)}")
|
||||
return problems
|
||||
|
||||
def update_db(self, problems, retries=5):
|
||||
for attempt in range(retries):
|
||||
try:
|
||||
conn = sqlite3.connect(DB_PATH, timeout=5)
|
||||
c = conn.cursor()
|
||||
c.execute('PRAGMA journal_mode=WAL;')
|
||||
|
||||
# Clear existing problems
|
||||
c.execute('DELETE FROM problems')
|
||||
|
||||
# Insert new problems
|
||||
for p in problems:
|
||||
c.execute('''INSERT INTO problems
|
||||
(folder, description, difficulty, test_code)
|
||||
VALUES (?, ?, ?, ?)''',
|
||||
(p['folder'], p['description'], p['difficulty'], p['test_code']))
|
||||
|
||||
conn.commit()
|
||||
print(f"Updated database with {len(problems)} problems")
|
||||
conn.close()
|
||||
return
|
||||
|
||||
except sqlite3.OperationalError as e:
|
||||
if 'locked' in str(e).lower():
|
||||
wait_time = 0.2 + random.random() * 0.3
|
||||
print(f"Database locked, retrying in {wait_time:.2f}s (attempt {attempt + 1})")
|
||||
time.sleep(wait_time)
|
||||
else:
|
||||
print(f"Database error: {e}")
|
||||
raise
|
||||
except Exception as e:
|
||||
print(f"Unexpected error updating database: {e}")
|
||||
raise
|
||||
|
||||
print('Failed to update problems DB after several retries due to lock.')
|
||||
|
||||
def rescan_and_update(self):
|
||||
print("Scanning for problems...")
|
||||
problems = self.scan()
|
||||
self.update_db(problems)
|
||||
|
||||
def run(self):
|
||||
print("Starting problem scanner...")
|
||||
|
||||
# Initial scan and table creation
|
||||
try:
|
||||
conn = sqlite3.connect(DB_PATH)
|
||||
self.create_table(conn)
|
||||
conn.close()
|
||||
print("Database initialized")
|
||||
except Exception as e:
|
||||
print(f"Failed to initialize database: {e}")
|
||||
return
|
||||
|
||||
# Initial scan
|
||||
self.rescan_and_update()
|
||||
|
||||
if WATCHDOG_AVAILABLE:
|
||||
print("Using watchdog for file monitoring")
|
||||
|
||||
class Handler(FileSystemEventHandler):
|
||||
def __init__(self, scanner):
|
||||
self.scanner = scanner
|
||||
self.last_event_time = 0
|
||||
|
||||
def on_any_event(self, event):
|
||||
# Debounce events to avoid too many rescans
|
||||
now = time.time()
|
||||
if now - self.last_event_time > 1: # Wait at least 1 second between rescans
|
||||
self.last_event_time = now
|
||||
print(f"File system event: {event.event_type} - {event.src_path}")
|
||||
self.scanner.rescan_and_update()
|
||||
|
||||
event_handler = Handler(self)
|
||||
self.observer = Observer()
|
||||
self.observer.schedule(event_handler, str(PROBLEMS_DIR), recursive=True)
|
||||
self.observer.start()
|
||||
|
||||
try:
|
||||
while True:
|
||||
time.sleep(1)
|
||||
except KeyboardInterrupt:
|
||||
print("Stopping problem scanner...")
|
||||
finally:
|
||||
self.observer.stop()
|
||||
self.observer.join()
|
||||
else:
|
||||
print(f"Watchdog not available, using polling every {self.scan_interval}s")
|
||||
# Fallback: poll every scan_interval seconds
|
||||
try:
|
||||
while True:
|
||||
time.sleep(self.scan_interval)
|
||||
self.rescan_and_update()
|
||||
except KeyboardInterrupt:
|
||||
print("Stopping problem scanner...")
|
||||
|
||||
def start_problem_scanner():
|
||||
scanner = ProblemScannerThread()
|
||||
scanner.start()
|
||||
return scanner
|
||||
|
||||
# Flask model loading functions
|
||||
def load_problems_from_json(json_path):
|
||||
"""Load problems from JSON file into Flask database"""
|
||||
if not os.path.exists(json_path):
|
||||
print(f"Problem JSON file not found: {json_path}")
|
||||
return
|
||||
|
||||
try:
|
||||
with open(json_path, 'r', encoding='utf-8') as f:
|
||||
problems = json.load(f)
|
||||
except Exception as e:
|
||||
print(f"Error reading JSON file: {e}")
|
||||
return
|
||||
|
||||
# This assumes you have imported the necessary Flask/SQLAlchemy components
|
||||
try:
|
||||
from models import db, Problem
|
||||
|
||||
for p in problems:
|
||||
# Check if problem already exists by title
|
||||
existing = Problem.query.filter_by(title=p['title']).first()
|
||||
|
||||
# Load test code from solution file if provided
|
||||
test_code = ''
|
||||
if 'solution' in p and os.path.exists(p['solution']):
|
||||
try:
|
||||
with open(p['solution'], 'r', encoding='utf-8') as sf:
|
||||
test_code = sf.read()
|
||||
except Exception as e:
|
||||
print(f"Error reading solution file for {p['title']}: {e}")
|
||||
|
||||
if existing:
|
||||
existing.description = p['description']
|
||||
existing.test_code = test_code
|
||||
print(f"Updated problem: {p['title']}")
|
||||
else:
|
||||
new_problem = Problem(title=p['title'], description=p['description'], test_code=test_code)
|
||||
db.session.add(new_problem)
|
||||
print(f"Added new problem: {p['title']}")
|
||||
|
||||
db.session.commit()
|
||||
print("Successfully updated problems from JSON")
|
||||
|
||||
except ImportError:
|
||||
print("Flask models not available - skipping JSON load")
|
||||
except Exception as e:
|
||||
print(f"Error loading problems from JSON: {e}")
|
||||
|
||||
def schedule_problem_reload(app, json_path, interval_hours=10):
|
||||
"""Schedule periodic reloading of problems from JSON"""
|
||||
def reload_loop():
|
||||
while True:
|
||||
try:
|
||||
with app.app_context():
|
||||
load_problems_from_json(json_path)
|
||||
time.sleep(interval_hours * 3600)
|
||||
except Exception as e:
|
||||
print(f"Error in problem reload loop: {e}")
|
||||
time.sleep(60) # Wait 1 minute before retrying
|
||||
|
||||
t = threading.Thread(target=reload_loop, daemon=True)
|
||||
t.start()
|
||||
|
||||
def run_code_against_tests(user_code, test_code, timeout=10):
|
||||
"""
|
||||
Execute user code against test code with proper error handling.
|
||||
|
||||
Args:
|
||||
user_code: The user's solution code
|
||||
test_code: The test code to validate the solution
|
||||
timeout: Maximum execution time in seconds
|
||||
|
||||
Returns:
|
||||
dict: Result with passed, output, runtime, and error fields
|
||||
"""
|
||||
if not user_code or not user_code.strip():
|
||||
return {
|
||||
'passed': False,
|
||||
'output': '',
|
||||
'runtime': 0,
|
||||
'error': 'No code provided'
|
||||
}
|
||||
|
||||
if not test_code or not test_code.strip():
|
||||
return {
|
||||
'passed': False,
|
||||
'output': '',
|
||||
'runtime': 0,
|
||||
'error': 'No test code available'
|
||||
}
|
||||
|
||||
start_time = time.perf_counter()
|
||||
output = ''
|
||||
error = None
|
||||
passed = False
|
||||
temp_file = None
|
||||
|
||||
try:
|
||||
# Check if unittest is used in test_code
|
||||
if 'unittest' in test_code:
|
||||
# Create temporary file with user code + test code
|
||||
with tempfile.NamedTemporaryFile('w+', suffix='.py', delete=False, encoding='utf-8') as f:
|
||||
# Combine user code and test code
|
||||
combined_code = f"{user_code}\n\n{test_code}"
|
||||
f.write(combined_code)
|
||||
f.flush()
|
||||
temp_file = f.name
|
||||
|
||||
try:
|
||||
# Run the file as a subprocess with timeout
|
||||
proc = subprocess.run(
|
||||
[sys.executable, temp_file],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
timeout=timeout,
|
||||
encoding='utf-8'
|
||||
)
|
||||
|
||||
output = proc.stdout
|
||||
if proc.stderr:
|
||||
output += f"\nSTDERR:\n{proc.stderr}"
|
||||
|
||||
passed = proc.returncode == 0
|
||||
if not passed:
|
||||
error = f"Tests failed. Return code: {proc.returncode}\n{output}"
|
||||
|
||||
except subprocess.TimeoutExpired:
|
||||
passed = False
|
||||
error = f"Code execution timed out after {timeout} seconds"
|
||||
output = "Execution timed out"
|
||||
|
||||
else:
|
||||
# Direct execution approach for simple assert-based tests
|
||||
local_ns = {}
|
||||
|
||||
# Capture stdout
|
||||
old_stdout = sys.stdout
|
||||
captured_output = io.StringIO()
|
||||
sys.stdout = captured_output
|
||||
|
||||
try:
|
||||
# Execute user code first
|
||||
exec(user_code, {}, local_ns)
|
||||
|
||||
# Execute test code in the same namespace
|
||||
exec(test_code, local_ns, local_ns)
|
||||
|
||||
# If we get here without exceptions, tests passed
|
||||
passed = True
|
||||
|
||||
except AssertionError as e:
|
||||
passed = False
|
||||
error = f"Assertion failed: {str(e)}"
|
||||
|
||||
except Exception as e:
|
||||
passed = False
|
||||
error = f"Runtime error: {traceback.format_exc()}"
|
||||
|
||||
finally:
|
||||
output = captured_output.getvalue()
|
||||
sys.stdout = old_stdout
|
||||
|
||||
except Exception as e:
|
||||
passed = False
|
||||
error = f"Execution error: {traceback.format_exc()}"
|
||||
|
||||
finally:
|
||||
# Clean up temporary file
|
||||
if temp_file and os.path.exists(temp_file):
|
||||
try:
|
||||
os.unlink(temp_file)
|
||||
except Exception as e:
|
||||
print(f"Warning: Could not delete temp file {temp_file}: {e}")
|
||||
|
||||
runtime = time.perf_counter() - start_time
|
||||
|
||||
result = {
|
||||
'passed': passed,
|
||||
'output': output.strip() if output else '',
|
||||
'runtime': runtime,
|
||||
'error': error if not passed else None
|
||||
}
|
||||
|
||||
print(f"Test execution result: passed={passed}, runtime={runtime:.3f}s")
|
||||
if error:
|
||||
print(f"Error: {error}")
|
||||
|
||||
return result
|
||||
40
src/problems/fibonacisequence/description.md
Normal file
40
src/problems/fibonacisequence/description.md
Normal file
@@ -0,0 +1,40 @@
|
||||
## Fibonacci Number
|
||||
|
||||
Write a function called `fibonacci` that takes a non-negative integer `n` as input and returns the **n-th Fibonacci number**.
|
||||
|
||||
The Fibonacci sequence is defined as:
|
||||
|
||||
* `F(0) = 0`
|
||||
* `F(1) = 1`
|
||||
* `F(n) = F(n-1) + F(n-2)` for `n > 1`
|
||||
|
||||
### Function Signature:
|
||||
|
||||
```python
|
||||
def fibonacci(n):
|
||||
# return your solution
|
||||
```
|
||||
|
||||
#### Requirements
|
||||
|
||||
* The function should return the `n`-th number in the Fibonacci sequence.
|
||||
* If `n` is less than `0`, print `"Incorrect input"`.
|
||||
* Your function will be tested with:
|
||||
|
||||
* Base cases (`n = 0` and `n = 1`)
|
||||
* Small values of `n`
|
||||
* Larger values of `n` (e.g., 9)
|
||||
* Multiple test cases in sequence
|
||||
|
||||
#### Example:
|
||||
|
||||
```python
|
||||
fibonacci(0) # returns 0
|
||||
fibonacci(1) # returns 1
|
||||
fibonacci(2) # returns 1
|
||||
fibonacci(3) # returns 2
|
||||
fibonacci(5) # returns 5
|
||||
fibonacci(9) # returns 34
|
||||
```
|
||||
|
||||
You can copy this into your problem’s solution description.
|
||||
7
src/problems/fibonacisequence/manifest.json
Normal file
7
src/problems/fibonacisequence/manifest.json
Normal file
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"title": "Fibonacci Sequence",
|
||||
"description": "Calculate the n-th Fibonacci number using a function. The Fibonacci sequence is defined as follows: F(0) = 0, F(1) = 1, and F(n) = F(n-1) + F(n-2) for n > 1.",
|
||||
"description_md": "problems/fibonacisequence/description.md",
|
||||
"difficulty": "medium",
|
||||
"test_code": "problems/fibonacisequence/test.py"
|
||||
}
|
||||
52
src/problems/fibonacisequence/test.py
Normal file
52
src/problems/fibonacisequence/test.py
Normal file
@@ -0,0 +1,52 @@
|
||||
import unittest
|
||||
|
||||
class TestSolution(unittest.TestCase):
|
||||
def test_simple(self):
|
||||
test_cases = [
|
||||
(0, 0), # Base case: n = 0
|
||||
(1, 1), # Base case: n = 1
|
||||
(2, 1), # Fibonacci(2) = 1
|
||||
(3, 2), # Fibonacci(3) = 2
|
||||
(5, 5), # Fibonacci(5) = 5
|
||||
(9, 34), # Fibonacci(9) = 34
|
||||
]
|
||||
|
||||
print("\n=== Function Output Test Results ===")
|
||||
for input_val, expected in test_cases:
|
||||
try:
|
||||
actual = fibonacci(input_val) # pyright: ignore[reportUndefinedVariable]
|
||||
status = "✓ PASS" if actual == expected else "✗ FAIL"
|
||||
print(f"{status} | Input: {input_val} -> Got: {actual} | Expected: {expected}")
|
||||
self.assertEqual(actual, expected)
|
||||
except Exception as e:
|
||||
print(f"✗ ERROR | Input: {input_val} -> Exception: {e}")
|
||||
raise
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main(verbosity=2)
|
||||
|
||||
"""
|
||||
def fibonacci(n):
|
||||
a = 0
|
||||
b = 1
|
||||
|
||||
# Check if n is less than 0
|
||||
if n < 0:
|
||||
print("Incorrect input")
|
||||
|
||||
# Check if n is equal to 0
|
||||
elif n == 0:
|
||||
return 0
|
||||
|
||||
# Check if n is equal to 1
|
||||
elif n == 1:
|
||||
return b
|
||||
else:
|
||||
for i in range(1, n):
|
||||
c = a + b
|
||||
a = b
|
||||
b = c
|
||||
return b
|
||||
|
||||
print(fibonacci(9))
|
||||
"""
|
||||
29
src/problems/reversedstring/description.md
Normal file
29
src/problems/reversedstring/description.md
Normal file
@@ -0,0 +1,29 @@
|
||||
## Reversed String
|
||||
|
||||
Write a function called ```revstring``` that takes a string as input and returns the reversed string.
|
||||
|
||||
### Function Signature:
|
||||
```python
|
||||
def revstring(x).
|
||||
# return your solution
|
||||
```
|
||||
|
||||
#### Requirements
|
||||
- The function should return the input string reversed
|
||||
- Your function will be tested with various cases, including:
|
||||
- An empty string
|
||||
- A single character
|
||||
- A palindrome ("racecar")
|
||||
- A string of numbers ("12345")
|
||||
- Special characters
|
||||
- A normal string ( "Hello World" )
|
||||
|
||||
#### Example:
|
||||
```python
|
||||
revstring("Hello World") # returns "dlroW olleH"
|
||||
revstring("") # returns ""
|
||||
revstring("racecar") # returns "racecar"
|
||||
revstring("12345") # returns "54321"
|
||||
revstring("!@# $%") # returns "%$ #@!"
|
||||
```
|
||||
You can copy this into your problems solution
|
||||
7
src/problems/reversedstring/manifest.json
Normal file
7
src/problems/reversedstring/manifest.json
Normal file
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"title":"Reversed String",
|
||||
"description":"Reverse a String using a Function ; Try to write as little code as possible",
|
||||
"description_md":"problems/reversedstring/description.md",
|
||||
"difficulty":"easy",
|
||||
"test_code":"problems/reversedstring/test.py"
|
||||
}
|
||||
26
src/problems/reversedstring/test.py
Normal file
26
src/problems/reversedstring/test.py
Normal file
@@ -0,0 +1,26 @@
|
||||
import unittest
|
||||
|
||||
class TestSolution(unittest.TestCase):
|
||||
def test_simple(self):
|
||||
test_cases = [
|
||||
("Hello World", "dlroW olleH"),
|
||||
("", ""),
|
||||
("a", "a"),
|
||||
("racecar", "racecar"),
|
||||
("12345", "54321"),
|
||||
("!@# $%", "%$ #@!")
|
||||
]
|
||||
|
||||
print("\n=== Function Output Test Results ===")
|
||||
for input_val, expected in test_cases:
|
||||
try:
|
||||
actual = revstring(input_val) # pyright: ignore[reportUndefinedVariable]
|
||||
status = "✓ PASS" if actual == expected else "✗ FAIL"
|
||||
print(f"{status} | Input: '{input_val}' -> Got: '{actual}' | Expected: '{expected}'")
|
||||
self.assertEqual(actual, expected)
|
||||
except Exception as e:
|
||||
print(f"✗ ERROR | Input: '{input_val}' -> Exception: {e}")
|
||||
raise
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main(verbosity=2)
|
||||
1
src/problems/sortlist/description.md
Normal file
1
src/problems/sortlist/description.md
Normal file
@@ -0,0 +1 @@
|
||||
this is a easy sorting problem **it is solvable in less than 2 seconds**
|
||||
7
src/problems/sortlist/manifets.json
Normal file
7
src/problems/sortlist/manifets.json
Normal file
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"title": "Sort List",
|
||||
"description": "Sort a List with a Function (sortlist); the function is supposed to take the list as an argument and is supposed to return the sorted list and print it.",
|
||||
"description_md": "problems/sortlist/description.md",
|
||||
"difficulty": "easy",
|
||||
"test_code": "problems/sortlist/test.py"
|
||||
}
|
||||
17
src/problems/sortlist/test.py
Normal file
17
src/problems/sortlist/test.py
Normal file
@@ -0,0 +1,17 @@
|
||||
import unittest
|
||||
|
||||
# This is the function the user is expected to write.
|
||||
# Its a really simple one, the user can choose not to type tho.
|
||||
|
||||
#def sortlist(lst = [4,3,2,1]) -> list:
|
||||
#return sorted(lst)
|
||||
|
||||
class TestSolution(unittest.TestCase):
|
||||
def test_sort(self):
|
||||
# define x as a empty array.
|
||||
# this will be used for the functiun ; a empty var does not work.
|
||||
self.x = []
|
||||
self.assertEqual(sortlist(self.x), sorted(self.x)) # pyright: ignore[reportUndefinedVariable]
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
185
src/static/index.css
Normal file
185
src/static/index.css
Normal file
@@ -0,0 +1,185 @@
|
||||
:root {
|
||||
--bg: #f6f8fb;
|
||||
--card: #fff;
|
||||
--muted: #6b7280;
|
||||
--accent: #2563eb;
|
||||
--shadow: 0 4px 12px rgba(16, 24, 40, 0.06);
|
||||
--radius: 8px;
|
||||
--mono: 'JetBrains Mono', monospace;
|
||||
}
|
||||
* { box-sizing: border-box; margin: 0; padding: 0; }
|
||||
html, body {
|
||||
height: 100%;
|
||||
}
|
||||
body {
|
||||
font-family: Inter, sans-serif;
|
||||
background: var(--bg);
|
||||
color: #0f172a;
|
||||
padding: 16px;
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
}
|
||||
.wrap {
|
||||
width: 100%;
|
||||
max-width: 1100px;
|
||||
}
|
||||
header {
|
||||
margin-bottom: 14px;
|
||||
}
|
||||
header h1 {
|
||||
font-size: 1.6rem;
|
||||
color: #111827;
|
||||
}
|
||||
header p {
|
||||
color: var(--muted);
|
||||
font-size: 0.9rem;
|
||||
}
|
||||
.content {
|
||||
display: grid;
|
||||
grid-template-columns: 1fr 1fr;
|
||||
gap: 12px;
|
||||
}
|
||||
.content.single-column {
|
||||
grid-template-columns: 1fr;
|
||||
}
|
||||
.card {
|
||||
background: var(--card);
|
||||
border-radius: var(--radius);
|
||||
box-shadow: var(--shadow);
|
||||
padding: 12px;
|
||||
}
|
||||
/* Search/filter controls */
|
||||
.search-controls {
|
||||
margin-bottom: 12px;
|
||||
display: flex;
|
||||
gap: 8px;
|
||||
}
|
||||
.search-input {
|
||||
flex: 1;
|
||||
padding: 6px 10px;
|
||||
border: 1px solid #e5e7eb;
|
||||
border-radius: 4px;
|
||||
font-size: 0.9rem;
|
||||
}
|
||||
.filter-select {
|
||||
padding: 6px 8px;
|
||||
border: 1px solid #e5e7eb;
|
||||
border-radius: 4px;
|
||||
font-size: 0.9rem;
|
||||
background: white;
|
||||
}
|
||||
/* Problems list */
|
||||
.problems-list .problem-item {
|
||||
padding: 8px;
|
||||
border-bottom: 1px solid #e5e7eb;
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
}
|
||||
.problem-item:last-child {
|
||||
border-bottom: none;
|
||||
}
|
||||
.problem-item a {
|
||||
text-decoration: none;
|
||||
color: #0077ff;
|
||||
font-weight: 600;
|
||||
}
|
||||
/* Difficulty badge */
|
||||
.difficulty {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
padding: 0.25em 0.6em;
|
||||
border-radius: 10px;
|
||||
font-size: 0.85em;
|
||||
font-weight: bold;
|
||||
text-transform: uppercase;
|
||||
color: white;
|
||||
white-space: nowrap;
|
||||
}
|
||||
.difficulty[data-difficulty="easy"] {
|
||||
background-color: #4CAF50; /* Green */
|
||||
}
|
||||
.difficulty[data-difficulty="medium"] {
|
||||
background-color: #FFC107; /* Amber */
|
||||
color: #333;
|
||||
}
|
||||
.difficulty[data-difficulty="hard"] {
|
||||
background-color: #F44336; /* Red */
|
||||
}
|
||||
/* Leaderboard */
|
||||
.leaderboard-head {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
margin-bottom: 6px;
|
||||
}
|
||||
.leaderboard-controls {
|
||||
display: flex;
|
||||
gap: 8px;
|
||||
margin-bottom: 12px;
|
||||
}
|
||||
.leaderboard-table {
|
||||
width: 100%;
|
||||
border-collapse: collapse;
|
||||
font-size: 0.9rem;
|
||||
}
|
||||
.leaderboard-table th,
|
||||
.leaderboard-table td {
|
||||
padding: 6px 8px;
|
||||
border-bottom: 1px solid #e5e7eb;
|
||||
text-align: left;
|
||||
}
|
||||
.leaderboard-table th {
|
||||
background: #f9fafb;
|
||||
font-weight: 600;
|
||||
color: var(--muted);
|
||||
}
|
||||
.leaderboard-table tr:hover {
|
||||
background: #f3f4f6;
|
||||
}
|
||||
/* Sort indicators */
|
||||
.sortable {
|
||||
cursor: pointer;
|
||||
position: relative;
|
||||
padding-right: 16px;
|
||||
}
|
||||
.sortable::after {
|
||||
content: "↕";
|
||||
position: absolute;
|
||||
right: 4px;
|
||||
top: 50%;
|
||||
transform: translateY(-50%);
|
||||
font-size: 0.8em;
|
||||
opacity: 0.5;
|
||||
}
|
||||
.sort-asc::after {
|
||||
content: "↑";
|
||||
opacity: 1;
|
||||
}
|
||||
.sort-desc::after {
|
||||
content: "↓";
|
||||
opacity: 1;
|
||||
}
|
||||
/* Toggle button */
|
||||
.btn {
|
||||
border: none;
|
||||
background: transparent;
|
||||
cursor: pointer;
|
||||
color: var(--accent);
|
||||
font-size: 0.85rem;
|
||||
padding: 4px 6px;
|
||||
border-radius: 4px;
|
||||
}
|
||||
.btn:hover {
|
||||
background: rgba(37, 99, 235, 0.08);
|
||||
}
|
||||
.btn.active {
|
||||
background: rgba(37, 99, 235, 0.15);
|
||||
}
|
||||
@media (max-width: 800px) {
|
||||
.content { grid-template-columns: 1fr; }
|
||||
.leaderboard-controls {
|
||||
flex-direction: column;
|
||||
}
|
||||
}
|
||||
260
src/static/style.css
Normal file
260
src/static/style.css
Normal file
@@ -0,0 +1,260 @@
|
||||
/* Reset and base styles */
|
||||
* {
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
body {
|
||||
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
|
||||
line-height: 1.6;
|
||||
color: #333;
|
||||
background-color: #f8f9fa;
|
||||
padding: 20px;
|
||||
max-width: 1200px;
|
||||
margin: 0 auto;
|
||||
}
|
||||
|
||||
/* Main heading */
|
||||
h1 {
|
||||
color: #2c3e50;
|
||||
margin-bottom: -10px;
|
||||
padding-bottom: 3px;
|
||||
border-bottom: 3px solid #3498db;
|
||||
font-size: 2.2em;
|
||||
}
|
||||
|
||||
h2 {
|
||||
color: #34495e;
|
||||
margin: 30px 0 20px 0;
|
||||
font-size: 1.5em;
|
||||
}
|
||||
|
||||
h3 {
|
||||
color: #34495e;
|
||||
margin: 25px 0 15px 0;
|
||||
font-size: 1.3em;
|
||||
}
|
||||
|
||||
/* Links and buttons */
|
||||
a {
|
||||
color: #3498db;
|
||||
text-decoration: none;
|
||||
padding: 8px 16px;
|
||||
border-radius: 5px;
|
||||
transition: background-color 0.3s ease;
|
||||
}
|
||||
|
||||
a:hover {
|
||||
background-color: #e3f2fd;
|
||||
text-decoration: none;
|
||||
}
|
||||
|
||||
/* Primary action link (Submit New Problem) */
|
||||
a[href="/problem/new"] {
|
||||
background-color: #3498db;
|
||||
color: white;
|
||||
font-weight: 600;
|
||||
margin-bottom: 30px;
|
||||
display: inline-block;
|
||||
padding: 12px 24px;
|
||||
border-radius: 8px;
|
||||
}
|
||||
|
||||
a[href="/problem/new"]:hover {
|
||||
background-color: #2980b9;
|
||||
}
|
||||
|
||||
/* Problem list */
|
||||
ul {
|
||||
list-style: none;
|
||||
background: white;
|
||||
border-radius: 8px;
|
||||
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
|
||||
padding: 25px;
|
||||
margin: 20px 0;
|
||||
}
|
||||
|
||||
li {
|
||||
padding: 15px 0;
|
||||
border-bottom: 1px solid #eee;
|
||||
}
|
||||
|
||||
li:last-child {
|
||||
border-bottom: none;
|
||||
}
|
||||
|
||||
li a {
|
||||
display: block;
|
||||
padding: 12px 20px;
|
||||
margin: -12px -20px;
|
||||
border-radius: 6px;
|
||||
font-size: 1.1em;
|
||||
}
|
||||
|
||||
li a:hover {
|
||||
background-color: #f8f9fa;
|
||||
transform: translateX(5px);
|
||||
transition: all 0.2s ease;
|
||||
}
|
||||
|
||||
/* Problem page specific styles */
|
||||
.problem-header {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
margin-bottom: 30px;
|
||||
gap: 20px;
|
||||
}
|
||||
|
||||
.back-btn {
|
||||
background-color: #95a5a6;
|
||||
color: white;
|
||||
border: none;
|
||||
padding: 10px 20px;
|
||||
border-radius: 6px;
|
||||
cursor: pointer;
|
||||
font-size: 14px;
|
||||
font-weight: 500;
|
||||
transition: background-color 0.3s ease;
|
||||
}
|
||||
|
||||
.back-btn:hover {
|
||||
background-color: #7f8c8d;
|
||||
}
|
||||
|
||||
.problem-desc {
|
||||
background: white;
|
||||
padding: 30px;
|
||||
border-radius: 8px;
|
||||
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
|
||||
margin-bottom: 30px;
|
||||
font-size: 1.1em;
|
||||
line-height: 1.7;
|
||||
}
|
||||
|
||||
/* Editor section */
|
||||
.editor-section {
|
||||
background: white;
|
||||
padding: 30px;
|
||||
border-radius: 8px;
|
||||
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
|
||||
margin-bottom: 30px;
|
||||
}
|
||||
|
||||
#editor {
|
||||
border: 2px solid #ddd;
|
||||
border-radius: 8px;
|
||||
margin: 20px 0;
|
||||
height: 400px;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
.editor-actions {
|
||||
margin-top: 20px;
|
||||
text-align: right;
|
||||
}
|
||||
|
||||
form button[type="submit"] {
|
||||
background-color: #27ae60;
|
||||
color: white;
|
||||
border: none;
|
||||
padding: 12px 30px;
|
||||
border-radius: 8px;
|
||||
cursor: pointer;
|
||||
font-size: 16px;
|
||||
font-weight: 600;
|
||||
transition: background-color 0.3s ease;
|
||||
}
|
||||
|
||||
form button[type="submit"]:hover {
|
||||
background-color: #229954;
|
||||
}
|
||||
|
||||
/* Results section */
|
||||
b {
|
||||
color: #2c3e50;
|
||||
display: inline-block;
|
||||
margin: 10px 0 5px 0;
|
||||
}
|
||||
|
||||
pre {
|
||||
background-color: #f4f4f4;
|
||||
padding: 20px;
|
||||
border-radius: 6px;
|
||||
border-left: 4px solid #3498db;
|
||||
margin: 10px 0 20px 0;
|
||||
overflow-x: auto;
|
||||
font-family: 'JetBrains Mono', 'Courier New', monospace;
|
||||
font-size: 14px;
|
||||
line-height: 1.4;
|
||||
}
|
||||
|
||||
pre[style*="color:red"] {
|
||||
border-left-color: #e74c3c;
|
||||
background-color: #fdf2f2;
|
||||
}
|
||||
|
||||
/* Status messages */
|
||||
p[style*="color:green"] {
|
||||
background-color: #d4edda;
|
||||
color: #155724;
|
||||
padding: 15px 20px;
|
||||
border-radius: 6px;
|
||||
border-left: 4px solid #27ae60;
|
||||
margin: 20px 0;
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
p[style*="color:red"] {
|
||||
background-color: #f8d7da;
|
||||
color: #721c24;
|
||||
padding: 15px 20px;
|
||||
border-radius: 6px;
|
||||
border-left: 4px solid #e74c3c;
|
||||
margin: 20px 0;
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
/* Back to Problems link */
|
||||
a[href="/"] {
|
||||
display: inline-block;
|
||||
margin-top: 30px;
|
||||
background-color: #6c757d;
|
||||
color: white;
|
||||
padding: 10px 20px;
|
||||
border-radius: 6px;
|
||||
font-weight: 500;
|
||||
}
|
||||
|
||||
a[href="/"]:hover {
|
||||
background-color: #5a6268;
|
||||
}
|
||||
|
||||
/* Responsive design */
|
||||
@media (max-width: 768px) {
|
||||
body {
|
||||
padding: 15px;
|
||||
}
|
||||
|
||||
.problem-header {
|
||||
flex-direction: column;
|
||||
align-items: flex-start;
|
||||
gap: 15px;
|
||||
}
|
||||
|
||||
h1 {
|
||||
font-size: 1.8em;
|
||||
}
|
||||
|
||||
.problem-desc, .editor-section, ul {
|
||||
padding: 20px;
|
||||
}
|
||||
|
||||
#editor {
|
||||
height: 300px;
|
||||
}
|
||||
|
||||
.editor-actions {
|
||||
text-align: center;
|
||||
}
|
||||
}
|
||||
BIN
src/templates/favicon/favicon.ico
Normal file
BIN
src/templates/favicon/favicon.ico
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 15 KiB |
102
src/templates/index.html
Normal file
102
src/templates/index.html
Normal file
@@ -0,0 +1,102 @@
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<meta name="viewport" content="width=device-width,initial-scale=1" />
|
||||
<title>Quick Problem Platform</title>
|
||||
<!--<link rel="favicon" href="/favicon/favicon.ico">-->
|
||||
<script src="script.js" async defer></script>
|
||||
<link rel="stylesheet" href="/static/index.css">
|
||||
|
||||
</head>
|
||||
<body>
|
||||
<div class="wrap">
|
||||
<header>
|
||||
<h1>Quick Problem Platform</h1>
|
||||
</header>
|
||||
<div class="content" id="contentContainer">
|
||||
<!-- Problems -->
|
||||
<section class="card problems-list">
|
||||
<div class="search-controls">
|
||||
<input
|
||||
type="text"
|
||||
class="search-input"
|
||||
id="problemSearch"
|
||||
placeholder="Search problems..."
|
||||
/>
|
||||
</div>
|
||||
<h2 style="margin-bottom:6px;font-size:1.1rem">Problems</h2>
|
||||
<div id="problemsContainer">
|
||||
{% for folder, description, test_code, difficulty in problems %}
|
||||
<div class="problem-item" data-name="{{ folder.replace('_',' ').title() }}" data-desc="{{ description }}">
|
||||
<a href="/problem/{{ folder }}">{{ folder.replace('_',' ').title() }}</a>
|
||||
<span class="difficulty" data-difficulty="{{ difficulty|lower }}">{{ difficulty }}</span>
|
||||
</div>
|
||||
{% else %}
|
||||
<div class="problem-item">No problems yet.</div>
|
||||
{% endfor %}
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<!-- Leaderboard -->
|
||||
<section class="card" id="leaderboardSection">
|
||||
<div class="leaderboard-head">
|
||||
<h2 style="font-size:1.1rem;margin:0">Leaderboard</h2>
|
||||
<button class="btn" id="toggleLeaderboard">Hide</button>
|
||||
</div>
|
||||
<div class="leaderboard-controls">
|
||||
<input
|
||||
type="text"
|
||||
class="search-input"
|
||||
id="userSearch"
|
||||
placeholder="Filter by user..."
|
||||
/>
|
||||
<input
|
||||
type="text"
|
||||
class="search-input"
|
||||
id="problemFilter"
|
||||
placeholder="Filter by problem..."
|
||||
/>
|
||||
<select class="filter-select" id="runtimeFilter">
|
||||
<option value="">All runtimes</option>
|
||||
<option value="best">Best runtime</option>
|
||||
<option value="worst">Worst runtime</option>
|
||||
</select>
|
||||
</div>
|
||||
<div id="leaderboardContainer">
|
||||
<table class="leaderboard-table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th class="sortable" data-sort="rank">Rank</th>
|
||||
<th class="sortable" data-sort="user">User</th>
|
||||
<th class="sortable" data-sort="problem">Problem</th>
|
||||
<th class="sortable" data-sort="runtime">Runtime (s)</th>
|
||||
<th class="sortable" data-sort="memory">Memory (KB)</th>
|
||||
<th>Line</th>
|
||||
<th class="sortable" data-sort="timestamp">Timestamp</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody id="leaderboardBody">
|
||||
{% for entry in leaderboard %}
|
||||
<tr data-user="{{ entry[0] }}" data-problem="{{ problem_titles.get(entry[1], 'Unknown') }}"
|
||||
data-runtime="{{ '%.4f'|format(entry[2]) }}" data-memory="{{ entry[3] }}"
|
||||
data-timestamp="{{ entry[5] }}">
|
||||
<td>{{ loop.index }}</td>
|
||||
<td>{{ entry[0] }}</td>
|
||||
<td>{{ problem_titles.get(entry[1], 'Unknown') }}</td>
|
||||
<td>{{ '%.4f'|format(entry[2]) }}</td>
|
||||
<td>{{ entry[3] }}</td>
|
||||
<td>{{ entry[4] if entry[4] else '-' }}</td>
|
||||
<td>{{ entry[5] }}</td>
|
||||
</tr>
|
||||
{% else %}
|
||||
<tr><td colspan="7">No leaderboard entries yet.</td></tr>
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</section>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
296
src/templates/problem.html
Normal file
296
src/templates/problem.html
Normal file
@@ -0,0 +1,296 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>{{ problem.title }} - Coding Problem</title>
|
||||
<link rel="stylesheet" href="/static/style.css">
|
||||
<link href="https://fonts.cdnfonts.com/css/jetbrains-mono" rel="stylesheet">
|
||||
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600&display=swap" rel="stylesheet">
|
||||
<style>
|
||||
body {
|
||||
font-family: 'Inter', sans-serif;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
background-color: #f9f9f9;
|
||||
color: #333;
|
||||
min-height: 100vh; /* allow content to grow */
|
||||
overflow-y: auto; /* allow vertical scroll */
|
||||
box-sizing: border-box;
|
||||
}
|
||||
*, *::before, *::after {
|
||||
box-sizing: inherit;
|
||||
}
|
||||
|
||||
.main-container {
|
||||
display: flex;
|
||||
flex-wrap: wrap; /* wrap on small screens */
|
||||
min-height: 100vh;
|
||||
width: 100vw;
|
||||
}
|
||||
|
||||
.problem-panel {
|
||||
flex: 1 1 400px; /* grow/shrink with base 400px */
|
||||
min-width: 300px;
|
||||
background: white;
|
||||
overflow-y: auto;
|
||||
padding: 20px;
|
||||
border-right: 1px solid #eaeaea;
|
||||
max-height: 100vh;
|
||||
}
|
||||
|
||||
.editor-container {
|
||||
flex: 1 1 400px;
|
||||
min-width: 300px;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
background: white;
|
||||
max-height: 100vh;
|
||||
overflow: hidden; /* internal scroll handling */
|
||||
}
|
||||
|
||||
.editor-header {
|
||||
padding: 15px 20px;
|
||||
border-bottom: 1px solid #eaeaea;
|
||||
flex-shrink: 0;
|
||||
}
|
||||
|
||||
.editor-wrapper {
|
||||
flex: 1 1 auto;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
min-height: 0;
|
||||
padding: 0 20px;
|
||||
overflow-y: auto;
|
||||
}
|
||||
|
||||
.problem-header {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
margin-bottom: 20px;
|
||||
}
|
||||
|
||||
.back-btn {
|
||||
background: none;
|
||||
border: none;
|
||||
cursor: pointer;
|
||||
font-size: 16px;
|
||||
color: #666;
|
||||
margin-right: 15px;
|
||||
padding: 5px;
|
||||
}
|
||||
|
||||
.back-btn:hover {
|
||||
color: #000;
|
||||
}
|
||||
|
||||
h1 {
|
||||
font-size: 22px;
|
||||
font-weight: 600;
|
||||
margin: 0;
|
||||
color: #1a1a1a;
|
||||
}
|
||||
|
||||
.problem-desc {
|
||||
line-height: 1.6;
|
||||
font-size: 15px;
|
||||
overflow-wrap: break-word;
|
||||
}
|
||||
|
||||
.problem-desc pre {
|
||||
background: #f6f8fa;
|
||||
padding: 12px;
|
||||
border-radius: 4px;
|
||||
overflow-x: auto;
|
||||
font-family: 'JetBrains Mono', monospace;
|
||||
font-size: 14px;
|
||||
}
|
||||
|
||||
.problem-desc code {
|
||||
background: #f6f8fa;
|
||||
padding: 2px 4px;
|
||||
border-radius: 3px;
|
||||
font-family: 'JetBrains Mono', monospace;
|
||||
font-size: 14px;
|
||||
}
|
||||
|
||||
.editor-actions {
|
||||
padding: 15px 0;
|
||||
display: flex;
|
||||
justify-content: flex-end;
|
||||
flex-shrink: 0;
|
||||
}
|
||||
|
||||
.editor-actions button {
|
||||
background-color: #007bff;
|
||||
color: white;
|
||||
border: none;
|
||||
padding: 8px 16px;
|
||||
border-radius: 4px;
|
||||
cursor: pointer;
|
||||
font-weight: 500;
|
||||
font-size: 14px;
|
||||
}
|
||||
|
||||
.editor-actions button:hover {
|
||||
background-color: #0069d9;
|
||||
}
|
||||
|
||||
#editor {
|
||||
flex: 1 1 auto;
|
||||
min-height: 300px;
|
||||
border: 1px solid #ddd;
|
||||
border-radius: 4px;
|
||||
overflow: auto;
|
||||
max-height: 60vh;
|
||||
}
|
||||
|
||||
.result-panel {
|
||||
margin-top: 20px;
|
||||
padding: 15px;
|
||||
background: #f8f9fa;
|
||||
border-radius: 4px;
|
||||
margin-bottom: 20px;
|
||||
min-height: 120px;
|
||||
overflow-y: auto;
|
||||
max-height: 30vh;
|
||||
}
|
||||
|
||||
.result-panel h3 {
|
||||
margin-top: 0;
|
||||
font-size: 16px;
|
||||
margin-bottom: 10px;
|
||||
}
|
||||
|
||||
.result-panel pre {
|
||||
background: #f6f8fa;
|
||||
padding: 12px;
|
||||
border-radius: 4px;
|
||||
overflow-x: auto;
|
||||
white-space: pre-wrap;
|
||||
font-family: 'JetBrains Mono', monospace;
|
||||
font-size: 14px;
|
||||
margin: 5px 0;
|
||||
}
|
||||
|
||||
.placeholder {
|
||||
color: #999;
|
||||
font-style: italic;
|
||||
text-align: center;
|
||||
padding: 20px;
|
||||
}
|
||||
|
||||
label {
|
||||
display: block;
|
||||
margin-bottom: 5px;
|
||||
font-size: 14px;
|
||||
color: #666;
|
||||
}
|
||||
|
||||
input[type="text"] {
|
||||
width: 100%;
|
||||
padding: 8px;
|
||||
border: 1px solid #ddd;
|
||||
border-radius: 4px;
|
||||
margin-bottom: 15px;
|
||||
font-family: 'Inter', sans-serif;
|
||||
}
|
||||
|
||||
/* Responsive adjustments */
|
||||
@media (max-width: 768px) {
|
||||
.main-container {
|
||||
flex-direction: column;
|
||||
height: auto;
|
||||
overflow-y: visible;
|
||||
}
|
||||
.problem-panel, .editor-container {
|
||||
flex: none;
|
||||
width: 100%;
|
||||
min-width: auto;
|
||||
max-height: none;
|
||||
border-right: none;
|
||||
border-bottom: 1px solid #eaeaea;
|
||||
}
|
||||
#editor {
|
||||
min-height: 400px;
|
||||
max-height: none;
|
||||
}
|
||||
.result-panel {
|
||||
max-height: none;
|
||||
}
|
||||
}
|
||||
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="main-container">
|
||||
<div class="problem-panel">
|
||||
<div class="problem-header">
|
||||
<button class="back-btn" onclick="window.location.href='/'">← Back</button>
|
||||
<h1>{{ problem.title }}</h1>
|
||||
</div>
|
||||
<div class="problem-desc">{{ problem.description | safe | markdown }}</div>
|
||||
</div>
|
||||
|
||||
<div class="editor-container">
|
||||
<div class="editor-header">
|
||||
<h2 style="margin:0;font-size:18px;">Submit Your Solution (Python)</h2>
|
||||
</div>
|
||||
<div class="editor-wrapper">
|
||||
<form method="post">
|
||||
<label for="username">Username (optional):</label>
|
||||
<input type="text" name="username" id="username" placeholder="Anonymous">
|
||||
<div id="editor"></div>
|
||||
<textarea name="user_code" id="user_code" style="display:none;"></textarea>
|
||||
<div class="editor-actions">
|
||||
<button type="submit">Run & Submit</button>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
<div class="result-panel">
|
||||
<h3>Result</h3>
|
||||
{% if result %}
|
||||
<p><b>Runtime:</b> {{ '%.4f'|format(result.runtime) }} seconds</p>
|
||||
<p><b>Output:</b></p>
|
||||
<pre>{{ result.output }}</pre>
|
||||
{% if result.error %}
|
||||
<p><b>Error:</b></p>
|
||||
<pre>{{ result.error }}</pre>
|
||||
{% endif %}
|
||||
{% else %}
|
||||
<div class="placeholder">
|
||||
Your code execution results will appear here
|
||||
</div>
|
||||
{% endif %}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script src="https://cdn.jsdelivr.net/npm/monaco-editor@0.45.0/min/vs/loader.js"></script>
|
||||
<script>
|
||||
require.config({ paths: { 'vs': 'https://cdn.jsdelivr.net/npm/monaco-editor@0.45.0/min/vs' } });
|
||||
require(['vs/editor/editor.main'], function() {
|
||||
var editor = monaco.editor.create(document.getElementById('editor'), {
|
||||
value: '',
|
||||
language: 'python',
|
||||
theme: 'vs-light',
|
||||
fontFamily: 'JetBrains Mono, monospace',
|
||||
fontLigatures: true,
|
||||
automaticLayout: true,
|
||||
fontSize: 16,
|
||||
minimap: { enabled: false }
|
||||
});
|
||||
document.querySelector('form').addEventListener('submit', function(e) {
|
||||
var code = editor.getValue();
|
||||
if (!code.trim()) {
|
||||
alert('Please enter your code before submitting.');
|
||||
e.preventDefault();
|
||||
return false;
|
||||
}
|
||||
document.getElementById('user_code').value = code;
|
||||
});
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
152
src/templates/script.js
Normal file
152
src/templates/script.js
Normal file
@@ -0,0 +1,152 @@
|
||||
// Toggle leaderboard visibility
|
||||
const toggleBtn = document.getElementById('toggleLeaderboard');
|
||||
const leaderboardSection = document.getElementById('leaderboardSection');
|
||||
const contentContainer = document.getElementById('contentContainer');
|
||||
|
||||
toggleBtn.addEventListener('click', () => {
|
||||
if (leaderboardSection.style.display === 'none') {
|
||||
leaderboardSection.style.display = '';
|
||||
toggleBtn.textContent = 'Hide';
|
||||
contentContainer.classList.remove('single-column');
|
||||
} else {
|
||||
leaderboardSection.style.display = 'none';
|
||||
toggleBtn.textContent = 'Show';
|
||||
contentContainer.classList.add('single-column');
|
||||
}
|
||||
});
|
||||
|
||||
// Problem search functionality
|
||||
const problemSearch = document.getElementById('problemSearch');
|
||||
const problemsContainer = document.getElementById('problemsContainer');
|
||||
const problemItems = problemsContainer.querySelectorAll('.problem-item');
|
||||
|
||||
problemSearch.addEventListener('input', () => {
|
||||
const searchTerm = problemSearch.value.toLowerCase();
|
||||
problemItems.forEach(item => {
|
||||
const name = item.dataset.name.toLowerCase();
|
||||
const desc = item.dataset.desc?.toLowerCase() || '';
|
||||
if (name.includes(searchTerm) || desc.includes(searchTerm)) {
|
||||
item.style.display = '';
|
||||
} else {
|
||||
item.style.display = 'none';
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// Leaderboard filtering and sorting
|
||||
const userSearch = document.getElementById('userSearch');
|
||||
const problemFilter = document.getElementById('problemFilter');
|
||||
const runtimeFilter = document.getElementById('runtimeFilter');
|
||||
const leaderboardBody = document.getElementById('leaderboardBody');
|
||||
const leaderboardRows = Array.from(leaderboardBody.querySelectorAll('tr'));
|
||||
const sortableHeaders = document.querySelectorAll('.sortable');
|
||||
|
||||
// Current sort state
|
||||
let currentSort = {
|
||||
column: null,
|
||||
direction: 'asc'
|
||||
};
|
||||
|
||||
// Filter leaderboard
|
||||
function filterLeaderboard() {
|
||||
const userTerm = userSearch.value.toLowerCase();
|
||||
const problemTerm = problemFilter.value.toLowerCase();
|
||||
const runtimeType = runtimeFilter.value;
|
||||
|
||||
leaderboardRows.forEach(row => {
|
||||
const user = row.dataset.user.toLowerCase();
|
||||
const problem = row.dataset.problem.toLowerCase();
|
||||
const runtime = parseFloat(row.dataset.runtime);
|
||||
const showUser = user.includes(userTerm);
|
||||
const showProblem = problem.includes(problemTerm);
|
||||
|
||||
let showRuntime = true;
|
||||
if (runtimeType === 'best') {
|
||||
// Find if this is the best runtime for this user+problem combo
|
||||
const userProblemRows = leaderboardRows.filter(r =>
|
||||
r.dataset.user === row.dataset.user &&
|
||||
r.dataset.problem === row.dataset.problem
|
||||
);
|
||||
const bestRuntime = Math.min(...userProblemRows.map(r => parseFloat(r.dataset.runtime)));
|
||||
showRuntime = runtime === bestRuntime;
|
||||
} else if (runtimeType === 'worst') {
|
||||
// Find if this is the worst runtime for this user+problem combo
|
||||
const userProblemRows = leaderboardRows.filter(r =>
|
||||
r.dataset.user === row.dataset.user &&
|
||||
r.dataset.problem === row.dataset.problem
|
||||
);
|
||||
const worstRuntime = Math.max(...userProblemRows.map(r => parseFloat(r.dataset.runtime)));
|
||||
showRuntime = runtime === worstRuntime;
|
||||
}
|
||||
|
||||
if (showUser && showProblem && showRuntime) {
|
||||
row.style.display = '';
|
||||
} else {
|
||||
row.style.display = 'none';
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Sort leaderboard
|
||||
function sortLeaderboard(column, direction) {
|
||||
const rows = Array.from(leaderboardBody.querySelectorAll('tr'));
|
||||
const index = Array.from(document.querySelectorAll('th')).findIndex(th => th.dataset.sort === column);
|
||||
|
||||
rows.sort((a, b) => {
|
||||
let aValue = a.cells[index].textContent;
|
||||
let bValue = b.cells[index].textContent;
|
||||
|
||||
// Special handling for numeric columns
|
||||
if (column === 'runtime' || column === 'memory' || column === 'rank') {
|
||||
aValue = parseFloat(aValue) || 0;
|
||||
bValue = parseFloat(bValue) || 0;
|
||||
return direction === 'asc' ? aValue - bValue : bValue - aValue;
|
||||
}
|
||||
|
||||
// Special handling for timestamps
|
||||
if (column === 'timestamp') {
|
||||
aValue = new Date(aValue).getTime();
|
||||
bValue = new Date(bValue).getTime();
|
||||
return direction === 'asc' ? aValue - bValue : bValue - aValue;
|
||||
}
|
||||
|
||||
// Default string comparison
|
||||
aValue = aValue.toLowerCase();
|
||||
bValue = bValue.toLowerCase();
|
||||
if (aValue < bValue) return direction === 'asc' ? -1 : 1;
|
||||
if (aValue > bValue) return direction === 'asc' ? 1 : -1;
|
||||
return 0;
|
||||
});
|
||||
|
||||
// Re-append rows in sorted order
|
||||
rows.forEach(row => leaderboardBody.appendChild(row));
|
||||
}
|
||||
|
||||
// Set up event listeners
|
||||
userSearch.addEventListener('input', filterLeaderboard);
|
||||
problemFilter.addEventListener('input', filterLeaderboard);
|
||||
runtimeFilter.addEventListener('change', filterLeaderboard);
|
||||
|
||||
// Set up sorting
|
||||
sortableHeaders.forEach(header => {
|
||||
header.addEventListener('click', () => {
|
||||
const column = header.dataset.sort;
|
||||
|
||||
// Reset all sort indicators
|
||||
sortableHeaders.forEach(h => {
|
||||
h.classList.remove('sort-asc', 'sort-desc');
|
||||
});
|
||||
|
||||
// Determine new sort direction
|
||||
if (currentSort.column === column) {
|
||||
currentSort.direction = currentSort.direction === 'asc' ? 'desc' : 'asc';
|
||||
} else {
|
||||
currentSort.column = column;
|
||||
currentSort.direction = 'asc';
|
||||
}
|
||||
|
||||
// Apply new sort
|
||||
header.classList.add(`sort-${currentSort.direction}`);
|
||||
sortLeaderboard(column, currentSort.direction);
|
||||
});
|
||||
});
|
||||
99
src/utils.py
Normal file
99
src/utils.py
Normal file
@@ -0,0 +1,99 @@
|
||||
import sys
|
||||
import traceback
|
||||
import time
|
||||
import io
|
||||
import tempfile
|
||||
import subprocess
|
||||
import os
|
||||
|
||||
def run_code_against_tests(user_code, test_code):
|
||||
local_ns = {}
|
||||
output = ''
|
||||
start = time.perf_counter()
|
||||
error = None
|
||||
passed = False
|
||||
temp_file = None
|
||||
|
||||
try:
|
||||
# Check if unittest is used in test_code
|
||||
if 'unittest' in test_code:
|
||||
# Write user code + test code to a temp file
|
||||
with tempfile.NamedTemporaryFile('w+', suffix='.py', delete=False, encoding='utf-8') as f:
|
||||
combined_code = f"{user_code}\n\n{test_code}"
|
||||
f.write(combined_code)
|
||||
f.flush()
|
||||
temp_file = f.name
|
||||
|
||||
# Run the file as a subprocess
|
||||
try:
|
||||
proc = subprocess.run(
|
||||
[sys.executable, temp_file],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
timeout=10,
|
||||
encoding='utf-8'
|
||||
)
|
||||
output = proc.stdout
|
||||
if proc.stderr:
|
||||
output += f"\n{proc.stderr}"
|
||||
|
||||
passed = proc.returncode == 0
|
||||
if not passed:
|
||||
error = f"Tests failed. Return code: {proc.returncode}\n{output}"
|
||||
else:
|
||||
# For successful unittest runs, the stderr contains the test results
|
||||
if proc.stderr and "OK" in proc.stderr:
|
||||
output = proc.stderr # Use stderr as the main output for unittest
|
||||
|
||||
except subprocess.TimeoutExpired:
|
||||
passed = False
|
||||
error = "Code execution timed out after 10 seconds"
|
||||
output = "Execution timed out"
|
||||
|
||||
else:
|
||||
# Capture stdout
|
||||
old_stdout = sys.stdout
|
||||
captured_output = io.StringIO()
|
||||
sys.stdout = captured_output
|
||||
|
||||
try:
|
||||
# Execute user code
|
||||
exec(user_code, {}, local_ns)
|
||||
# Execute test code (should raise AssertionError if fail)
|
||||
exec(test_code, local_ns, local_ns)
|
||||
passed = True
|
||||
|
||||
except AssertionError as e:
|
||||
passed = False
|
||||
error = f"Assertion failed: {str(e)}"
|
||||
|
||||
except Exception as e:
|
||||
passed = False
|
||||
error = f"Runtime error: {traceback.format_exc()}"
|
||||
|
||||
finally:
|
||||
output = captured_output.getvalue()
|
||||
sys.stdout = old_stdout
|
||||
|
||||
except Exception as e:
|
||||
passed = False
|
||||
error = f"Execution error: {traceback.format_exc()}"
|
||||
|
||||
finally:
|
||||
# Clean up temporary file
|
||||
if temp_file and os.path.exists(temp_file):
|
||||
try:
|
||||
os.unlink(temp_file)
|
||||
except Exception as e:
|
||||
print(f"Warning: Could not delete temp file {temp_file}: {e}")
|
||||
|
||||
runtime = time.perf_counter() - start
|
||||
|
||||
result = {
|
||||
'passed': passed,
|
||||
'output': output.strip() if output else '',
|
||||
'runtime': runtime,
|
||||
'error': error if not passed else None
|
||||
}
|
||||
|
||||
return result
|
||||
Reference in New Issue
Block a user