Skip to content

Instantly share code, notes, and snippets.

@laurent22
laurent22 / levenshteinDistance.go
Last active November 22, 2018 16:06
Golang function to calculate Levenshtein distance between two strings. Adapted from Wikipedia article.
import (
"math"
"strings"
)
func levenshteinDistance(s string, t string) int {
// degenerate cases
s = strings.ToLower(s)
t = strings.ToLower(t)
if (s == t) { return 0 }
@laurent22
laurent22 / extractBaseUrl
Created July 15, 2013 11:30
Extracts base URL from location of current document.
// Extracts base URL from location of current document.
function extractBaseUrl() {
// Get URL without query path
var url = [location.protocol, '//', location.host, location.pathname].join('');
// Remove filename if present
var f = url.substr(url.lastIndexOf("/") + 1).toLowerCase();
if (f.indexOf('.html') >= 0 || f.indexOf('.htm') >= 0 || f.indexOf('.php')) {
url = url.substr(0, url.length - f.length);
}
// Add last slash if missing
@laurent22
laurent22 / dummyconsole.js
Last active December 15, 2015 01:29
Fake Javascript console to avoid errors in browsers that don't support the feature.
if (typeof(console) === 'undefined') {
var functionNames = ['info', 'error', 'warn', 'dir', 'trace', 'log', 'assert'];
console = {};
for (var i = 0; i < functionNames.length; i++) console[functionNames[i]] = function(){};
}
@sepehr
sepehr / readable_random_string.php
Last active December 8, 2023 15:11
PHP: Human-readable Random String
<?php
/**
* Generates human-readable string.
*
* @param string $length Desired length of random string.
*
* retuen string Random string.
*/
function readable_random_string($length = 6)