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 / 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;
@blasten
blasten / pre-commit
Created April 21, 2015 04:51
Pre-commit git hook
#!/usr/bin/env python
import os, sys
"""
Checks your git commit with JSHint. Only checks staged files
"""
def jshint():
errors = []
function randomNumber(min, max) {
min = parseInt(min, 10);
max = parseInt(max, 10);
if (min !== min) {
min = 0;
}
if (max !== max || max <= min) {
max = (min || 1) << 1; //doubling
} else {
max = max + 1;
#!/bin/bash
# utils
# alias kp='pkill -f polyserve' - Kill all the `polyserve` processes
# alias p='~/polyserve.sh' - Polyserve
base=`basename "$PWD"`
port=$(( RANDOM % (1000) + 8000 ))
url="http://localhost:$port/components/$base/demo/index.html"
function refresh() {
var SCROLL_DIRECTION_UP = -1;
var SCROLL_DIRECTION_DOWN = 1;
var SCROLL_DIRECTION_NONE = 0;
var direction = SCROLL_DIRECTION_NONE;
var ratio = 0.5;
var delta = this.scrollTop - this.scrollPosition;
this.scrollPosition = this.scrollTop;
Base.isLightDom = function (node) {
return Polymer.dom(node).getOwnerRoot() === Polymer.dom(this).getOwnerRoot();
};
<!--
@license
Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
-->
<dom-module id="x-example">
<style>
:host {
display: block;
}
</style>
<template>
<div id="text">x-example</div>