Skip to content

Instantly share code, notes, and snippets.

View mnito's full-sized avatar

Michael P. Nitowski mnito

View GitHub Profile
@mnito
mnito / popcount_and_hamming.py
Created June 9, 2018 15:05
Population count and hamming distance in Python
def popcount(v):
lu = (0, 1, 1, 2, 1, 2, 2, 3)
c = 0
while v > 0:
c += lu[v & 0x7]
v >>= 3
return c
def hamming(v1, v2):
@mnito
mnito / tf_idf_example_service.rb
Created September 26, 2017 21:38
Not crazy familiar with Ruby but wanted to try to get up a basic web API quickly - took me a little over an hour from setup to posting here
require 'json'
require 'sinatra'
def tf_idf(word, doc, docs)
tf = doc.scan(/#{word}/).length / Float(doc.split.size)
docs_with_word = docs.map{ |doc| (doc.include? word) ? 1 : 0 }.reduce(0, :+)
idf = Math.log(docs.length / Float(1 + docs_with_word))
tf * idf
end
@mnito
mnito / srgb_xyz_conversion.js
Last active July 8, 2024 10:15
srgb and xyz color conversion
function xyz_to_srgb (xyz) {
var xsm = [
[3.2406255, -1.537208, -0.4986286],
[-0.9689307, 1.8757561, 0.0415175],
[0.0557101, -0.2040211, 1.0569959]
];
var rLinear = xyz.x * xsm[0][0] + xyz.y * xsm[0][1] + xyz.z * xsm[0][2];
var gLinear = xyz.x * xsm[1][0] + xyz.y * xsm[1][1] + xyz.z * xsm[1][2];
var bLinear = xyz.x * xsm[2][0] + xyz.y * xsm[2][1] + xyz.z * xsm[2][2];