Skip to content

Instantly share code, notes, and snippets.

Avatar

Andy Zhang zhangandyx

  • San Francisco, CA
View GitHub Profile
@zhangandyx
zhangandyx / hill_climb_1
Created Jul 1, 2017
Basic Hill Climb with variance reduction for MC Policy Evaluation
View hill_climb_1
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,
@zhangandyx
zhangandyx / karabiner-import.sh
Created Feb 10, 2015
Quick settings for OS X Karabiner
View karabiner-import.sh
#!/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 .
@zhangandyx
zhangandyx / interview_debug.py
Last active Dec 28, 2015
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.
View interview_debug.py
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
@zhangandyx
zhangandyx / lexical_closure.go
Created Oct 28, 2013
Quick demo/reminder of lexical closures (in Go)
View lexical_closure.go
// You can edit this code!
// Click here and start typing.
package main
import "fmt"
func main() {
fmt.Println("Hello, 世界")
f := who()
g := who()
@zhangandyx
zhangandyx / binary_tree_marshal.py
Created Oct 10, 2013
Function to marshal binary tree (represented as dict). Done kind of in a hurry (like 20m)....
View binary_tree_marshal.py
# 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}}'
@zhangandyx
zhangandyx / sudoku.py
Created Oct 10, 2013
Quick solution to sudoku checker. Does NOT check if board is correctly completable, only if it's currently correct.
View sudoku.py
# 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
@zhangandyx
zhangandyx / stack_machine_functional.rb
Created Oct 8, 2013
Functional-ish version of stack machine
View stack_machine_functional.rb
$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 })
@zhangandyx
zhangandyx / simple_datastore.rb
Created Oct 2, 2013
Simple key-value datastore. Basic data & transaction commands.
View simple_datastore.rb
## 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 = {}
@zhangandyx
zhangandyx / tokenize_string.rb
Created Sep 26, 2013
Given string and punctuation, tokenize string on punctuation and whitespace. Done using both split and scan.
View tokenize_string.rb
def tokenize_query_split query, punctuation
punc_regex = /[#{punctuation}\s]+/
tokens = query.split(punc_regex)
tokens.each do |token|
p token
end
end
@zhangandyx
zhangandyx / fizzbuzz.rb
Created Sep 26, 2013
Extensible FizzBuzz
View fizzbuzz.rb
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