Skip to content

Instantly share code, notes, and snippets.

@jakab922
jakab922 / sys.path.py
Created January 8, 2014 13:24
sys path manipulation
import sys
import os
from os.path import join
from mock import Mock, patch
from twisted.internet import defer
src_path = os.path.realpath(join(os.path.dirname(__file__), "../src/"))
current_path = os.path.realpath(os.path.dirname(__file__))
sys.path.insert(0, current_path)
sys.path.insert(0, src_path)
@jakab922
jakab922 / bubba
Created April 11, 2014 11:32
i8042 error
[ 0.637461] Write protecting the kernel text: 4232k
[ 0.637500] Write protecting the kernel read-only data: 1272k
[ 0.647427] random: systemd-tmpfile urandom read with 0 bits of entropy available
[ 0.650489] systemd-udevd[47]: starting version 212
[ 0.653627] i8042: PNP: PS/2 Controller [PNP0303:PS2K] at 0x60,0x64 irq 1
[ 0.653634] i8042: PNP: PS/2 appears to have AUX port disabled, if this is incorrect please boot with i8042.nopnp
[ 0.654699] i8042: Warning: Keylock active
[ 0.654847] serio: i8042 KBD port at 0x60,0x64 irq 1
[ 0.660105] input: AT Translated Set 2 keyboard as /devices/platform/i8042/serio0/input/input0
[ 0.679289] sdhci: Secure Digital Host Controller Interface driver
# BaseTestClient is flask.testing.FlaskClient
class TestClient(BaseTestClient):
"""
Test Client.
"""
def __exit__(self, exc_type, exc_value, tb):
self.preserve_context = False
top = _request_ctx_stack.top
def check(A, B, C, index):
totals = [0 for _ in xrange(index)]
for i in xrange(index - 1, -1, -1):
totals[i] += B[i]
if C[i] != -1:
totals[C[i]] += totals[i]
return all([totals[i] <= A[i] for i in xrange(index)])
@jakab922
jakab922 / fib_comp.hs
Created October 6, 2015 20:44
Simple fibonacci generator
import System.Environment
fib_gen = 0 : 1 : [a + b | (a, b) <- zip fib_gen (tail fib_gen)]
main = do
args <- getArgs
let first_arg = if null args then "1" else head args
let fa = read first_arg :: Int
print $ last $ take fa fib_gen
checkers = [
lambda nums: False,
lambda nums: True,
lambda nums: nums[-1] % 2 == 0,
lambda nums: sum(nums) % 3 == 0,
lambda nums: (10 * nums[-2] + nums[-1]) % 4 == 0,
lambda nums: nums[-1] in [0, 5],
lambda nums: sum(nums) % 3 == 0 and nums[-1] % 2 == 0,
lambda nums: sum([10 ** i * nums[-(i + 1)] for i in xrange(len(nums))]) % 7 == 0,
lambda nums: sum([10 ** i * nums[-(i + 1)] for i in xrange(3)]) % 8 == 0,
""" Computing the number of permutations on n points
no fix points in 3 ways. """
from functools import wraps
from math import factorial, e, floor
from sys import argv
CACHE = {}
def cache(f):
@wraps(f)
from collections import defaultdict
from sys import argv
def to_dict(word):
ret = defaultdict(int)
for letter in word:
ret[letter] += 1
return ret
@jakab922
jakab922 / freelancers_dream.py
Last active December 10, 2015 10:59
Solution for Freelancer's dream on codeforces(http://codeforces.com/contest/606/problem/E)
""" The problem is an extremely easy linear programming problem.
The only issue is that there is no simplex solver in the
standard library and scipy is not available on codeforces.
"""
from scipy.optimize import linprog
n, p, q = map(int, raw_input().strip().split())
A = [[], []]
b = [-p, -q]
c = [1 for _ in xrange(n)]
import re
PUT_PATTERN = re.compile(r'PUT (?P<what>[0-9]+) INSIDE (?P<to>[0-9]+)')
LOOSE_PATTERN = re.compile(r'SET (?P<which>[0-9]+) LOOSE')
SWAP_PATTERN = re.compile(r'SWAP (?P<one>[0-9]+) WITH (?P<other>[0-9]+)')
class Bag(object):
def __init__(self, n, parent=None):
self.n = n