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
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 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
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
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
from math import sqrt, ceil | |
from functools import wraps | |
from optparse import OptionParser | |
CACHE = {} | |
def cache(f): | |
@wraps(f) | |
def cached(*args, **kwargs): |
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
#include <iostream> | |
#include <string> | |
#include <algorithm> | |
using namespace std; | |
bool sol(string& s) { | |
bool total[26]; | |
fill_n(&total, 26, false); | |
for(char& c : s) { |
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
#!python | |
#cython: boundscheck=False, wraparound=False, initializedcheck=False | |
from cpython cimport bool | |
LEFT, RIGHT = range(2) | |
RED, BLACK = range(2) | |
cocodes = ["R", "B"] | |
chcodes = ["l", "r"] |
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 random import shuffle, randint as ri | |
from itertools import product | |
PLAYER, OP, BOTH, NEITHER = range(4) | |
suits = ["clubs", "diamonds", "hearts", "spades"] | |
values = [ | |
"ace", "two", "three", "four", "five", | |
"six", "seven", "eight", "nine", "ten", | |
"jack", "queen", "king"] |
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
RED, BLACK = range(2) | |
LEFT, RIGHT = range(2) | |
SMALLER, EQUAL, BIGGER = range(-1, 2) | |
def _set_relation(child, parent, rtype): | |
if child is not None: | |
child.parent = parent | |
if parent is not None: | |
if rtype == LEFT: |