Skip to content

Instantly share code, notes, and snippets.

View yeukhon's full-sized avatar

Yeuk Hon Wong yeukhon

  • TikTok USDS / ByteDance
  • New York, NY
  • 20:48 (UTC -04:00)
View GitHub Profile
@yeukhon
yeukhon / pub.js
Last active August 29, 2015 14:04
Simple example of pub/sub using node.js and redis client
// Usage:
// node pub.js
// then starts typing message (ENTER per message)
// A publisher could be invokved by a background job
// processor or a user triggered action received by
// web app.
var redis = require("redis");
var pub = redis.createClient();
process.stdin.resume();
@yeukhon
yeukhon / IDEAS.md
Last active August 29, 2015 14:05
Hacker School pair programming interview with Yeuk Hon Wong

Environment: I think we will try the coding on Linux. I use Mac but I can just work from a vagrant machine or from an EC2 instance.

robots-txt-scanner takes in a robots.txt file and outputs a list of (token, lexeme) tuple.

print scanner.scan("User-agent: Google\nDisallow: *")
(('\\USER_AGENT_VALUE/', 'User-agent: Google'), ('\\DISALLOW_VALUE/', 'Disallow: *'))

Features:

@yeukhon
yeukhon / foo.js
Created August 23, 2014 18:31
browserify question
var Foo = function () {
alert("Hello! You've just called Foo()");
}
module.exports = Foo
#
# Wide-open CORS config for nginx
#
location / {
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' '*';
#
// api.js
var Job = function(settings, jq) {
var createJob = function(args) {
return $.ajax({
url: "https://yeukhon.me/api/job",
type: "POST",
crossDomain: true,
data: {
job_name: args.job_name,
import re
text1 = "alphanumeric@example.org"
text2 = "alphanumeric@long.subdomain.more.subdomain.domain.org"
r1 = re.compile(r'[a-zA-Z0-9]+@[a-zA-Z]+\.[a-zA-Z]+(\.[a-zA-Z]+)*')
print(r1.match(text1).group())
print(r1.match(text2).group())
print("part 2 - compare with and without (?:")
@yeukhon
yeukhon / README.md
Created September 4, 2014 21:48
How do you know where to "wrap" or "break" Python statement or expression into multiple lines?
stuff = set([ '%s.%s' % (prefix, type) for type in types(suffix) if prefix in get_types(given_type) ])
# how do I know where I can split this onto multiple lines?

and my answer is at the expression.

>>> [x
... for
@yeukhon
yeukhon / list_alloc.py
Last active August 29, 2015 14:06
list extend vs nested loop
"""
>>> a = [[1,2], [3,3], [4]]
>>> o = []
>>> for x in a:
... o.extend(x)
...
>>> o
[1, 2, 3, 3, 4]
import pqdict
def dijkstra(graph, source, target=None):
"""
Computes the shortests paths from a source vertex to every other vertex in
a graph
"""
# The entire main loop is O( (m+n) log n ), where n is the number of
# vertices and m is the number of edges. If the graph is connected
@yeukhon
yeukhon / qselect.py
Last active August 29, 2015 14:06
Quickselect. Find the i-th order statistics in Python using Quicksort's randomized selection. This is a solution for https://www.hackerrank.com/challenges/find-median Note it is probably a better idea to pass the indices around instead of slicing (quicksort is an in-place sorting algorithm). But for simplicity, let's just use slice (which create…
import random
def q_select(numbers, ith):
if len(numbers) < 2:
return numbers[0]
# Chooses a pivot uniformly from random
p_index = random.randint(0, len(numbers)-1)
pivot = numbers[p_index]
# Before the partition, swap the pivot with the last element
numbers[p_index], numbers[-1] = (