The domain of the Fibonacci sequence is the non-negative integers. Test for invalid inputs and for performance.
def fibonacci(n): if type(n) is not int or n < 0: return None def _fibonacci(n, a=0, b=1): if not n: return 0 if n == 1: return b else: return _fibonacci(n-1, b, a+b) return _fibonacci(n)
def fibonacci(n, a=0, b=1):if n == 0: return 0if n == 1: return belse:return fibonacci(n-1, b, a+b)- def fibonacci(n):
- if type(n) is not int or n < 0:
- return None
- def _fibonacci(n, a=0, b=1):
- if not n: return 0
- if n == 1: return b
- else:
- return _fibonacci(n-1, b, a+b)
- return _fibonacci(n)
# TODO: Replace examples and use TDD development by writing your own tests # These are some of the methods available: # test.expect(boolean, [optional] message) test.assert_equals(fibonacci(8), 21) test.assert_equals(fibonacci(20), 6765) test.assert_equals(fibonacci(25), 75025) test.assert_equals(fibonacci(36), 14930352) test.assert_equals(fibonacci(100), 354224848179261915075) test.assert_equals(fibonacci(500), 139423224561697880139724382870407283950070256587697307264108962948325571622863290691557658876222521294125) test.assert_equals(fibonacci('a string'), None) test.assert_equals(fibonacci(1.5), None) test.assert_equals(fibonacci(-1), None) test.assert_equals(fibonacci(600), 110433070572952242346432246767718285942590237357555606380008891875277701705731473925618404421867819924194229142447517901959200) test.assert_equals(fibonacci(900), 54877108839480000051413673948383714443800519309123592724494953427039811201064341234954387521525390615504949092187441218246679104731442473022013980160407007017175697317900483275246652938800) test.assert_equals(fibonacci(997), 10261062362033262336604926729245222132668558120602124277764622905699407982546711488272859468887457959087733119242564077850743657661180827326798539177758919828135114407499369796465649524266755391104990099120377) import time time.sleep(10) test.assert_equals(fibonacci(0), 0) test.assert_equals(fibonacci(1), 1) # test.assert_not_equals(actual, expected, [optional] message) # You can use Test.describe and Test.it to write BDD style test groupings
- # TODO: Replace examples and use TDD development by writing your own tests
- # These are some of the methods available:
- # test.expect(boolean, [optional] message)
- test.assert_equals(fibonacci(8), 21)
- test.assert_equals(fibonacci(20), 6765)
- test.assert_equals(fibonacci(25), 75025)
- test.assert_equals(fibonacci(36), 14930352)
- test.assert_equals(fibonacci(100), 354224848179261915075)
- test.assert_equals(fibonacci(500), 139423224561697880139724382870407283950070256587697307264108962948325571622863290691557658876222521294125)
- test.assert_equals(fibonacci('a string'), None)
- test.assert_equals(fibonacci(1.5), None)
- test.assert_equals(fibonacci(-1), None)
- test.assert_equals(fibonacci(600), 110433070572952242346432246767718285942590237357555606380008891875277701705731473925618404421867819924194229142447517901959200)
- test.assert_equals(fibonacci(900), 54877108839480000051413673948383714443800519309123592724494953427039811201064341234954387521525390615504949092187441218246679104731442473022013980160407007017175697317900483275246652938800)
- test.assert_equals(fibonacci(997), 10261062362033262336604926729245222132668558120602124277764622905699407982546711488272859468887457959087733119242564077850743657661180827326798539177758919828135114407499369796465649524266755391104990099120377)
- import time
- time.sleep(10)
- test.assert_equals(fibonacci(0), 0)
- test.assert_equals(fibonacci(1), 1)
- # test.assert_not_equals(actual, expected, [optional] message)
- # You can use Test.describe and Test.it to write BDD style test groupings