Skip to content

Instantly share code, notes, and snippets.

View keif's full-sized avatar

Keith Baker keif

View GitHub Profile
@keif
keif / more encode and decode
Last active August 29, 2015 14:19
A generic morse encode/decode exercise utilizing filters and map instead of regex matching.
var charCodes= {
"a": ". _",
"b": "_ . . .",
"c": "_ . _ .",
"d": "_ . .",
"e": ".",
"f": ". . _ .",
"g": "_ _ .",
"h": ". . . .",
"i": ". .",
@keif
keif / evalRPN
Created April 21, 2015 06:00
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, *, /. Each operand may be an integer or another expression. For example: ["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9 ["4", "13", "5", "/", "+"] -> (4 + (13 / 5)) -> 6
// ["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9
// ["4", "13", "5", "/", "+"] -> (4 + (13 / 5)) -> 6
var arr1 = ["2", "1", "+", "3", "*"]; // 9
var arr2 = ["4", "13", "5", "/", "+"]; // 6
// lifted from jQuery
var inArray = function (elem, arr, i) {
var len;
if (arr) {
@keif
keif / palindrome
Last active April 26, 2018 14:47
Palindrome Examples in JavaScript
// naive approach
// Naively, we can simply examine every substring and check if it is palindromic.
// The time complexity is O(n^3).// Therefore, this approach is just a start, we need a better algorithm.
var longestPalindrome1 = function (string) {
var maxPalinLength = 0,
longestPalindrome = null,
length = string.length;
// check all possible sub strings
for (var i = 0; i < length; i++) {
@keif
keif / Manacher
Created April 22, 2015 05:09
A VERY INCOMPLETE AND UNTESTED JavaScript code rip of Manacher's algorithm. I want to get this into node to actually play with it.
/**
* Computes the longest palindromic substring in linear time
* using Manacher's algorithm.
*
* Credits: The code is lifted from the following excellent reference
* http://www.leetcode.com/2011/11/longest-palindromic-substring-part-ii.html
**/
var Manacher = (function () {
var palindromic = [], // p[i] = length of longest palindromic substring of t, centered at i
@keif
keif / wordbreak1
Created April 22, 2015 05:34
// Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words. // For example, given var string = "leetcode", dict = ["leet", "code"]; // Return true because "leetcode" can be segmented as "leet code".
// Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words.
// For example, given
var string = "leetcode",
dict = ["leet", "code"];
// Return true because "leetcode" can be segmented as "leet code".
// 1. Naive Approach
// This problem can be solve by using a naive approach, which is trivial. A discussion can always start from that though.
// Time is O(n^2) and exceeds the time limit.
var wordBreak = function (string, dict) {
@keif
keif / wordbreak2
Created April 22, 2015 05:46
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word. Return all such possible sentences. For example, given s = "catsanddog", dict = ["cat", "cats", "and", "sand", "dog"], the solution is ["cats and dog", "cat sand dog"].
// Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each
// word is a valid dictionary word. Return all such possible sentences. For example, given:
// s = "catsanddog", dict = ["cat", "cats", "and", "sand", "dog"], the solution is ["cats and dog", "cat sand dog"].
// need to track the actual matched words
var wordBreak = function (string, dict) {
//create an array of ArrayList<String>
var dp = [string.length + 1];
dp[0] = [];
@keif
keif / unique
Created April 24, 2015 04:52
Old School vs. New School: Double for loop vs. a dictionary method for getting the unique values from array.
// old school double loop
// quadratic O(n^2) inefficiency
var unique = function(array) {
var arr = [],
len = array.length;
for (var idx = 0; idx < len; i++) {
for(var jdx = i + 1; jdx < len; jdx++) {
if (array[idx] === array[jdx]) {
j = ++i;
@keif
keif / osx-for-hackers.sh
Created November 10, 2015 06:50 — forked from brandonb927/osx-for-hackers.sh
OSX for Hackers: Yosemite Edition. This script tries not to be *too* opinionated and any major changes to your system require a prompt. You've been warned. Also, please don't email me about this script, my poor inbox...
#!/bin/sh
# Alot of these configs have been taken from the various places
# on the web, most from here
# https://github.com/mathiasbynens/dotfiles/blob/master/.osx
# Set the colours you can use
black='\033[0;30m'
white='\033[0;37m'
red='\033[0;31m'
// variables for panel splitter;
var panelSplitterWidth = {
defaultW: null,
currentW: null
};
// fires on mousedown on panel splitter draggable area
var panelSplitterClick = function(event) {
var $dragPanel = $(event.Event.target).closest("td:not('.af_panelSplitter_horizontal-icon-container')");
@keif
keif / caskconfig.sh
Last active February 24, 2016 14:06
#!/bin/sh
# Install Homebrew
which -s brew
if [[ $? != 0 ]] ; then
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
else
brew update
fi