This file contains 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
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): |
This file contains 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
sync && echo 3 > /proc/sys/vm/drop_caches | |
sysctl -w vm.drop_caches=3 |
This file contains 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
>>> 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 |
This file contains 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
>>> 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): |
This file contains 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
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)) |
This file contains 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 # 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) |
This file contains 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
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 |
This file contains 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
#!/usr/bin/env python | |
def i4_choose ( x, n ): | |
#***************************************************************************** | |
# | |
## I4_CHOOSE computes the binomial coefficient C(x,n) as an I4. | |
# | |
# Discussion: | |
# |
This file contains 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
#!/usr/bin/env python | |
def comp_enum ( x, n ): | |
#******************************************************************************/ | |
# | |
## COMP_ENUM returns the number of compositions of the integer x into n parts. | |
# | |
# Discussion: | |
# |
This file contains 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 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) |
OlderNewer