Skip to content

Instantly share code, notes, and snippets.

View andlima's full-sized avatar

André Lima andlima

  • Rio de Janeiro, Brazil
View GitHub Profile
@andlima
andlima / p2.py
Created May 27, 2012 23:41
Solução do P2 - 2016
def distance(points, i, j):
x1, y1 = points[i]
x2, y2 = points[j]
return ((x1 - x2) ** 2 + (y1 - y2) ** 2) ** 0.5
def merge_root(root, i, j):
good, bad = sorted([root[i], root[j]])
bad_keys = [k for k, v in root.iteritems() if v == bad]
for key in bad_keys:
root[key] = good
@andlima
andlima / gist:2872210
Created June 5, 2012 02:43
MST - Kruskal
def distance(points, i, j):
x1, y1 = points[i]
x2, y2 = points[j]
return ((x1 - x2) ** 2 + (y1 - y2) ** 2) ** 0.5
def find_root(height_and_parent, i):
hi, pi = height_and_parent[i]
if pi is None:
return i
return find_root(height_and_parent, pi)
@andlima
andlima / problem_c.py
Last active December 16, 2015 09:59
Google Code Jam 2013 - Qualification Round - Problem C
from bisect import bisect_left, bisect_right
def solve(numbers, a, b):
answer = bisect_right(numbers, b) - bisect_left(numbers, a)
return answer if answer > 0 else 0
def main():
numbers = [1, 4, 9]
previous = ('012', [''])
@andlima
andlima / trie.py
Last active August 29, 2015 14:01
from __future__ import print_function
class Trie(object):
'''Implementation of a trie.'''
def __init__(self, collection=None):
self.ends = False
self.children = {}
if collection is not None:
@andlima
andlima / mersenne_twister.rs
Last active August 29, 2015 14:06
Mersenne Twister 19937 PRNG
fn initialize_generator(mt: &mut [uint], index: &mut uint, seed: uint)
{
mt[0] = seed;
*index = 0;
for i in range(1, 624) {
mt[i] = (
(
1812433253 * (mt[i-1] ^ (mt[i-1] >> 30))
) + i
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.