Skip to content

Instantly share code, notes, and snippets.

View zheplusplus's full-sized avatar

Zhe Lin zheplusplus

View GitHub Profile
@zheplusplus
zheplusplus / markdown_split.js
Created June 4, 2013 08:53
Markdown split sample regex
/* inline codes are within two ` */
'a``b``c``d`e``f'.split(/``((?:[^`]|`[^`])*)``/)
/* ["a", "b", "c", "d`e", "f"] */
@zheplusplus
zheplusplus / test.js
Last active December 20, 2015 08:39
Automation based tokenizer
var tknz = typeof require === 'undefined' ? window.tokenizer
: require('./tokenizer');
var t = tknz.Tokenizer();
t
.simpleSymbols('=+-*/%<>!.', 'operator')
.simpleSymbols('(', 'op_paren')
.simpleSymbols(')', 'cl_paren')
.simpleSymbols('{', 'op_brace')
.simpleSymbols('}', 'cl_brace')
.simpleSymbols(';', 'semicolon')
@zheplusplus
zheplusplus / cartesian_product.js
Created October 24, 2013 03:34
Cartesian product
function cartesianProduct(sets) {
return sets.reduce(function(items, nextSequence) {
return nextSequence.map(function(element) {
return items.map(function(item) {
var copyItem = item.slice(0);
copyItem.push(element);
return copyItem;
});
}).reduce(function(sum, current) {
return sum.concat(current);
@zheplusplus
zheplusplus / keeprun.c
Last active December 27, 2015 10:49
Keep a process running, and recover it after terminated. 我们不用很麻烦也能让一个进程持续跑起来.
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
int main(int argc, char* argv[])
{
if (argc == 1) {
fprintf(stderr, "Usage:\n");
fprintf(stderr, "keeprun prog arg0 arg1 ...");
return 1;
# encoding=utf-8
# from http://bbs.eve-china.com/thread-583164-1-1.html
SHIP_REPROCESSING = {
'Incursus': {
u'三钛合金': 11528,
u'类晶体胶矿': 9882,
u'类银超金属': 3656,
u'同位聚合体': 7,
import sys
import os
import tempfile
import uuid
import subprocess
import collections
def parse_seq(sequence):
Segment = collections.namedtuple('Segment', 'epnum,start,duration,subt')
result = []
@zheplusplus
zheplusplus / tukasans.py
Last active August 29, 2015 13:59
つかさンス ASCII Art Font
import sys
import json
FIXED_WIDTH = len('_________')
FIXED_HEIGHT = 6
def map_characters(string, typo_group):
return {
string[col]: [typo_group[row][col * (1 + FIXED_WIDTH):
@zheplusplus
zheplusplus / group.py
Created July 2, 2014 10:16
Mahjong Waits Algorithm
class Group(object):
pass
class Sequence(Group):
def __init__(self, suit, starting_rank):
self.suit = suit
self.starting_rank = starting_rank
@zheplusplus
zheplusplus / for_each_i.hpp
Last active August 29, 2015 14:10
C++0x for_each with index
template <typename Iterator, typename Function>
Function for_each_i(Iterator begin, Iterator end, Function f)
{
typedef decltype(*begin) value_type;
int i = 0;
return std::move(std::for_each(begin, end, [&](value_type& e) {f(i++, e);}));
}
@zheplusplus
zheplusplus / redis_profiling.py
Created January 16, 2015 06:58
Redis profiling script (random keys)
import sys
import uuid
import random
from datetime import datetime
import redis
prefix = str(uuid.uuid4())
groups = 20
run_per_group = 1000
total = groups * run_per_group