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 / updating-external-data-when-props-changes-using-promises.js
Created October 25, 2018 02:48 — forked from bvaughn/updating-external-data-when-props-changes-using-promises.js
Example for loading new external data in response to updated props
// This is an example of how to fetch external data in response to updated props,
// If you are using an async mechanism that does not support cancellation (e.g. a Promise).
class ExampleComponent extends React.Component {
_currentId = null;
state = {
externalData: null
};
@hzhu
hzhu / core_actions.csv
Last active July 12, 2018 09:52
Sample of Core Events & Actions
User Action API Event
Sally creates an account /api/account/create “Account Created”
Then searches for a product /api/search “Products Searched”
Then adds the product to their cart /api/cart/update “Product Added”
Then adds a payment method /api/credit_cards “Payment Info Entered”
Then pays for her product /api/orders/new “Order Completed”
@hzhu
hzhu / dedup_neighbors.md
Last active February 26, 2017 07:46
Remove Neighboring Duplicates

Problem

Write a function that accepts a string argument and removes duplicate characters that are neighbors. If the new string also has duplicate neighbors, those should be removed also.

reduce_dups('abb') => "a"
reduce_dups('abba') => ""
reduce_dups('aba') => "aba"

Analysis of Problem

@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.

@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 / .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 / 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 / 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 / .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 / .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.")
)))))