This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Declare `~>` operator | |
infix operator ~> {} | |
// Define the `~>` operator | |
// | |
// Example: | |
// if inputStr ~> "^[a-zA-Z]*$" { | |
// println("\(inputStr) only has letters") | |
// } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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)]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* The following algorithm maximizes the size of the thumbnails in a grid with an arbitrary width | |
* Some examples of this algorithm are: | |
* https://www.flickr.com/search/?q=landscapes | |
* https://www.google.com/search?q=landscapes&source=lnms&tbm=isch | |
* | |
* | |
* @param width {Number} the width of the viewport in the same unit as the size of the images | |
* @param maxRowHeight {Number} the maximum allowed size of the row | |
* @param images {Array} containing the size of each image in the following structure: [width, height] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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++) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Grabs a lock and ensures mutual exclusion while calling `closure` | |
* Example: | |
* synced(self) { | |
* // Code | |
* } | |
*/ | |
func synced(lock: AnyObject, closure: () -> ()) { | |
objc_sync_enter(lock) | |
closure() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |