Skip to content

Instantly share code, notes, and snippets.

import pytest
from sol2 import solve
def brute_solve(n, step=2):
curr = range(n)
what = 0
while len(curr) > 1:
nwhat = (what - len(curr) % step) % step
@jakab922
jakab922 / vimrc
Created October 6, 2017 05:18
my vimrc for now
" Vundle setup
set nocompatible
filetype off
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
Plugin 'dyng/ctrlsf.vim'
Plugin 'vim-syntastic/syntastic'
Plugin 'tomtom/tlib_vim'
Plugin 'toml-lang/toml'
Plugin 'Shougo/unite.vim'
from fractions import gcd
from collections import defaultdict as dd
n, k = map(int, raw_input().strip().split())
ais = filter(lambda x: x != 0, map(int, raw_input().strip().split()))
n = len(ais)
mod = 10 ** 9 + 7
curr = {1: 1}
for ai in ais:
@jakab922
jakab922 / list_max.py
Created July 25, 2017 11:01
Solution to hackerrank's list_max problem. The algorithm runs in O(m log n) but it's too slow due to being written in python.
"""The problem statement is the following:
You are given a list of size N, initialized with zeroes. You have to perform M operations on the list and output the maximum of final values of all the N elements in the list. For every operation, you are given three integers a, b and k. The value k needs to be added to all the elements ranging from index a through b (both inclusive).
Input Format
The first line will contain two integers N and M separated by a single space.
The next M lines will each contain three integers a, b and k separated spaces.
The numbers in the list are numbered from 1 to N.
@jakab922
jakab922 / hanoi_moves.py
Last active April 6, 2017 12:50
Towers of Hanoi move generator
def sol(n):
other = lambda x: 1 if x == 2 else 2
first, empty = (1, 2) if n % 2 == 0 else (2, 1)
ms = [[1, 2, 0], [2, 0, 1]]
m = ms[empty % 2]
ret = [(0, first)]
for i in xrange(2, n + 1):
ret.append((0, empty))
ret.extend(map(lambda x: (m[x[0]], m[x[1]]), ret[:-1]))
@jakab922
jakab922 / rbt_half_baked.py
Created April 3, 2017 10:24
Half baked red and black tree
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:
@jakab922
jakab922 / blackjack.py
Created March 30, 2017 15:52
Command line blackjack game
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"]
@jakab922
jakab922 / rbt.pyx
Created March 2, 2017 17:11
Red and black tree insert operation implemented. It's an order of magnitude slower than the stl set so I guess there is space for improvement.
#!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"]
#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) {
@jakab922
jakab922 / egg_and_floors.py
Last active February 7, 2017 14:04
Solution to the eggs and floors puzzle. The original asks that if you have a 100 floors and 2 eggs what' the minimum number of drops needed to figure out where the eggs break.
from math import sqrt, ceil
from functools import wraps
from optparse import OptionParser
CACHE = {}
def cache(f):
@wraps(f)
def cached(*args, **kwargs):