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 / 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
# -*- coding: utf-8 -*-
# Este script extrai, para cada unidade federativa, todas as coligações
# partidárias para deputados estaduais e federais das eleições brasileiras
# de 2010.
#
# Por André Lima
# Email: andre (ponto) lima (arroba) gmail (ponto) com
# Twitter: @andlima
#
# Licença: é permitido a qualquer um usar, distribuir e modificar livremente
@andlima
andlima / ear.py
Created March 14, 2011 02:02
Simple ear training script
import math
import time
import ossaudiodev
import random
# Simple ear training script - by Andre Lima
#
# This program is based on code from:
#
# tone.py - Single / Dual Tone Generator
X=$1;
while [[ $X ]]; do
echo $X;
X=$(echo $X | python prefix.py);
done;
@andlima
andlima / primes.py
Created October 1, 2011 17:34
A simple generator of the primes sequence
primes_list = [2, 3]
def primes(maximum):
for p in primes_list:
if p > maximum:
return
yield p
for m in range(primes_list[-1] + 2, maximum + 1, 2):
sqrt_m = int(m ** 0.5)
if all(m % p != 0 for p in primes(sqrt_m)):
primes_list.append(m)
@andlima
andlima / primes.py
Created October 1, 2011 21:25
Sieve of Eratosthenes in Python
def primes(maximum):
discarded = set()
was_discarded = discarded.__contains__
discard_all = discarded.update
limit = maximum + 1
yield 2
for m in xrange(3, limit, 2):
if not was_discarded(m):
yield m
discard_all(xrange(m * 2, limit, m))
@andlima
andlima / inverse_sieve.c
Created October 1, 2011 21:46
Inverse Sieve of Eratosthenes in C
#include <stdio.h>
#include <stdlib.h>
typedef unsigned long long int t_item;
#define BITS sizeof(t_item)
void init(t_item *bitset, int maximum) {
int p = maximum / BITS;
int i;
@andlima
andlima / sieve.c
Created October 1, 2011 21:58
Sieve of Eratosthenes in C
#include <stdio.h>
#include <stdlib.h>
typedef unsigned long long int t_item;
static t_item all_true = ~0LL;
#define BITS sizeof(t_item)
void init_with_true(t_item *bitset, int maximum) {
@andlima
andlima / gist:1776496
Created February 9, 2012 02:06
Quick selection algorithm
int quick_find_kth(int *v, int n, int k) {
if (n == 1 && k == 0) return v[0];
int pivot = v[n-1];
int store = 0;
for (int i=0; i<n-1; i++) {
if (v[i] < pivot) {
swap(v[i], v[store++]);
}