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 / 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()
@blasten
blasten / followsPattern.js
Last active August 29, 2015 14:14
Given a pattern P, check if the string S follows that pattern.
// Given a pattern P, check if the string S follows that pattern.
// e.g.
// P = aab
// S = hellohelloworld
// -> true
// This a naive brute force solution that runs in O(2^N) where N is the size of S,
// I hope there's a better solution.
function followsPattern(P, S) {
function solve(i, complement) {
if (i === S.length) {
@blasten
blasten / DOM-dsf-bfs.js
Last active August 29, 2015 14:14
Depth and Breath first transversals without recursion in the DOM tree
function dfs(root, fn) {
if (!(root instanceof HTMLElement)) {
throw new Error('`root` should be an HTML Element');
return;
}
if (!(fn instanceof Function)) {
fn = function(element) {
console.debug(element);
}
}
@blasten
blasten / rotatePicture.js
Created February 4, 2015 19:06
Rotates a matrix of pixels in place
function rotatePicture(matrix) {
if (matrix.length == 0 || matrix.length != matrix[0].length) {
return [];
}
var n = matrix.length;
var center = parseInt(n/2);
for (var i = 0; i < center; i++) {
var start = i;