Skip to content

Instantly share code, notes, and snippets.

View hzhu's full-sized avatar
🍵
building matcha.xyz

henryzhu.eth hzhu

🍵
building matcha.xyz
View GitHub Profile
@hzhu
hzhu / LOYAL3-hearts-II.markdown
Created November 12, 2013 06:07
A Pen by Henry Zhu.
@hzhu
hzhu / reduce.js
Last active May 2, 2019 14:15
Reduce ES5 / ES6
// reduce boils down a list of values into a single value
// var callback = function (previousValue, currentValue, index, array) {
// return previousValue + currentValue
// }
// [].reduce(callback, initialValue)
//
// Reduce invokes the callback on each item in the array.
// On the first invocation, the first item is passed to the callback as currentValue.
// and the initialValue is passed as the previousValue.
// The returned value of that is then passed into the next invocation of the second item as previousValue.
@hzhu
hzhu / .js
Last active September 24, 2015 21:56
Data Transformation
var x = [ {node: "ip2", app: "dbsyncer", containerId: "1234"},
{node: "ip2", app: "logforwa", containerId: "3423"},
{node: "ip4", app: "dbsyncer", containerId: "2213"},
{node: "ip4", app: "logforwa", containerId: "3434"} ]
var newObj = {};
x.forEach(function (item) {
newObj[item.node] = newObj[item.node] || [];
@hzhu
hzhu / .cljs
Last active November 10, 2015 23:25
Reagent Register document.readyState
(defn register-dom-complete []
(aset js/document "onreadystatechange"
(fn [x]
(let [ready-state (.-readyState js/document)]
(if (= ready-state "complete")
(js/alert "DOM has completely loaded: invoke your callbacks here.")
)))))
@hzhu
hzhu / .cljs
Created November 10, 2015 23:39
Reagent CSSTransitionGroup on elements that are not in a list
(ns animation.core
(:require [reagent.core :as reagent]))
(enable-console-print!)
(def css-transition-group
(reagent/adapt-react-class js/React.addons.CSSTransitionGroup))
(def style
".my-item {
@hzhu
hzhu / docker.sh
Last active April 25, 2017 04:46
Docker Commands
# Remove Docker Images
docker rmi $(docker images -q)
docker rmi <img id>
# Remove Docker All Containers.
docker rm $(docker ps -a -q)
# Build Docker Image
docker build - < Dockerfile
doccker build -t <img name> </path/to/Dockerfile>
@hzhu
hzhu / nginx.conf
Created November 25, 2015 17:33
nginx.conf backup
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
@hzhu
hzhu / .js
Created April 29, 2016 20:55
////////////////////////
// Solution #1 -- Adnan
////////////////////////
function pairs (obj) {
var arr = [], key
for (key in obj) arr.push([key, obj[key]])
return arr
}
var string = document.body.innerText
var counts = {}, order
@hzhu
hzhu / .js
Created June 22, 2016 19:33
occurances of a string
// Given a string of text, output the characters ordered from
// highest to lowest, with each character paired with
// the number of times it occurs.
function displayCharOccuraces(text) {
window.charObj = {}
window.arr = []
for (var i = 0; i < text.length; i++) {
if (!charObj[text[i]]) {
@hzhu
hzhu / stock_prices.md
Last active March 5, 2017 08:03
Interview Cake: Stock Prices

The Problem

Suppose we could access yesterday's stock prices as an array, where:

  • The indices are the time in minutes past trade opening time, which was 9:30am local time.
  • The values are the price in dollars of Apple stock at that time. So if the stock cost $500 at 10:30am, stockPricesYesterday[60] = 500.

Write an efficient function that takes stockPricesYesterday and returns the best profit I could have made from 1 purchase and 1 sale of 1 Apple stock yesterday.