This file contains hidden or 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 parse_flags(): | |
from optparse import OptionParser | |
usage = "usage: %prog [options]" | |
parser = OptionParser(usage=usage) | |
parser.add_option( | |
"-v", "--verbose", dest="verbose", | |
help="set to True to display additional output from the simulation", | |
action="store_true", default=False) | |
parser.add_option( | |
"--num_dummies", dest="num_dummies", |
This file contains hidden or 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 has_directed_cycle(graph): | |
for node in graph.iterkeys(): | |
frontline = [node] | |
was = set([node]) | |
while frontline: | |
curr = frontline.pop() | |
if curr in was: | |
continue | |
was.add(curr) | |
for neighbor in graph[curr]: |
This file contains hidden or 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 fractions import Fraction as F, gcd | |
from os import environ as env | |
__all__ = ["greedy_solve"] | |
def _find_next(t, c): | |
p = c | |
zero = F(0, 1) | |
while t - F(1, c) < zero: |
This file contains hidden or 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 fractions import Fraction as F | |
class NotSolved(Exception): | |
pass | |
def odd_solve(t): | |
coll = [] | |
zero = F(0, 1) |
This file contains hidden or 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 collections import defaultdict as dd | |
from itertools import product | |
def solve(limit, values, weights): | |
""" Solution for the unbounded knapsack problem. """ | |
weights, values = zip(*sorted([(w, v) for w, v in zip(weights, values)])) | |
inf = limit + 1 | |
maxv = [0 for _ in xrange(limit + 1)] | |
for value, weight in zip(values, weights): |
This file contains hidden or 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 numpy as np | |
def gradient_descent(x, y, alpha, rounds): | |
z = np.ones(x.shapes[1]) | |
for _ in xrange(rounds): | |
z = z - alpha * np.dot(np.dot(X, z) - y, X) | |
return z |
This file contains hidden or 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 delete(tree, key): | |
remove_node = find(tree, key) | |
if remove_node is None: | |
raise KeyError | |
remove_color = remove_node.color | |
replace_node = None | |
rl, rr, tl = remove_node.left, remove_node.right, tree.leaf | |
if rl == tl and rr == tl: | |
replace_node = tree.leaf | |
transplant(tree, remove_node, tree.leaf) |
This file contains hidden or 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 java.util.Date; | |
import java.text.SimpleDateFormat; | |
SimpleDateFormat formatter = new SimpleDateFormat( "yyyyMMddHHmmss" ); | |
return formatter.format( new java.util.Date() ); |
This file contains hidden or 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
""" Solver logic for https://boardgamegeek.com/boardgame/1198/set """ | |
from random import shuffle | |
from itertools import product, combinations | |
revd = [ | |
["one", "two", "three"], | |
["red", "green", "blue"], | |
["empty", "half", "full"], | |
["ellipse", "rectangle", "wavy"]] |
This file contains hidden or 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
""" The problem description: | |
Given a Data Structure having first n integers and next n chars. | |
A = i1 i2 i3 ... iN c1 c2 c3 ... cN.Write an in-place algorithm | |
to rearrange the elements of the array ass A = i1 c1 i2 c2 ... in cn | |
""" | |
def swap(arr, ind1, ind2): |