Skip to content

Instantly share code, notes, and snippets.

View FerdinaKusumah's full-sized avatar
🧑‍🚀
✌🏻

Ferdina Kusumah FerdinaKusumah

🧑‍🚀
✌🏻
View GitHub Profile
@FerdinaKusumah
FerdinaKusumah / double_leading_python.py
Last active December 21, 2019 16:39
Underscore Python
class Sample():
def __init__(self):
self.a = 1
self._b = 2
self.__c = 3
>>> t = Sample()
>>> dir(t)
@FerdinaKusumah
FerdinaKusumah / trailing_python.py
Created December 21, 2019 15:47
Underscore Python
>>> def function(class):
File "<stdin>", line 1
def function(class):
^
SyntaxError: invalid syntax
>>> def function(class_):
... pass
...
>>>
>>> import example_module
>>> example_module.external_func()
23
>>> example_module._internal_func()
42
@FerdinaKusumah
FerdinaKusumah / internal_method.py
Created December 21, 2019 15:32
Underscore Python
# This is example_module.py:
def external_func():
return 23
def _internal_func():
return 42
>>> from example_module import *
class Example:
def __init__(self):
self.foo = 21
self._bar = 25
>>> t = Example()
>>> t.foo
21
>>> t._bar
@FerdinaKusumah
FerdinaKusumah / non_deterministic_function.py
Created December 16, 2019 02:00
Non deterministic function
def nondeterministic_function(a, b):
if a % b == 0:
return "Even number"
return "Odd number"
@FerdinaKusumah
FerdinaKusumah / deterministic_function.py
Created December 16, 2019 01:57
Deterministic function
def deterministic_function(a, b):
return a + b
@FerdinaKusumah
FerdinaKusumah / memoization_lru_cache.py
Created December 16, 2019 01:50
Memoization Fibonacci Lru cache
import timeit
from functools import lru_cache
@lru_cache(maxsize=256)
def fibonacci_lru_cache(n: int):
if n == 0:
return 0
elif n == 1:
return 1
@FerdinaKusumah
FerdinaKusumah / memoization_fibonacci.py
Last active December 16, 2019 01:49
Memoization fibonacci
import timeit
from functools import wraps
def memoize(func):
cache = dict()
@wraps(func)
def to_cache(*args):
if args in cache:
return cache[args]
@FerdinaKusumah
FerdinaKusumah / fibonacci.py
Last active December 16, 2019 01:38
Fibonacci
def fibonacci(n: int):
""" Define function fibonacci with recursion"""
if n == 0:
return 0
elif n == 1:
return 1
return fibonacci(n-1) + fibonacci(n-2)
N = 35
fib = fibonacci(N)