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
# -*- 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:1774060
Created February 8, 2012 21:34
Median of medians selection algorithm
int find_kth(int *v, int n, int k) {
if (n == 1 && k == 0) return v[0];
int m = (n + 4)/5;
int *medians = new int[m];
for (int i=0; i<m; i++) {
if (5*i + 4 < n) {
int *w = v + 5*i;
for (int j0=0; j0<3; j0++) {
int jmin = j0;
@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++]);
}
@andlima
andlima / scrape_letras_terra.py
Created April 7, 2012 03:59
Scrape letras.terra.com.br
"""
A short script that, given an artist, scrapes all his/her/their songs
from 'http://letras.terra.com.br'
Dependence: BeautifulSoup, to parse the lyrics from each song page.
Author: Andre Lima - http://github.com/andlima
Licensed under MIT License