refactored the prb page and added fib seqq

This commit is contained in:
2025-08-12 16:37:35 +02:00
parent a03f9ddb14
commit 0bffdf612c
4 changed files with 357 additions and 29 deletions

View 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 problems solution description.

View File

@@ -0,0 +1,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",
"test_code": "problems/fibonacisequence/test.py"
}

View 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))
"""

View File

@@ -3,28 +3,270 @@
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{{ problem.title }}</title>
<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="problem-header">
<button class="back-btn" onclick="window.location.href='/'">← Back</button>
<h1 style="margin-bottom:0;">{{ problem.title }}</h1>
</div>
<div class="problem-desc">{{ problem.description | safe | markdown }}</div>
<div class="editor-section" style="max-width:1160;margin:0">
<h2 style="margin-top:0;">Submit Your Solution (Python)</h2>
<form method="post">
<label for="username">Username (optional):</label>
<input type="text" name="username" id="username" placeholder="Anonymous" style="margin-bottom:10px;">
<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 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>
</form>
<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' } });
@@ -50,17 +292,5 @@
});
});
</script>
{% if result %}
<div class="editor-section" style="max-width:1160;margin:0;margin-top: 5px;">
<h3>Result:</h3>
<b>Runtime:</b> {{ '%.4f'|format(result.runtime) }} seconds<br>
<b>Output:</b>
<pre>{{ result.output }}</pre>
{% if result.error %}
<b>Error:</b>
<pre>{{ result.error }}</pre>
{% endif %}
</div>
{% endif %}
</body>
</html>
</html>