Recursion
Mathematics
def fibonacci(n): return n if n <= 1 else fibonacci(n - 1) + fibonacci(n - 2)
function fibonacci(n) {if (n <= 1) return n;return fibonacci(n - 1) + fibonacci(n - 2);}- def fibonacci(n):
- return n if n <= 1 else fibonacci(n - 1) + fibonacci(n - 2)
import unittest from solution import fibonacci class TestFibonacci(unittest.TestCase): """Test suite for the `fibonacci` method.""" def test_zero(self): """Zero value test. Raises: AssertionError: if `fibonacci(0)` returns other than 0. """ self.assertEquals(fibonacci(0), 0) def test_negative_one(self) -> None: """Negative one value test. Raises: AssertionError: if `fibonacci(-1)` returns other than -1. """ self.assertEquals(fibonacci(-1), -1) def test_one(self) -> None: """Positive one value test. Raises: AssertionError: if `fibonacci(1)` returns other than 1. """ self.assertEquals(fibonacci(1), 1) def test_first_9(self) -> None: """First 9 values test. Raises: AssertionError: if any of the first 9 integer digits from 0 returns an unexpected value. """ seq = (0, 1, 1, 2, 3, 5, 8, 13, 21, 34) for n, ret in zip(range(0, 10), seq): self.assertEquals(fibonacci(n), ret)
const { expect } = require('chai');- import unittest
- from solution import fibonacci
describe('fibonacci', function() {it('should return 0 for input 0', function() {expect(fibonacci(0)).to.equal(0);});it('should return 1 for input 1', function() {expect(fibonacci(1)).to.equal(1);});it('should return 1 for input 2', function() {expect(fibonacci(2)).to.equal(1);});it('should return 2 for input 3', function() {expect(fibonacci(3)).to.equal(2);});it('should return 55 for input 10', function() {expect(fibonacci(10)).to.equal(55);});});- class TestFibonacci(unittest.TestCase):
- """Test suite for the `fibonacci` method."""
- def test_zero(self):
- """Zero value test.
- Raises:
- AssertionError: if `fibonacci(0)` returns other than 0.
- """
- self.assertEquals(fibonacci(0), 0)
- def test_negative_one(self) -> None:
- """Negative one value test.
- Raises:
- AssertionError: if `fibonacci(-1)` returns other than -1.
- """
- self.assertEquals(fibonacci(-1), -1)
- def test_one(self) -> None:
- """Positive one value test.
- Raises:
- AssertionError: if `fibonacci(1)` returns other than 1.
- """
- self.assertEquals(fibonacci(1), 1)
- def test_first_9(self) -> None:
- """First 9 values test.
- Raises:
- AssertionError: if any of the first 9 integer digits from 0
- returns an unexpected value.
- """
- seq = (0, 1, 1, 2, 3, 5, 8, 13, 21, 34)
- for n, ret in zip(range(0, 10), seq):
- self.assertEquals(fibonacci(n), ret)