Skip to content

Instantly share code, notes, and snippets.

/**
* @param {number[][]} matrix
* @param {number} target
* @return {boolean}
*/
var searchMatrix = function(matrix, target) {
if (matrix.length === 0) return false
let filteredRows = getFilteredRows(matrix, target)
let filteredCols = getFilteredCols(matrix, target)

read plist -> xml file

defaults read -app Terminal | plutil -convert xml1 -o plist.xml -r -- -

@albertywu
albertywu / bash.md
Last active May 21, 2020 20:51
bash toolkit

Map

replaces lines with a substring match of that line (capture groups)

sed -E 's/regex/string/'

echo "a secret: 12345" | sed -E 's/a secret: (.*)/\1/'

(outputs 12345)

@albertywu
albertywu / unix.sh
Last active August 13, 2019 04:50
unix toolbelt cheatsheet
# filter rows in a file by line number / position
awk
# filter rows in a file by regex
grep
# filter cols in a file
awk
# transform rows by regex
@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
@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 / 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 / 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 / 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 / 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