Skip to content

Instantly share code, notes, and snippets.

View juanplopes's full-sized avatar
🌲
Is that a Segment Tree problem?

Juan Lopes juanplopes

🌲
Is that a Segment Tree problem?
View GitHub Profile
100 1073741823
1 1
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
@juanplopes
juanplopes / convexhull01.html
Created February 26, 2014 23:38
Revised @ElemarJR's Convex Hull
<!doctype html>
<html>
<head>
<title>ConvexHull 01</title>
<style type="text/css">
* {
margin: 0;
padding: 0;
}
@juanplopes
juanplopes / convexhull01.html
Last active August 29, 2015 13:56
Revised @ElemarJR's Convex Hull (using Monotone Chain)
<!doctype html>
<html>
<head>
<title>ConvexHull 01</title>
<style type="text/css">
* {
margin: 0;
padding: 0;
}
@juanplopes
juanplopes / 2048.js
Created March 12, 2014 02:53
Este script provavelmente joga 2048 melhor que você
var sendSingle = function(key) {
var event = new KeyboardEvent('keydown');
Object.defineProperty(event, 'keyCode', {get : function() {return key;}});
Object.defineProperty(event, 'which', {get : function() {return key;}});
document.dispatchEvent(event);
}
var i = 0;
handle = setInterval(function() {
@juanplopes
juanplopes / gist:178060e556e68421a8c5
Last active August 29, 2015 14:02
Ford-Fulkerson example for presentation: http://goo.gl/DizfF7
from collections import defaultdict
def add(network, a, b, capacity):
network[a][b] = network[b][a] = capacity
def send(network, a, b, V, minimum=1000000):
V.add(a)
if a == b:
print '-> path', a
return minimum
@juanplopes
juanplopes / pave.md
Last active August 29, 2015 14:05
Candidato, é pavê ou pra comê? (texto de @kramercarlos)

Texto criado pelo amigo Carlos Krämer (github . twitter . facebook) lá no Facebook, reproduzido aqui com a permissão dele.

Jornalista: Candidato, é pavê ou pra comê?

Dilma: Veja bem, nunca foi tanto pra comê quanto no meu governo. Há um ditado.... eu digo, editaram quando eu era o cachorro atrás de uma criança ... que diz que todo mundo que insere, ou melhor, ingere o que é pavê, tem uma satisfação imensa de... como dizem... dobrar a esquina. Mas nós precisamos continuar mudando, meu governo foi ótimo, eu fui eleita para dar prosseguimento ao governo Lula, Lula, LULA. Mas ninguém está mais insatisfeita com ele, digo, com o governo, não com o Lula, do que eu, mas os pessimistas é que estão tornando tudo ruim.

Marina: Eu não acho que é pavê nem pra comê. Nós precisamos aprender a reunir o que é bom em tudo, as melhores

@juanplopes
juanplopes / mersenne.py
Created September 16, 2014 19:00
Finding Mersenne Primes using Lucas Lehmer method
def prime(n):
return all(n%i!=0 for i in xrange(2,int(n**0.5)+1))
def lucas_lehmer(p):
return reduce(lambda s, _: (s*s-2)%(2**p-1), xrange(p-2), 4)==0
for i in range(2, 2048):
if prime(i) and lucas_lehmer(i):
print '2**%d-1 is a Mersenne prime' % i
print 2**i-1
@juanplopes
juanplopes / schedule.py
Created September 30, 2014 13:09
Simple scheduling algorithm in python
import heapq
def schedule(tasks):
heap = []
def offer(begin, end):
while heap and heap[0] <= begin:
heapq.heappop(heap)
heapq.heappush(heap, end)
return len(heap)
@juanplopes
juanplopes / schedule.py
Created September 30, 2014 14:51
Simple scheduling algorithm in python (using Fenwick Trees)
class Fenwick:
def __init__(self, n):
self.n = n+1
self.T = [0] * self.n
def adjust(self, k, v):
k+=1
while k < self.n:
self.T[k] += v
k += k&-k
OPS = {
'+': int.__add__,
'-': int.__sub__,
'*': int.__mul__,
'/': int.__div__,
}
def evaluate(expression):
def add(chars):
e = mult(chars)