Skip to content

Instantly share code, notes, and snippets.

Avatar

Terence Pae tpae

  • San Francisco, CA
  • Twitter @tpae
View GitHub Profile
View exportjson.js
// Includes functions for exporting active sheet or all sheets as JSON object (also Python object syntax compatible).
// Tweak the makePrettyJSON_ function to customize what kind of JSON to export.
var FORMAT_ONELINE = 'One-line';
var FORMAT_MULTILINE = 'Multi-line';
var FORMAT_PRETTY = 'Pretty';
var LANGUAGE_JS = 'JavaScript';
var LANGUAGE_PYTHON = 'Python';
@tpae
tpae / OfferManager.sol
Last active Mar 7, 2021
Library to help manage multiple offer bids for tokens
View OfferManager.sol
//SPDX-License-Identifier: Unlicense
pragma solidity ^0.7.0;
import "@openzeppelin/contracts/utils/Counters.sol";
import "@openzeppelin/contracts/utils/EnumerableSet.sol";
library OfferManager {
using EnumerableSet for EnumerableSet.UintSet;
using EnumerableSet for EnumerableSet.AddressSet;
using Counters for Counters.Counter;
View MergeSort.js
function MergeSort() { }
MergeSort.prototype.sort = function(values) {
if (values.length < 2) {
return values;
}
var mid = Math.floor(values.length/2);
var left = this.sort(values.slice(0, mid));
var right = this.sort(values.slice(mid, values.length));
@tpae
tpae / LRUCache.js
Created Nov 21, 2016
super simple JavaScript Implementation of LRUCache
View LRUCache.js
// LRUCache.js - super simple JavaScript Implementation
// LRU stands for "Least Recently Used"
// https://en.wikipedia.org/wiki/Cache_replacement_policies#LRU
// -----------------------------------------
function LRUNode(key) {
this.key = key;
this.next = this.prev = null;
}
@tpae
tpae / Trie.js
Created Nov 20, 2016
Trie.js - super simple JavaScript implementation
View Trie.js
// Trie.js - super simple JS implementation
// https://en.wikipedia.org/wiki/Trie
// -----------------------------------------
// we start with the TrieNode
function TrieNode(key) {
// the "key" value will be the character in sequence
this.key = key;
@tpae
tpae / graph.js
Last active Aug 13, 2021
JavaScript implementation of Graph Data Structure
View graph.js
// Implement a Graph
// basic operations:
// - add vertex (node)
// - add edge (node -> node)
function GraphNode(val) {
this.val = val;
this.edges = {};
}
@tpae
tpae / queue.js
Last active Aug 13, 2021
JavaScript implementation of Queue Data Structure
View queue.js
// Implement a Queue using LinkedLists
// we know that queue is LIFO
// operations: enqueue, dequeue
function QueueNode(val) {
this.val = val;
this.next = null;
}
function Queue() {
@tpae
tpae / stack.js
Last active Nov 10, 2016
JavaScript implementation of Stack Data Structure
View stack.js
// Implement a stack using LinkedLists.
// Operations:
// -> push
// -> pop
// -> peek
function StackNode(val) {
this.val = val;
this.next = null;
@tpae
tpae / minHeap.js
Last active Sep 20, 2021
JavaScript implementation of Min Heap Data Structure
View minHeap.js
// Implement a min heap:
// -> insert, extract_min
// property:
// - elements are in ascending order
// - complete binary tree (node is smaller than it’s children)
// - root is the most minimum
// - insert takes O(logn) time
// - insert to the bottom right
@tpae
tpae / BSTree.swift
Last active May 8, 2018
Swift implementation of Binary Search Tree
View BSTree.swift
import Foundation
class Node {
var val: Int
var left: Node?
var right: Node?