Skip to content

Instantly share code, notes, and snippets.

View blasten's full-sized avatar

Emmanuel Garcia blasten

  • c.ai
  • Palo Alto, CA
View GitHub Profile
@blasten
blasten / closet-point.js
Last active August 29, 2015 14:12
Algorithm that returns the closest point from a list to a given point useful for UI interactions.
// Returns the closest point from a list to a given point
// the naive approach runs in linear time, which is a quite slow if are calling this procedure a lot.
// The following is an approach using a KD-tree. It runs in <O(n*log n), O(log n)> time
//
// Example:
// var closetPoint = closetPointTree([[2, 3], [10, 17], [5, 6]]);
// closetPoint([10, 16])
// -> [ 10, 17 ]
function closetPointTree(points) {
@blasten
blasten / group-intervals.js
Created January 6, 2015 05:57
Group all overlapping intervals.
// Group all overlaping intervals
// * * * * * * *
// This is an approach to a problem the engineers at Google Calandar/ Outlook probably faced.
// You have events that may overlap and you want to display them in such a way that
// they don't overlap with each other. One approach is to distribute them into columns.
// Each column has events that don't overlap with each other.
// Cost: O(n*log n) if the interval aren't sorted by the starting time,
// O(n) otherwise.
// Sample run: groupOverlapingIntervals([ [2, 5], [5, 6],[3, 4] ])
@blasten
blasten / gcd.swift
Created January 6, 2015 18:40
Managing threads in swift using Grand Central Dispatch
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
// I'm the background thread
dispatch_async(dispatch_get_main_queue()) {
// I'm the UI thread
}
}
@blasten
blasten / regexp-helper.swift
Created January 6, 2015 19:52
Infix operator for regular expressions.
// Declare `~>` operator
infix operator ~> {}
// Define the `~>` operator
//
// Example:
// if inputStr ~> "^[a-zA-Z]*$" {
// println("\(inputStr) only has letters")
// }
@blasten
blasten / suffix-array.js
Last active August 29, 2015 14:12
Suffix array
// Build a suffix array in O(n^2*log n);
// kinda too much, but now you can search for a pattern in O(n*log n) where `n` is number of characters in `str`
//
function searchPattern(str) {
var strLen = str.length;
var suffixArray = new Array(strLen);
var suffixes = new Array(strLen);
for (var i = strLen-1; i >= 0; i--) {
suffixes[i] = [i, str.substr(i)];
@blasten
blasten / string2html.js
Last active August 29, 2015 14:13
Applies a HTML formatting to a raw string
/**
* Applies HTML formatting to a raw string
* Runtime: O(n)
*
* @param str {String} A raw string
* @param formats {Array} An array of formats where each one has the following structure: [startOffset, endOffset, tagName]
* @return formattedString {String}
**/
function format(str, formats) {
var formatsLen = formats.length;
@blasten
blasten / image-gallery-layout.js
Last active August 29, 2015 14:13
The following algorithm maximizes the size of the thumbnails in a grid as seen on Google+, Flickr, or Google images
function newRange(startElement, startOffset, endElement, endOffset) {
var range = document.createRange();
range.setStart(startElement, startOffset);
range.setStart(endElement, endOffset);
return range;
}
function selectRange(range) {
window.getSelection().addRange(range);
}
// https://oj.leetcode.com/problems/word-ladder/
function replaceChar(str, index, character) {
return str.substr(0, index) + character + str.substr(index + 1);
}
function ladderLength(start, end, dict) {
var dictTable = {};
var endTable = {};
for (var j = 0; j < end.length; j++) {
@blasten
blasten / synced.swift
Created January 26, 2015 19:54
Grabs a lock and ensures mutual exclusion in swift
/**
* Grabs a lock and ensures mutual exclusion while calling `closure`
* Example:
* synced(self) {
* // Code
* }
*/
func synced(lock: AnyObject, closure: () -> ()) {
objc_sync_enter(lock)
closure()