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 fib(n): | |
if n in (0,1): | |
return n | |
else: | |
return fib(n-1) + fib(n-2) | |
def memoize(n): | |
cache = {} | |
def helper(x): | |
if x not in cache: |
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 time | |
bound,mem = 1000000,{} | |
def collatz(n): | |
if n==1: return [1] | |
else: return [n] + collatz(n/2 if (n%2==0) else 3*n+1) if (n>1) else [1] | |
def collatzCount_recursive_memoized(n): | |
if n not in mem: mem[n]= 1 + collatzCount_recursive_memoized(n/2 if (n%2==0) else 3*n+1) if (n>1) else 1 | |
return mem[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 time | |
import itertools | |
import hashlib | |
def regular_for_loop(bound): | |
_md5 = hashlib.md5() | |
for i in itertools.repeat("foo", bound): | |
_md5.update(i) | |
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 math | |
def isAbundant(n,f): #make this run fast | |
div = f(n) | |
return True if sum(div)>n else False | |
def factors_for_loop_plus_operator(n): | |
# bug, feed this a perfect square and the square root | |
# will be counted twice | |
div = [1] |
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 time import time | |
def test(iterable): | |
start = time() | |
for i in range(scale): | |
if i in iterable: | |
a = 90.0 + 6000 | |
return time()-start | |
def aggregate(count,f,a): |
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 time import time | |
from math import sqrt | |
def disproves(n): | |
i=2 | |
while i*i*2<n: | |
diff = n-i*i*2 | |
if not any(diff%j==0 for j in xrange(3,int(sqrt(diff))+1,2)) :return False | |
i+=1 | |
return True | |
n=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
Tynt = window.Tynt || []; | |
if (typeof Tynt.TIL == "undefined") { | |
(function() { | |
var Ja = function() { | |
var h = document, | |
l = h.body, | |
p = h.documentElement, | |
aa = eval("/*@cc_on!@*/false"), | |
ba = function(a, b) { |
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
*.swp |
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 sqrt | |
import random,time | |
import profile | |
random.seed(1) | |
def bellman_ford(edges): | |
numRows = len(edges) | |
numCols = len(edges[0]) |
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
sleuth = 7+1+5 | |
target = 37 | |
simple_sum = target - sleuth # 24 | |
from itertools import combinations | |
seed = [2 for n in range(7)] + [3 for n in range(7)] + [5 for n in range(7)] | |
answers = set(a for a in combinations(seed,7) if sum(a) == simple_sum) |
OlderNewer