Skip to content

Instantly share code, notes, and snippets.

View zh4ngx's full-sized avatar

Andy Zhang zh4ngx

  • San Francisco, CA
View GitHub Profile
@zh4ngx
zh4ngx / hill_climb_1
Created July 1, 2017 01:39
Basic Hill Climb with variance reduction for MC Policy Evaluation
import gym
import numpy as np
from gym.wrappers.monitoring import Monitor
env = gym.make('CartPole-v0')
env = Monitor(env, 'tmp/cart-pole-hill-climb-1', force=True)
print("Action space: {0}".format(env.action_space))
print("Observation space: {0}\n\tLow: {1}\n\tHigh: {2}".format(
env.observation_space,
@zh4ngx
zh4ngx / karabiner-import.sh
Created February 10, 2015 19:09
Quick settings for OS X Karabiner
#!/bin/sh
cli=/Applications/Karabiner.app/Contents/Library/bin/karabiner
$cli set general.dont_remap_internal 1
/bin/echo -n .
$cli set remap.commandL2optionL 1
/bin/echo -n .
$cli set general.dont_remap_apple_pointing 1
/bin/echo -n .
@zh4ngx
zh4ngx / interview_debug.py
Last active December 28, 2015 17:39
Simple interview question to test basic debugging & communication skills. May require a live person to answer questions (particularly if the candidate doesn't know Python). TODO: rewrite this in a few languages and put in a repo.
def foo(a, b):
"""Please document me.
What am I supposed to do?
Rename anything you see fit.
Also, I think I have bugs. Please debug me.
"""
bar = {}
for x in a:
bar[x] = True
@zh4ngx
zh4ngx / lexical_closure.go
Created October 28, 2013 08:22
Quick demo/reminder of lexical closures (in Go)
// You can edit this code!
// Click here and start typing.
package main
import "fmt"
func main() {
fmt.Println("Hello, 世界")
f := who()
g := who()
@zh4ngx
zh4ngx / binary_tree_marshal.py
Created October 10, 2013 02:42
Function to marshal binary tree (represented as dict). Done kind of in a hurry (like 20m)....
# Arbitary binary tree (integers)
### 1
### 2 3
### 6 4 5
## {1:{"left":2,"right":{3:{"left":4,"right":5}}}}
## Goal: -> '{1:{left:2,right:{3:{left:4,right:5}}}}'
# 1
# 2 3
## '{1:{left:2,right:3}}'
@zh4ngx
zh4ngx / sudoku.py
Created October 10, 2013 02:36
Quick solution to sudoku checker. Does NOT check if board is correctly completable, only if it's currently correct.
# Check nums for rows, columns and blocks
def check(nums):
num_dict = {}
# assume nums is a list of integers
# placeholder for empty is None
for num in nums:
if not num:
continue
if num in num_dict:
return false
@zh4ngx
zh4ngx / stack_machine_functional.rb
Created October 8, 2013 06:31
Functional-ish version of stack machine
$stack = []
def solution(s)
s.each_char do |c|
case c
when /[0-9]/
$stack.push c
puts c
when '+'
return -1 unless op_nums(lambda { |left, right| left + right })
@zh4ngx
zh4ngx / simple_datastore.rb
Created October 2, 2013 07:59
Simple key-value datastore. Basic data & transaction commands.
## SUMMARY
# Use Hash($db) to maintain state
# Track # open transactions
# Memoize modified vars per transaction ($history)
# Be naught, use globals (not thread-safe)
$db = {}
$transactions = 0
$history = {}
@zh4ngx
zh4ngx / tokenize_string.rb
Created September 26, 2013 08:39
Given string and punctuation, tokenize string on punctuation and whitespace. Done using both split and scan.
def tokenize_query_split query, punctuation
punc_regex = /[#{punctuation}\s]+/
tokens = query.split(punc_regex)
tokens.each do |token|
p token
end
end
@zh4ngx
zh4ngx / fizzbuzz.rb
Created September 26, 2013 08:10
Extensible FizzBuzz
def fizzbuzz(max):
1.upto(max).each do |num|
line = ''
line << 'Fizz' if num % 3 == 0
line << 'Buzz' if num % 5 == 0
line = num if line.empty?
p line
end
end