Skip to content

Instantly share code, notes, and snippets.

@jakab922
jakab922 / parse_flags.py
Created December 3, 2016 19:17
Parsing flags for the smartcab assignment
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",
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]:
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:
from fractions import Fraction as F
class NotSolved(Exception):
pass
def odd_solve(t):
coll = []
zero = F(0, 1)
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):
@jakab922
jakab922 / gradient_descent.py
Created September 3, 2016 11:25
Gradient decent for linear regression
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
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)
@jakab922
jakab922 / datetime_format.bsh
Created August 30, 2016 07:59
Formatting the current time in beanshell
import java.util.Date;
import java.text.SimpleDateFormat;
SimpleDateFormat formatter = new SimpleDateFormat( "yyyyMMddHHmmss" );
return formatter.format( new java.util.Date() );
""" 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"]]
""" 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):