Skip to content

Instantly share code, notes, and snippets.

View danilobellini's full-sized avatar

Danilo de Jesus da Silva Bellini danilobellini

  • Sao Paulo, Brazil
View GitHub Profile
@danilobellini
danilobellini / kruskal.py
Last active October 30, 2021 13:42
Kruskal's algorithm
"""
Kruskal's algorithm
Pure Python implementation by Danilo J. S. Bellini
"""
import pytest
def kruskal_sets(graph):
nodes = frozenset.union(*graph)
@danilobellini
danilobellini / closure.clj
Last active May 23, 2020 13:46
Simple closure example in Scheme, Haskell, Smalltalk, Python, Ruby, Clojure, CoffeeScript and JavaScript/ECMAScript
; Clojure
(defn mul [value]
(fn [data]
(map
(fn [item] (* item value))
data)))
; user=> ((mul 2) [3 2 8 -2])
; (6 4 16 -4)
@danilobellini
danilobellini / ethereum_note.py
Created July 24, 2018 17:53
Sanic (Python) web server for storing a single information note in the Ethereum blockchain
"""
Application to store a single message note in the Ethereum blockchain.
In order to run this web server, the ethereum node daemon
(e.g. the Geth HTTP RPC on 8545) should be running in the same host.
Input parameters should be on the following environment variables:
export ETH_CONTRACT_ADDRESS=0xAAAA... # Enforced
export ETH_NODE_URL='http://localhost:8545' # Optional
@danilobellini
danilobellini / example.py
Created May 13, 2018 18:08
A really simple/incomplete bottle/flask-like "single blocking worker thread" web server implementation (Python 2)
from server import WebServer
app = WebServer()
@app.route("/")
def root():
return "Hello World!"
@app.route("/file")
def file_route():
with open("server.py", "rb") as f:
@danilobellini
danilobellini / genetic.py
Last active October 18, 2017 16:37
A "Hello World" genetic algorithm I've made based on the C# code shown by Italo Jose de Oliveira at his TDC São Paulo 2017 presentation
#!/usr/bin/python3
"""
Genetic algorithm example that finds the best string match using the
hamming distance to define an aptitude function.
"""
from random import choice, seed, randrange
from functools import partial
import string
def crossover(a, b):
@danilobellini
danilobellini / reson.py
Last active March 18, 2017 18:20
Trying to do a time-variant Butterworth filter using SciPy and AudioLazy. Also, a faster time-variant resonator (AudioLazy) for comparison. Both examples includes instantaneous frequency measurements (from Discrete Hilbert Transform).
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Created on Mon Sep 09 23:06:21 2013
# @author: Danilo J. S. Bellini
"""
AudioLazy time-variant resonator helping instantaneous frequency measurements.
"""
from __future__ import print_function
from audiolazy import (sHz, sinusoid, gauss_noise, line, z, inf, resonator,
@danilobellini
danilobellini / plugin_talk_ptbr_2016.py
Created July 26, 2016 06:36
[Proposta/ementa de apresentação] Plugin pytest-doctest-custom (2016, PtBr)
#!/usr/bin/env python
# coding: utf-8
"""
>>> "Apresentação sobre doctest e py.test" is "nice"
True
>>> "O.o heeey, como assim?"
HUAHUAHUAHUA
>>> "Apresentação sobre doctest e py.test" is "a bad idea"
False
>>> # Ok, ¬¬ chega de strings
@danilobellini
danilobellini / divisors.py
Last active July 25, 2016 12:46
Find all the divisors from a given number
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Created on Wed Oct 30 10:55:51 2013
# By Danilo J. S. Bellini
# Based on https://gist.github.com/anonymous/7123189
"""
Find all the divisors from a given number
"""
# Ensure compatibility in both Python 2 and 3
@danilobellini
danilobellini / viral_maths_puzzle_sympy_solution.py
Created May 2, 2016 20:52
Smallest quadratic equations that answers a viral math puzzle using Sympy
#!/usr/bin/python
# By Danilo J. S. Bellini
# 2016-05-02 17:31:56 BRT
"""
Smallest quadratic equations that answers the viral math puzzle below,
using Sympy
1 + 4 = 5
2 + 5 = 12
3 + 6 = 21
8 + 11 = ?
@danilobellini
danilobellini / python_nordeste_show.py
Created January 8, 2014 07:36
"AudioLazy Band Show" Python Nordeste 2014 talk proposal description
# -*- coding: utf-8 -*-
# Created on Wed Jan 8 04:03:53 2014
# @author: Danilo J. S. Bellini
# Python Nordeste 2014 talk proposal description
# http://2014.pythonnordeste.org/
# Type it in IPython Shell.
# Using this as a script would need to replace lazy_band assignment to