This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def fibonacci_goldenratio(target): | |
""" | |
This method works by calculating the golden ratio, and then | |
raising the golden ration to the power of n, whereby n is the position of the fibonacci sequence. | |
As we look for higher and higher values of n, rounding errors throw us off. This method works for values of n | |
less than 72 or so. | |
:param target: | |
:return: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def matrix_fibonacci(target: int) -> int: | |
""" | |
This is the fastest way to compute a fibonacci number, using matrix mathematics. | |
It completes quickly. | |
The math is explained in detailed on the Wiki page at: https://en.wikipedia.org/wiki/Fibonacci_number#Matrix_form | |
>>> matrix_fibonacci(80) | |
23416728348467685 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@memo | |
def fibonacci_wrapped(i: int = None) -> int: | |
""" | |
This technique uses exactly the same recursive algorithm as in the naive solution. | |
However, we are now using a technique known as "memoization". This is really useful in some | |
recursive algorithms, because otherwise the same set of values is constantly being calculated. | |
Here we use a nice and simple decorator to add the memoization capability. | |
However, we're still using recursion, and at some point the maximum recursion depth will be reached. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def memo(func): | |
""" | |
An @memo decorator for functions and methods. | |
It defines an internal cache. The key is value of the parameter used in the wrapped function. | |
Say we call a fibonacci generator with a value of 30. The result is stored in the cache: | |
cache[30] = 1346269 | |
When we call the fibonacci generator with 30, we can just get the value for 30 out of the cache instead | |
of calculating it all again. Pretty goddamn sweet. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def fibonacci_recursive(i: int = None) -> int: | |
""" | |
The simplest way to calculate a fibonacci number, using recursion. | |
Simple works, but not in all cases! | |
>>> fibonacci_recursive(20) | |
6765 | |
>>> # fibonacci_recursive(100) No, don't do this, it will take forever and exhaust your computer's memory | |
""" | |
if i == 0: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def fibonacci_iterative(target: int) -> int: | |
""" | |
This version uses iteration to start at the beginning of the sequence and work up to the target value. | |
It is in this way similar to fibonacci_bottomup | |
Luckily, the iterative version doesn't have the memory space issues, there are no deeply nested call chains here. | |
Unfortunately this method is slow, very slow. | |
>>> fibonacci_iterative(80) | |
23416728348467685 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
hello: str = 'Hello World!' | |
sl: slice = slice(-2, 5, -1) | |
print(f'Start: {sl.start}') | |
print(f'Stop: {sl.stop}') | |
print(f'Step: {sl.step}') | |
print(f'Slice: {sl}') | |
print(hello[sl]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
hello: str = 'Hello World!' | |
sl: slice = slice(-2, 5, -1) | |
print(f'Start: {sl.start}') | |
print(f'Stop: {sl.stop}') | |
print(f'Start: {sl.start}') | |
print(f'Step: {sl.step}') | |
print(f'Slice: {sl}') | |
print(hello[sl]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
hello: str = 'Hello World!' | |
sl: slice = slice(-2, 5, -1) | |
print(sl.start) | |
print(sl.stop) | |
print(sl.step) | |
print(hello[sl]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
hello: str = 'Hello World!' | |
sl: slice = slice(-2, 5, -1) | |
assert hello[sl] == 'dlroW' |