Skip to content

Instantly share code, notes, and snippets.

View Scinawa's full-sized avatar
🎯
Focusing

Alessandro Luongo Scinawa

🎯
Focusing
View GitHub Profile
from datetime import datetime
def with_list(iterable):
t0 = datetime.now()
first, rest = iterable[0], iterable[1:]
t1 = datetime.now()
return (t1-t0).total_seconds()
def with_extended(iterable):
sync && echo 3 > /proc/sys/vm/drop_caches
sysctl -w vm.drop_caches=3
@Scinawa
Scinawa / fun_n_x.py
Created March 24, 2016 04:19
function from N to X
>>> x = len("ABCDEF") # x= 6,
>>> n=3
>>> for i in product("ABCDEF", repeat=n):
... print(i)
('A', 'A', 'A')
...
('F', 'F', 'F')
>>> len( [i for i in product("ABCDEF", repeat=n)] ) == x**n
@Scinawa
Scinawa / inj_fun_n_x.py
Created March 24, 2016 04:23
Injective functions from N to X
>>> s="ABCD"
>>> repetition=3
>>> a = [i for i in itertools.permutations(s, repetition)]
>>> len(a) == math.factorial(len(s)) / math.factorial(len(s)-repetition)
True
def sample_function(n=0, s=set() ):
f = {}
for i in range(0, n):
@Scinawa
Scinawa / inj_fun_n_x_perm_n.py
Last active March 24, 2016 16:17
Injective functions from N to X, up to a permutation of N
import itertools
s="ABCD"
rep=3
a = [i for i in itertools.combinations(s, rep)]
len(a) == math.factorial(len(s)) / (math.factorial(rep) * math.factorial(len(s)-rep))
@Scinawa
Scinawa / stirling_1.py
Created March 24, 2016 05:23
Stirling number of the first kind
@memo # can be memoized, otherwise comment this line
def stirling1(n,k):
"""Returns the stirling number of the first kind using recursion.."""
if n == 0 and k == 0:
return 1
if k == 0 and n >= 1:
return 0
if k > n:
return 0
return stirling1(n-1, k-1) - (n - 1) * stirling1(n-1, k)
@Scinawa
Scinawa / function_n_x_up_n.py
Last active March 24, 2016 06:16
Functions from N to X, up to a permutation of N
from math import factorial
x=10
n=3
res = math.factorial(x+n-1) / ( math.factorial(n) * math.factorial(x+n-1-n))
>>> len([i for i in itertools.combinations_with_replacement(range(0,x), n)]) == res
True
@Scinawa
Scinawa / binomial.py
Created March 25, 2016 22:00
Calculate binomial x chose n
#!/usr/bin/env python
def i4_choose ( x, n ):
#*****************************************************************************
#
## I4_CHOOSE computes the binomial coefficient C(x,n) as an I4.
#
# Discussion:
#
@Scinawa
Scinawa / comp_enum.py
Created March 25, 2016 22:05
compositions of the integer x into n parts.
#!/usr/bin/env python
def comp_enum ( x, n ):
#******************************************************************************/
#
## COMP_ENUM returns the number of compositions of the integer x into n parts.
#
# Discussion:
#
@Scinawa
Scinawa / stirling_second.py
Created March 25, 2016 22:56
stirling number of the second kind
def stirling_second(x,n):
if n <= 1 or x == n: return 1
if n > x or x <= 0: return 0
return stirling_second(x-1, n-1) + n * stirling_second(x-1, n)