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
var externalObj = {key: 'value'}; | |
var items = { | |
obj: { | |
'string prop': 'string val', | |
5: 10, | |
nested: [[3, [5, 2]]], | |
'function': function(){return true;}, | |
reference: externalObj | |
}, |
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
class Node: | |
def __init__(self, val = None): | |
self.value = val | |
self.next = None | |
def printValues(self): | |
n = self | |
values = [n.value] | |
while n.next: |
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
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) |
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
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 |
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
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]; |
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
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 | |
}; |
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
var Trie = function() { | |
this.children = {}; // Like {'2': <ref to Trie instance>, '3': ...} | |
this.words = []; // Like [['award', 10764], ['aware', 6625]] | |
}; |
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
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); |
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 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; } |
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 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. |
OlderNewer