Skip to content

Instantly share code, notes, and snippets.

View ejamesc's full-sized avatar

Cedric Chin ejamesc

View GitHub Profile
@ejamesc
ejamesc / gist:8297902
Created January 7, 2014 11:10
Radixsort in Python, for a meetup in Ho Chi Minh City
def digit(num, curr_dig):
diff = pow(10, curr_dig - 1)
num = num / diff
return num % 10
def radix_sort(arr, n):
buckets = [[] for x in range(0, 10)]
curr_dig = 1
@ejamesc
ejamesc / set_time_zone.rb
Last active December 23, 2015 05:49
Setting timezones locally in Ruby
def with_time_zone(tz_name)
prev_tz = ENV['TZ']
ENV['TZ'] = tz_name
yield
ensure
ENV['TZ'] = prev_tz
end
with_time_zone('Asia/Singapore') { Chronic.parse("this week Monday 21:00") }
@ejamesc
ejamesc / gist:5275963
Created March 30, 2013 08:55
.jshintrc file.
{
// See http://www.jshint.com/options/ for in-depth explanations
// Predefined globals that JSHint ignores
"browser" : true, // standard globals like 'window'
"devel" : true, // development globals, e.g. 'console'
"dojo" : true, // dojo toolkit globals
"nonstandard" : true, // widely-adopted globals, e.g. 'escape'
"predef" : [ // extra globals
-(id)initWithTerms:(NSArray*)ts{ // 5 points
// REQUIRES: "ts" satisfies clauses given in the representation invariant
// EFFECTS: constructs a new polynomial using "ts" as part of the representation.
// the method does not make a copy of "ts". remember to call checkRep to check for representation invariant
if (self = [super init]) {
terms = [ts sortedArrayUsingComparator:^NSComparisonResult(id a, id b) {
// RatTerm has a property expt that is an int
int first = [(RatTerm*)a expt];
int second = [(RatTerm*)b expt];
[[37956, "Episode"], [852, "Episode"], [852, "Episode"], [832, "Episode"], [841, "Episode"], [37958, "Episode"], [845, "Episode"], [840, "Episode"], [37957, "Episode"], [839, "Episode"], [844, "Episode"], [37959, "Episode"], [3230, "Episode"], [64135, "Movie"], [849, "Episode"], [71141, "Clip"], [71141, "Clip"], [68063, "Clip"], [71137, "Clip"], [71142, "Clip"], [71142, "Clip"], [70988, "Clip"], [70988, "Clip"], [70885, "Episode"], [71242, "Episode"], [71242, "Episode"], [71242, "Episode"], [70779, "Episode"], [70779, "Episode"], [37955, "Episode"], [37956, "Episode"], [847, "Episode"], [846, "Episode"], [832, "Episode"], [843, "Episode"], [37958, "Episode"], [840, "Episode"], [37957, "Episode"], [839, "Episode"], [37959, "Episode"], [3230, "Episode"], [64135, "Movie"], [64001, "Movie"], [71178, "Clip"], [71178, "Clip"], [71141, "Clip"], [71137, "Clip"], [71137, "Clip"], [71142, "Clip"], [71142, "Clip"], [70988, "Clip"], [70885, "Episode"], [70885, "Episode"], [70885, "Episode"], [71242, "Episode"], [71242, "
@ejamesc
ejamesc / gist:2699562
Created May 15, 2012 06:21
Temp xpath for Viki
//*[contains(@id, 'tools')]/*[contains(@class, 'blurb')][contains(., 'Splash')]/ul/li
@ejamesc
ejamesc / gist:2322002
Created April 6, 2012 18:44
Language Detection
require 'rubygems'
require 'mysql2'
require 'unsupervised-language-detection'
db1 = Mysql2::Client.new(:host => 'localhost', :username =>'xxx', :password => 'xxx', :database => 'sgbeat')
db2 = Mysql2::Client.new(:host => 'localhost', :username =>'xxx', :password => 'xxx', :database => 'sgb_pure')
tweets = db1.query("SELECT * FROM tweets").each do |row|
str = row["tweet"]
puts row["id"]
@ejamesc
ejamesc / gist:2318355
Created April 6, 2012 09:10
Java Socket Readline
/* Crude implementation of a readLine function -> used to get HTTP headers from a socket InputStream
* Then you may get the rest of the binary data by just calling sock.getInputStream().read()
*/
public static String readLine(Socket sock) {
String line = new String();
int c;
try {
while ((c = sock.getInputStream().read()) != '\n') {
line += (char) c;
@ejamesc
ejamesc / gist:2292298
Created April 3, 2012 14:11
Dealing with HTTP Headers in Java
package com.stackoverflow.q2307291;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
public class Test {
@ejamesc
ejamesc / assignment.java
Created March 22, 2012 17:06
Assignment
public StoreAndValue eval(Store s, Environment e) {
StoreAndValue s_and_v = rightHandSide.eval(s, e);
int loc = s.newLocation();
// Be careful of this. It might be wrong, though it works now
e.extendDestructive(leftHandSide, loc);
if (s_and_v.value instanceof BoolValue) {
BoolValue res = (BoolValue) s_and_v.value;
s = s.extend(loc, res);