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
// Reverse words in a string | |
var reverseWords = function(sentence){ | |
var words = sentence.split(" ").reverse(); // Split the sentence into an array of words and reverse it | |
var string = ""; | |
for(word in words) | |
string += (word > 0 ? " " : "") + words[word]; // Concatenate each word to the output and add spaces where required | |
return string; | |
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
// Make new flatDict and return it | |
function flattenDictionary(dict) { | |
const go = (dict, initialKey, flatDict) => { | |
for (const key of Object.keys(dict)) { | |
const value = dict[key]; | |
const flatKey = initialKey ? `${initialKey}.${key}` : key; | |
// When the value is a primitive | |
if (typeof value !== 'object') flatDict[flatKey] = value; |
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
''' | |
Interview hack: Memorize preorder/inorder/postorder tree ITERATORS (no recursion) and their reverses. | |
It simplifies a disproportionate number of questions to simple for loops (see below). | |
I consider the implementations below the simplest way to memorize the iterative tree traversal algorithms, | |
because they are so similar to each other, and to their respective recursive versions. | |
Notes: | |
- We only visit a node after we have expanded its children (i.e. added them to the stack) in the desired order. | |
- `x is curr` does the expanded flagging for us, because we always expand the current node. |
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
#!python | |
class Node(object): | |
def __init__(self, data): | |
"""Initialize this node with the given data""" | |
self.data = data | |
self.next = None | |
def __repr__(self): |
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
#!python | |
from linkedlist import LinkedList, Node | |
import unittest | |
class NodeTest(unittest.TestCase): | |
def test_init(self): | |
data = 'ABC' |
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
#!python | |
from linkedlist import LinkedList, Node | |
import unittest | |
class NodeTest(unittest.TestCase): | |
def test_init(self): | |
data = 'ABC' |