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