Skip to content

Instantly share code, notes, and snippets.

@olslash
olslash / LinkedList.py
Created June 19, 2014 17:43
babby's first linked list
class Node:
def __init__(self, val = None):
self.value = val
self.next = None
def printValues(self):
n = self
values = [n.value]
while n.next:
@olslash
olslash / generatedtables
Last active August 29, 2015 14:03
fml chatroom schema
ysql> describe users;
+------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(255) | NO | | NULL | |
| created_at | datetime | NO | | NULL | |
| updated_at | datetime | NO | | NULL | |
+------------+--------------+------+-----+---------+----------------+
4 rows in set (0.01 sec)
var mergeSort = function(array) {
function merge(left, right) {
// merge two lists, which are assumed to be sorted.
left || (left = []);
right || (right = []);
var result = [];
while (left.length > 0 || right.length > 0) {
if (left.length > 0 && right.length > 0) {
// if both lists have values
@olslash
olslash / gist:dd52c242a74f0daaa108
Last active August 29, 2015 14:04
budget observable
var createObservedObject = function(callback) {
var obj = {};
var args = [].slice.call(arguments, 1);
obj.addObservedProperty = function(propertyName) {
obj['_' + propertyName] = undefined;
Object.defineProperty(obj, propertyName, {
get: function() {
return obj['_' + propertyName];
@olslash
olslash / gist:25ca5be554fca1689762
Created August 10, 2014 23:23
T9 blogpost - 1
var keys = {
'a': 2, 'b': 2, 'c': 2,
'd': 3, 'e': 3, 'f': 3,
'g': 4, 'h': 4, 'i': 4,
'j': 5, 'k': 5, 'l': 5,
'm': 6, 'n': 6, 'o': 6,
'p': 7, 'q': 7, 'r': 7, 's': 7,
't': 8, 'u': 8, 'v': 8,
'w': 9, 'x': 9, 'y': 9, 'z': 9
};
@olslash
olslash / gist:96b7fa4440072c925100
Last active August 29, 2015 14:05
T9 blogpost - 2
var Trie = function() {
this.children = {}; // Like {'2': <ref to Trie instance>, '3': ...}
this.words = []; // Like [['award', 10764], ['aware', 6625]]
};
@olslash
olslash / gist:6cc6859e53d19a45e174
Last active August 29, 2015 14:05
T9 blogpost - 3
Trie.prototype.insert = function(word, useFrequency) {
// Traverse the tree to the node where the word should be inserted. If any
// needed nodes do not exist along the way, they are created.
var nodeToAddWord = traverseAddingNodes(this);
// Insert the word into the wordlist of the node returned above. Use the
// data provided (frequency of use in English text) to place the word in
// the correct position, so that we can recommend more common words first.
insertWordIntoListByFrequency(nodeToAddWord.words, word, useFrequency);
@olslash
olslash / gist:edcc6902c80ecb09008d
Last active August 29, 2015 14:05
T9 blogpost - 4
function traverseAddingNodes(node) {
var i = 0, len = word.length;
// Traverse the tree's existing nodes as far as possible.
for(i, len; i < len; i++) {
var thisLetter = word[i];
var thisKey = keys[thisLetter];
if(node.children.hasOwnProperty(thisKey)) {
node = node.children[thisKey];
} else { break; }
@olslash
olslash / gist:1c1b057264874378d225
Last active August 29, 2015 14:05
T9 blogpost - 5
function insertWordIntoListByFrequency(list, word, useFrequency) {
var wordToInsert = [word, useFrequency]; // Store word in a tuple.
var wordsLength = list.length;
if(wordsLength === 0) {
// Handle the case where this node has no words yet
list.push(wordToInsert);
} else {
// Find where to insert this word among others, based on its
// frequency property.
@olslash
olslash / gist:2f372184e78d0ddb48fe
Last active August 29, 2015 14:05
T9 blogpost - 6
Trie.prototype.getSuggestions = function(keyString, suggestionDepth) {
// Traverse the tree based on the key digits in keyString, to find the
// node where relevant words are stored.
var result = [];
var node = this;
for(var i = 0; i < keyString.length; i++) {
var thisKey = keyString[i];
if(!node.children.hasOwnProperty(thisKey)) { break; }
node = node.children[thisKey];