Skip to content

Instantly share code, notes, and snippets.

View jeffchiucp's full-sized avatar
🎯
Focusing

Jeffrey jeffchiucp

🎯
Focusing
View GitHub Profile
@jeffchiucp
jeffchiucp / reverseWords.js
Created April 3, 2018 18:23 — forked from willread/reverseWords.js
Reverse the words in a string
// 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;
// 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;
@jeffchiucp
jeffchiucp / tree_iterators.py
Created December 4, 2017 09:34 — forked from alexbowe/tree_iterators.py
Method to simplify many programming interview tree questions.
'''
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.
@jeffchiucp
jeffchiucp / linkedlist.py
Created October 31, 2017 19:18 — forked from anonymous/linkedlist.py
linkedlist python implementation
#!python
class Node(object):
def __init__(self, data):
"""Initialize this node with the given data"""
self.data = data
self.next = None
def __repr__(self):
@jeffchiucp
jeffchiucp / LinkedList_test.py
Created October 31, 2017 19:17 — forked from anonymous/LinkedList_test.py
comprehensive test for LinkedList_test.py
#!python
from linkedlist import LinkedList, Node
import unittest
class NodeTest(unittest.TestCase):
def test_init(self):
data = 'ABC'
@jeffchiucp
jeffchiucp / LinkedList_test.py
Created October 31, 2017 19:17 — forked from anonymous/LinkedList_test.py
comprehensive test for LinkedList_test.py
#!python
from linkedlist import LinkedList, Node
import unittest
class NodeTest(unittest.TestCase):
def test_init(self):
data = 'ABC'