Skip to content

Instantly share code, notes, and snippets.

View ejamesc's full-sized avatar

Cedric Chin ejamesc

View GitHub Profile
@ejamesc
ejamesc / gist:1042420
Created June 23, 2011 12:01
Generate bit.ly urls in Python
import urllib
try:
import json
except ImportError:
import simplejson as json
#Enter your bitly username and apikey here
BITLY_USER = 'xxxxxx'
BITLY_API = 'xxxxxxxxxxxxxxxxxxxxxxxxx'
@ejamesc
ejamesc / gist:1197655
Created September 6, 2011 14:15
CS2104 Vanilla Assembly Language Linked List
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
/* gobal declarations */
int eax, ebx, ecx, edx, esi, edi;
unsigned char M[10000];
int a[5];
/* prototypes */
@ejamesc
ejamesc / gist:1203972
Created September 8, 2011 17:17
Ugliest Interpreter Known To Man
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* Structs */
typedef struct Variable
{
char* identifier;
int value;
struct Variable* next;
@ejamesc
ejamesc / gist:1214138
Created September 13, 2011 15:38
Operand matching
/* End cases, where str is atom or number */
count(Str, Op, 0) :- atom(Str).
count(Str, Op, 0) :- number(Str).
/* Program */
count(Str, Op, R) :- Str =..[A,B,C],
test(A, Op, R1),
count(B, Op, R2),
count(C, Op, R3),
R is R1 + R2 + R3.
@ejamesc
ejamesc / gist:1215160
Created September 13, 2011 21:01
Differentiation in Prolog
/* 6 differentiation rules */
derive(X, X, 1).
derive(In, X, 0) :- atomic(In).
derive(In1+In2, X, Res1 + Res2) :- derive(In1, X, Res1), derive(In2, X, Res2).
derive(In1-In2, X, Res1 - Res2) :- derive(In1, X, Res1), derive(In2, X, Res2).
derive(In1*In2, X, In1 * Res1 + Res2 * In2) :- derive(In1, X, Res2), derive(In2, X, Res1).
derive(In1/In2, X, (In2 * Res1 - In1 * Res2)/In2*In2 ) :- derive(In1, X, Res1), derive(In2, X, Res2).
@ejamesc
ejamesc / gist:1215533
Created September 14, 2011 00:13
Incomplete, slightly buggy algebraic simplification in Prolog
simplify(X,Res) :- simple(X,Z), simple(Z,Res), !.
simple(X,X) :- atomic(X), ! .
simple(In, Res) :- In =.. [Ope, A],
simple(A, C), unary(Ope,C,Res).
simple(In, Res) :- In =.. [A,B,C],
simple(B, Res1),
simple(C, Res2),
simple(A, Res1, Res2, Res).
@ejamesc
ejamesc / gist:1283621
Created October 13, 2011 07:06
Stupid Haskell Insert Using foldr
-- PS5 Problem 1
insert a pos l =
fst ( foldr
(\x (b,c) -> (if c==pos-1 then a:x:b else x:b, c+1 ))
((drop pos [a]),0) l)
@ejamesc
ejamesc / gist:1288232
Created October 14, 2011 20:25
Gravatar Retrieval
def gravatar_url(email, size=80):
"""Return the gravatar image for the given email address."""
return 'http://www.gravatar.com/avatar/%s?d=identicon&s=%d' % \
(md5(email.strip().lower().encode('utf-8')).hexdigest(), size)
@ejamesc
ejamesc / gist:1372562
Created November 17, 2011 07:04
Semaphore PS
"""
This simulation program works as follows:
The Task class contains a list of tasks to run, and tracks block state.
The Task.next() function exposes the next function of the generator tasks it contains.
When a block is called (via Task.block(name_of_task)) all other tasks except for
name_of_task will be added to the internal Task blocklist.
Task.next() will skip over any tasks currently on the blocklist
during Round-Robin iteration.
Task.release_block() clears the block list, thus allowing all other tasks to continue running
@ejamesc
ejamesc / gist:1373187
Created November 17, 2011 13:54
Sieve
"""Translated from Haskell:
let sieve(p:xs) = p : sieve (filter (\ x -> x `mod` p /= 0) xs) in sieve [2..]
"""
from itertools import ifilter
def ints(k):
yield k
while True:
yield k