From 04dc638cf0f3e8548f5b2a5580a3ef0a68c44cf0 Mon Sep 17 00:00:00 2001 From: rattatwinko Date: Wed, 13 Aug 2025 22:56:06 +0200 Subject: [PATCH] if the user submitted solution is shit wrong then fucking dont add to the shit database. also removed unnccessary fucking function --- src/app.py | 9 +++-- src/problem_loader.py | 22 ------------ src/problems/ReversedList/description.md | 44 ++++++++++++++++++++++++ src/problems/ReversedList/manifest.json | 7 ++++ src/problems/ReversedList/test.py | 27 +++++++++++++++ 5 files changed, 85 insertions(+), 24 deletions(-) create mode 100644 src/problems/ReversedList/description.md create mode 100644 src/problems/ReversedList/manifest.json create mode 100644 src/problems/ReversedList/test.py diff --git a/src/app.py b/src/app.py index 2541c77..05d9c5d 100644 --- a/src/app.py +++ b/src/app.py @@ -85,7 +85,6 @@ def view_problem(folder): 'test_code': row[2], # now correct } - result = None if request.method == 'POST': user_code = request.form['user_code'] @@ -96,6 +95,7 @@ def view_problem(folder): 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: @@ -109,6 +109,7 @@ def view_problem(folder): 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'] @@ -119,7 +120,11 @@ def view_problem(folder): line_number = tb_lines[-1].lineno except Exception: pass - log_leaderboard(username, problem['folder'], run_result['runtime'], memory_used, line_number) + + # ONLY log to leaderboard if the solution passed all tests + if run_result['passed']: + log_leaderboard(username, problem['folder'], run_result['runtime'], memory_used, line_number) + result = run_result return render_template('problem.html', problem=problem, result=result) diff --git a/src/problem_loader.py b/src/problem_loader.py index 09d5d51..5c85a43 100644 --- a/src/problem_loader.py +++ b/src/problem_loader.py @@ -5,28 +5,6 @@ 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: diff --git a/src/problems/ReversedList/description.md b/src/problems/ReversedList/description.md new file mode 100644 index 0000000..e701145 --- /dev/null +++ b/src/problems/ReversedList/description.md @@ -0,0 +1,44 @@ +## Reverse a List + +Write a function called `reverse_list` that takes a list as input and returns the list in reverse order. +You are **not allowed** to just use Python’s built-in `.reverse()` method or slicing (`[::-1]`) — try to reverse it manually for practice. + +### Function Signature: + +```python +def reverse_list(lst): + # your code here +``` + +### Requirements + +* The function should return a new list with the elements in reversed order. +* The input list can contain: + + * Numbers + * Strings + * Booleans + * A mix of different types +* Your function will be tested with: + + * A small list (e.g., `[1, 2, 3]` → `[3, 2, 1]`) + * A longer list (e.g., `[1, 2, 3, 4]` → `[4, 3, 2, 1]`) + * An empty list (should return an empty list) + * A single-element list (should return the same list) + * A mixed-type list (e.g., `[1, 'a', True]` → `[True, 'a', 1]`) + +### Example + +```python +reverse_list([1, 2, 3]) +# Output: [3, 2, 1] + +reverse_list([]) +# Output: [] + +reverse_list([5]) +# Output: [5] + +reverse_list([1, 'a', True]) +# Output: [True, 'a', 1] +``` \ No newline at end of file diff --git a/src/problems/ReversedList/manifest.json b/src/problems/ReversedList/manifest.json new file mode 100644 index 0000000..e13e95a --- /dev/null +++ b/src/problems/ReversedList/manifest.json @@ -0,0 +1,7 @@ +{ + "title": "Reversed List", + "description": "Given a list, return a new list with the elements in reverse order.", + "description_md": "problems/reversedlist/description.md", + "difficulty": "easy", + "test_code": "problems/reversedlist/test.py" +} \ No newline at end of file diff --git a/src/problems/ReversedList/test.py b/src/problems/ReversedList/test.py new file mode 100644 index 0000000..6b0ace5 --- /dev/null +++ b/src/problems/ReversedList/test.py @@ -0,0 +1,27 @@ +import unittest + +#def reverse_list(lst : list) -> list: + #return lst[::-1] + +class TestSolution(unittest.TestCase): + def test_simple(self): + test_cases = [ + ([1, 2, 3], [3, 2, 1]), # Simple case + ([1, 2, 3, 4], [4, 3, 2, 1]), # Longer list + ([], []), # Empty list + ([5], [5]), # Single element list + ([1, 'a', True], [True, 'a', 1]) # Mixed types + ] + print("\n FUNCTION OUTPUT TEST RESULTS") + + for input_val , expected in test_cases: + try: + actual = reverse_list(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)