Skip to content

Instantly share code, notes, and snippets.

@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 / 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.
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 / 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'
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 / cards.py
Created November 30, 2017 06:33
Finding the missing Dobble cards
symbols = [
"sziv", "virag", "ora", "villanykorte", "teknos", "pok", "cica", "szmotyi",
"kerdojel", "sajt", "katica", "lakat", "nap", "hopehely", "tilos",
"napszemuveg", "felkialtojel", "emberke", "jinjang", "tuz", "pokhalo", "madar",
"bohoc", "kalapacs", "iglu", "dino", "gyertya", "szaj", "fa", "hoember", "ollo", "lohere",
"hangjegy", "sakk", "szellem", "vizcsepp", "hold", "vasmacska", "jeg", "cumi", "bomba",
"zebra", "kutya", "koponya", "repa", "alma", "ceruza", "villam", "kaktusz", "auto",
"kez", "celkereszt", "szem", "delfin", "level", "sarkany", "kulcs" ]
s = set(symbols)
print "The number of symbols are: %s" % (len(symbols), )
@jakab922
jakab922 / sanitize.py
Created December 18, 2017 14:52
Script for sanitizing python projects so that code intelligence works in them
import os.path
import os
def touch(fname, times=None):
"""via: https://stackoverflow.com/a/1160227/8785384 """
with open(fname, "a"):
os.utime(fname, times)
@jakab922
jakab922 / stuff.py
Last active January 19, 2018 09:58
Testing example
# kmp.py
def get_prefix(string):
l = len(string)
ret = [0] * l
border = 0
for i in xrange(1, l):
while border > 0 and string[i] != string[border]:
border = ret[border - 1]
if string[i] == string[border]:
border += 1
n = int(raw_input().strip())
acc = []
for _ in xrange(n):
c = int(raw_input().strip())
if c >= 90:
acc.append(c)
if acc:
print "%.2f" % (sum(acc) / float(len(acc)))
@jakab922
jakab922 / sol.cc
Created April 22, 2018 23:00
Solution for Topcoder Open 2018 / 1A / 1000
#include <vector>
#include <unordered_set>
#include <algorithm>
#include <cmath>
#include <tuple>
#include <iostream>
using namespace std;
typedef long long ll;