Skip to content

Instantly share code, notes, and snippets.

View fmasanori's full-sized avatar

Fernando Masanori fmasanori

View GitHub Profile
@fmasanori
fmasanori / ChuckJokesPython3.py
Last active September 5, 2023 11:36
Chuck Norris Nerd Jokes
from urllib.request import Request, urlopen
import json
req = Request(
url='https://api.chucknorris.io/jokes/random',
headers={'User-Agent': 'Mozilla/5.0'}
)
resp = urlopen(req).read()
data = json.loads(resp.decode('utf-8'))
@fmasanori
fmasanori / gmail.py
Created February 9, 2013 13:23
Gmail Send Message
import smtplib
usr = '[email protected]'
pwd = 'your password'
dst = 'destination email'
server = smtplib.SMTP('smtp.gmail.com:587')
server.starttls()
server.login(usr, pwd)
@fmasanori
fmasanori / fibcache.py
Created February 12, 2013 12:52
Fibonacci Recursivo com Cache
from functools import lru_cache
@lru_cache(maxsize=None)
def fib(n):
if n <= 2:
return 1
return fib(n-1) + fib(n-2)
@fmasanori
fmasanori / Hello Bottle.py
Created February 12, 2013 13:08
Hello Bottle World
import bottle
#this is the handler for the root address on the web browser
@bottle.route('/')
def home_page():
return 'Hello Bottle World'
@bottle.route('/testpage')
def test_page():
return 'Testing page'
@fmasanori
fmasanori / clock.py
Last active June 28, 2017 19:19
Clock GUI
import tkinter
from time import strftime
#by Luciano Ramalho
clock = tkinter.Label()
clock.pack()
clock['font'] = 'Helvetica 120 bold'
clock['text'] = strftime('%H:%M:%S')
@fmasanori
fmasanori / gamename.py
Last active December 17, 2015 13:19
Jogo de advinhar um nome feminino entre os mais frequentes no Brasil (feito por uma menina de 12 anos)
import random
nomes = '''Júlia Sophia Isabella Manuela Giovanna Alice Laura
Luiza Beatriz Mariana Yasmin Gabriela Rafaela Isabelle Lara
Letícia Valentina Nicole Sarah Vitória Isadora Lívia Helena
Lorena Clara Larissa Emanuelly Heloisa Marina Melissa Gabrielly
Eduarda Rebeca Amanda Alícia Bianca Lavínia Fernanda Ester
Carolina Emily Cecília Pietra Milena Marcela Laís Natália
Maria Bruna Camila Luana Catarina Olivia Agatha Mirella
Sophie Stella Stefany Isabel Kamilly Elisa Luna Eloá Joana
Mariane Bárbara Juliana Rayssa Alana Caroline Brenda Evelyn
@fmasanori
fmasanori / unicode.py
Created May 21, 2013 13:31
Unicode normalizations
a = 'Co\u00f1o!'
b = 'Con\u0303o!'
print (a, b, a == b, len(a), len(b))
print ()
import unicodedata
print ('Fully Composed')
a1 = unicodedata.normalize('NFC', a)
b1 = unicodedata.normalize('NFC', b)
print (a1, b1, a1 == b1, len(a1), len(b1))
@fmasanori
fmasanori / time_decorator.py
Created May 21, 2013 14:03
Time decorator
import time
from functools import wraps
def tempo(func):
@wraps(func)
def wrapper(*args, **kwargs):
t1 = time.time()
result = func(*args, **kwargs)
t2 = time.time()
print(func.__name__, t2 - t1)
return result
@fmasanori
fmasanori / facebook_hackaton.py
Last active September 27, 2017 16:59
Selection Test 2013 Facebook Hackaton Given two positive integers n and k, generate all binary integer between 0 and 2 ** n-1, inclusive. These binaries will be drawn in descending order according to the number of existing 1s. If there is a tie choose the lowest numerical value. Return the k-th element from the selected list. Eg n = 3 and k = 5 …
def hack1(n, k):
def f(s):
return s.count('1')
binaries = []
for x in range(2**n):
binaries.append(bin(x))
binaries.sort(key=f, reverse = True)
return binaries[k - 1]
def hack(n, k):
@fmasanori
fmasanori / sort_vs_heapq.py
Created May 21, 2013 14:35
Sort vs Heapq
import time, random
N = 1000000
NL = 1 #100000 10 1
origem = [random.randrange(N) for x in range(N)]
lista = list(origem)
t = time.time()
lista.sort(reverse=True)
print (time.time() - t)