From c7c1b8ecd690c1af699e21b5d52254e494f3f028 Mon Sep 17 00:00:00 2001 From: rattatwinko Date: Tue, 12 Aug 2025 18:56:52 +0200 Subject: [PATCH] some more info and some difficulty added! --- app.py | 13 ++++-- problem_scanner.py | 18 +++++--- problems/fibonacisequence/manifest.json | 1 + problems/reversedstring/manifest.json | 1 + problems/sortlist/manifets.json | 1 + readme.md | 56 +++++++++++++++++++------ static/index.css | 29 ++++++++++++- templates/index.html | 40 +++++++++--------- 8 files changed, 116 insertions(+), 43 deletions(-) diff --git a/app.py b/app.py index 7676b6e..6a51732 100644 --- a/app.py +++ b/app.py @@ -41,13 +41,14 @@ def index(): db_path = Path(__file__).parent / 'problems.sqlite3' conn = sqlite3.connect(db_path) c = conn.cursor() - c.execute('SELECT folder, description, test_code FROM problems') + # + 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} + 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']) @@ -67,17 +68,21 @@ def view_problem(folder): db_path = Path(__file__).parent / 'problems.sqlite3' conn = sqlite3.connect(db_path) c = conn.cursor() - c.execute('SELECT folder, description, test_code FROM problems WHERE folder = ?', (folder,)) + 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], - 'test_code': row[2], + 'difficulty': row[2], + 'test_code': row[3], # This is used internally, not displayed 'title': row[0].replace('_', ' ').title() } + result = None if request.method == 'POST': user_code = request.form['user_code'] diff --git a/problem_scanner.py b/problem_scanner.py index 4c6460d..e3b7dff 100644 --- a/problem_scanner.py +++ b/problem_scanner.py @@ -35,6 +35,7 @@ class ProblemScannerThread(threading.Thread): id INTEGER PRIMARY KEY AUTOINCREMENT, folder TEXT, description TEXT, + difficulty TEXT, test_code TEXT )''') conn.commit() @@ -65,13 +66,18 @@ class ProblemScannerThread(threading.Thread): 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 + 'test_code': test_code, + 'difficulty': difficulty }) - print(f"Found problem: {folder.name}") + print(f"Found problem: {folder.name} ; Difficulty: {difficulty}") except Exception as e: print(f"Error reading problem files for {folder.name}: {e}") else: @@ -99,8 +105,10 @@ class ProblemScannerThread(threading.Thread): # Insert new problems for p in problems: - c.execute('INSERT INTO problems (folder, description, test_code) VALUES (?, ?, ?)', - (p['folder'], p['description'], p['test_code'])) + 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") diff --git a/problems/fibonacisequence/manifest.json b/problems/fibonacisequence/manifest.json index af6b31f..8779762 100644 --- a/problems/fibonacisequence/manifest.json +++ b/problems/fibonacisequence/manifest.json @@ -2,5 +2,6 @@ "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" } \ No newline at end of file diff --git a/problems/reversedstring/manifest.json b/problems/reversedstring/manifest.json index 4cd8985..d3d47d7 100644 --- a/problems/reversedstring/manifest.json +++ b/problems/reversedstring/manifest.json @@ -2,5 +2,6 @@ "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" } \ No newline at end of file diff --git a/problems/sortlist/manifets.json b/problems/sortlist/manifets.json index b10280c..a8920fb 100644 --- a/problems/sortlist/manifets.json +++ b/problems/sortlist/manifets.json @@ -2,5 +2,6 @@ "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" } \ No newline at end of file diff --git a/readme.md b/readme.md index 3f24359..3e0976f 100644 --- a/readme.md +++ b/readme.md @@ -6,23 +6,55 @@ but more lightweight if you want to contribute write tests like this: +### FileStructure: + +In /problems/ create a folder named after the problem. + +In this folder create ```manifest.json, test.py, description.md``` + +**Manifest.JSON needs to exsist and _needs_ to look like this:** +```json +{ + "title": "Title of the Problem", + "description": "Write a very short description here", + "description_md": "problems/problempath/description.md", + "difficulty": "easy || medium || hard", + "test_code": "problems/problempath/test.py" +} +``` +I do know it might be a bit tedious but this is required and its the easiest way. + +#### After you've decided on how you would name / write your Test write it like this: + + - It is important to note that you _CAN_ write the Code the User is expected to write firstly. **BUT** after writing the UnitTest and it passing, comment out the written code. + +It is supposed to look something like this (/sortlist/): + ```python +""" +@TESTSAMPLE.PY / NAME THIS "test.py" in your actual project +""" import unittest -# -# This is importantly commented out. The UnitTest tests this if uncommented. -# This is only here for reference -# -# def revstring(x): -# return x[::-1] +" )) First Point from the List " +# def sortlist(lst = [4,3,2,1]) -> list: +# return sorted(lst) -# +")) This is a 'easy' Test, if you want you can write more defined ones." class TestSolution(unittest.TestCase): - def test_simple(self): - # !! This needs to be dynamic ; if the user enters some shit then it is supposed to work too - x=""; - self.assertEqual(revstring(x), x[::-1]) + def test_sort(self): + self.x = [] + self.assertEqual(sortlist(self.x), sorted(self.x)) # pyright: ignore[reportUndefinedVariable] <- This is only here so that pyright doesnt complain ; NOT NECCESARY! if __name__ == "__main__": unittest.main() -``` \ No newline at end of file +``` +#### Writing the description: + +**Please** by _God_ write simple and easy to understand terms. If you write like Einstein noone is going to understand you. + +- Syntax: + - Normal Markdown. + - Start with "##" instead of "#" ; "##" looks better + - Use CrossLinks ( something like [W3](https://www.w3schools.com/), or the [PyDocs](https://docs.python.org/3/)) + - Good Formatting is always appreciated diff --git a/static/index.css b/static/index.css index 5e1af31..96f1dbb 100644 --- a/static/index.css +++ b/static/index.css @@ -73,6 +73,9 @@ header p { .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; @@ -82,6 +85,28 @@ header p { 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; @@ -155,6 +180,6 @@ header p { @media (max-width: 800px) { .content { grid-template-columns: 1fr; } .leaderboard-controls { - flex-direction: column; + flex-direction: column; } -} \ No newline at end of file +} diff --git a/templates/index.html b/templates/index.html index cb2df4b..7d241c2 100644 --- a/templates/index.html +++ b/templates/index.html @@ -14,29 +14,29 @@

Quick Problem Platform

-
-
-
- +
+
+ +
+

Problems

+
+ {% for folder, description, test_code, difficulty in problems %} + -

Problems

-
- {% for folder, description, test_code in problems %} - - {% else %} -
No problems yet.
- {% endfor %} -
-
+ {% else %} +
No problems yet.
+ {% endfor %} +
+