Skip to content

Instantly share code, notes, and snippets.

View fmasanori's full-sized avatar

Fernando Masanori fmasanori

View GitHub Profile
# BlueBook code decryption
import sys
def sieve(n):
# Compute primes using sieve of Eratosthenes
x = [1] * n
x[1] = 0
for i in range(2,n//2):
j = 2 * i
while j < n:
x[j] = 0
#traduzido e adaptado de http://blog.trinket.io/writing-poetry-in-python/
from random import choice, randint
adjetivos = '''compreensivo temperamental confiável confiável honesto desonesto
interessante chato carinhoso simpático amigável generoso ciumento invejoso
inseguro ambicioso ansioso bondoso sensato sensível teimoso preguiçoso
trabalhador calmo paciente inteligente esperto espirituoso astuto neurótico
ousado apático cínico sarcástico irônico cético alegre conservador pessimista
otimista tolerante corajoso educado mal-educado determinado sociável
solidário arrogante maldoso desajeitado burro independente confiável dependente
@fmasanori
fmasanori / votos.py
Created October 9, 2014 13:23
Resultados das eleições 2014 Python 3 (adaptado Vinicius Marangoni)
#versão Python 3 de http://pastebin.com/uRnDZaTz (Vinicius Marangoni)
import urllib.request
import json
url = 'http://divulga.tse.jus.br/2014/divulgacao/oficial/143/dadosdivweb/br/br-0001-e001431-w.js'
def main():
resp = urllib.request.urlopen(url).read()
resp = json.loads(resp.decode('utf-8'))
candidatos = resp['cand']
@fmasanori
fmasanori / heapsort.py
Created September 3, 2014 15:01
Python Heapsort
from heapq import heappush, heappop
def heapsort(v):
h = []
for x in v:
heappush(h, x)
return [heappop(h) for i in range(len(h))]
from random import shuffle
v = list(range(8))
shuffle(v)
@fmasanori
fmasanori / mergesortI.py
Created September 3, 2014 13:57
Python Interactive Mergesort
def merge(p, q, r, v):
w = []
i, j = p, q
while i < q and j < r:
if v[i] <= v[j]:
w.append(v[i])
i += 1
else:
w.append(v[j])
j += 1
@fmasanori
fmasanori / mergesort.py
Last active August 29, 2015 14:05
mergesort
def merge(e, d):
r = []
i, j = 0, 0
while i < len(e) and j < len(d):
if e[i] <= d[j]:
r.append(e[i])
i += 1
else:
r.append(d[j])
j += 1
@fmasanori
fmasanori / dec2bin.py
Created August 4, 2014 14:19
Decimal para binário em Python 3
def dec2bin(n):
b = ''
while n != 0:
b = b + str(n % 2)
n = int(n / 2)
return b[::-1]
def d2b(n):
if n == 0:
return ''
@fmasanori
fmasanori / JS Chess
Last active August 29, 2015 14:04
JS Chess
#@gwidion code
print"".join(u"\x1b[%dm%c%s"%((42+y%2^y>>3&1),((1,3,2,-1,0,2,3,1)+(4,)*8+32*(-9781,)+8*(10,)+(7,9,8,5,6,8,9,7))[y]+9813,(y+1)%8and" "or" \x1b[48m\n")for y in range(64))
@fmasanori
fmasanori / jogos.py
Last active October 30, 2022 00:00
World Cup in six lines of Python 3. Jogos da Copa do Mundo em cinco linhas de Python 3.
import requests
jogos = requests.get('http://worldcup.sfg.io/matches').json()
for jogo in jogos:
if jogo['status'] in ('completed', 'in progress'):
print (jogo['home_team']['country'], jogo['home_team']['goals'], 'x',
jogo['away_team']['country'], jogo['away_team']['goals'])
class int42(int):
def __init__(self, n):
int.__init__(n)
def __add__(a, b):
return 42
def __str__(n):
return '42'