Skip to content

Instantly share code, notes, and snippets.

@raeq
raeq / fibonacci.py
Created September 5, 2020 10:21
Golden Ration method to calculate Fibonacci Numbers
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:
@raeq
raeq / fibonacci.py
Created September 5, 2020 10:13
Double matrix method for calculating Fibonacci
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
@raeq
raeq / fibonacci.py
Created September 5, 2020 10:09
Memoized recursion
@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.
@raeq
raeq / fibonacci.py
Created September 5, 2020 10:03
Memoization and recursive functions
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.
@raeq
raeq / fibonacci.py
Created September 5, 2020 09:54
Recursive method to find fibonacci numbers
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:
@raeq
raeq / fibonacci.py
Created September 5, 2020 08:19
Iterative solution for calculating a member of the Fibonacci sequence.
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
@raeq
raeq / slicing.py
Created August 31, 2020 12:24
Slice Object
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])
@raeq
raeq / slicing.py
Created August 31, 2020 12:24
Slice Object
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])
@raeq
raeq / slicing.py
Created August 31, 2020 12:22
Slice objects
hello: str = 'Hello World!'
sl: slice = slice(-2, 5, -1)
print(sl.start)
print(sl.stop)
print(sl.step)
print(hello[sl])
@raeq
raeq / slicing.py
Created August 31, 2020 12:19
Using a slice object.
hello: str = 'Hello World!'
sl: slice = slice(-2, 5, -1)
assert hello[sl] == 'dlroW'