Skip to content

Instantly share code, notes, and snippets.

class ProfitCalculator {
constructor(prices) {
this.prices = prices
}
// implementation 1 uses standard for-loop
getMaxProfit() {
let maxProfit = 0
let minPrice = this.prices[0]
const returnedDrones = {}
function dispatchDrones(drones) {
drones.forEach(d => {
droneId = d.dispatch() // droneId contains a random but unique id
returnedDrones[droneId] = false
}
}
// run this function at end of day
// assumes the passed-in array contains a single non-duplicate. Finds the non-duplicate.
// O(3n) = O(n) runtime
findNonduplicate = (numbers) => {
const count = {}
// N
numbers.forEach(n => count[n] = count[n] ? count[n] + 1 : 1)
return (Object
.keys(count)
class IntWithPow(val i: Int) {
def pow(n: Int) = Math.pow(i, n).toInt
}
implicit def intToInt(i: Int) = new IntWithPow(i)
// z = ax + by + c
type LinSolution = (Int, Int, Int)
val testRange = 0 to 5
@albertywu
albertywu / derived collection methods.js
Last active March 28, 2017 18:59
derived collection methods
// given: Array.prototype.forEach, derive:
// 1. reduce
// 2. concat
// 3. map
// 4. filter
// 5. flatten
// 6. flatMap
const reduce = (xs, f, z) => {
var acc = z
@albertywu
albertywu / badApiData.scala
Last active April 3, 2017 21:57
custom play json Reads[X]
// The "favoriteNumber" key returns a value that Int | String. Normalize the response.
val json1 = Json.parse(
"""
|{
|"name": "Albert",
|"age": 32,
|"favoriteNumber": 42
|}
""".stripMargin)
@albertywu
albertywu / jazzygif.sh
Last active October 8, 2018 04:55
jazzygif... convert .mov -> animated .gif on Mac OS X without any 3rd party installs
# add to your ~/.bash_profile or startup script
jazzygif () { ffmpeg -i "$1" -vf scale=-1:-1 -r 10 -f image2pipe -vcodec ppm - | convert -delay 5 -layers Optimize -loop 0 - "$2"; }
# here's how you run it (assumes that pug.mp4 is in current working directory)
jazzygif pug.mp4 pug.gif
# or, specify paths to files
jazzygif ~/Desktop/pug.mp4 ~/Documents/pugs/pug.gif
@albertywu
albertywu / scaffold.sh
Last active December 12, 2018 18:52
scaffold a node project with babel + flow
#!/usr/bin/env bash
set -ex
PROJECT=$1
mkdir $PROJECT && cd $PROJECT
yarn init -y
yarn add -D @babel/cli @babel/core @babel/preset-env @babel/preset-flow
yarn add @babel/polyfill
echo "{\"presets\": [\"@babel/preset-env\", \"@babel/preset-flow\"]}" | jq >> .babelrc
mkdir src
@albertywu
albertywu / requestBuilder.ts
Created February 24, 2019 06:43
Builder Pattern (Typescript)
type Methods = 'get' | 'post';
interface Req {
data: object;
method: Methods;
url: string;
foo?: number
}
class Req implements Req {
@albertywu
albertywu / fraud.ts
Last active July 16, 2019 14:02
Fraud Detector
/*
Notes:
DataSource
- a stream of data
- hasNext(), getNext()
- examples: Location, Transactions, Contacts, ...
- an algorithm's output is a DataSource
Transaction