Skip to content

Instantly share code, notes, and snippets.

" If TOhtml is unavailable or the wrong version, at least handle some
" canonical encoding names in Vim.
elseif nenc =~ 'iso[-_]8859-1'
return 'latin1'
elseif nenc =~ 'iso[-_]8859-2'
return 'latin2'
elseif necn ==? 'gb2312' " necn should be nenc
return 'cp936' " GB2312 imprecisely means CP936 in HTML
elseif nenc =~ '\(cp\|win\(dows\)\?\)-125\d'
return 'cp125'.nenc[strlen(nenc)-1]
@cpylua
cpylua / rbsearch.c
Created April 14, 2011 09:12
binary search on a rotated sorted list
/*
Problem:
An element in a sorted array can be found in O(log n) time via binary
search. But suppose I rotate the sorted array at some pivot unknown to
you beforehand. So for instance, 1 2 3 4 5 might become 3 4 5 1 2.
Now devise a way to find an element in the rotated array in O(log n) time.
Solution:
Binary search can not be applied to unsorted lists, we must somehow find
a way to transform the unsorted list to a "sorted" list.
@cpylua
cpylua / about.md
Created August 10, 2011 01:05 — forked from jasonrudolph/about.md
Programming Achievements: How to Level Up as a Developer
@cpylua
cpylua / ct_assert.c
Created August 12, 2011 01:48
C Compile Time assert
#define COMPILE_TIME_ASSERT(expr) char UNIQUE_NAME[(expr) ? 1 : -1]
#define UNIQUE_NAME MAKE_NAME(__LINE__)
#define MAKE_NAME(line) MAKE_NAME2(line)
#define MAKE_NAME2(line) constraint_ ## line
COMPILE_TIME_ASSERT(sizeof(int) * 8 >= 64);
#ifndef CASSERT
#define CASSERT( exp, name ) typedef int dummy##name [ (exp ) ? 1 : -1 ];
#endif
@cpylua
cpylua / bestbefore.c
Created August 17, 2011 03:22
bestbefore
/* http://www.spotify.com/us/jobs/tech/best-before/ */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
char* process_raw(char *p);
char* process(char *pa, char *pb, char *pc);
char* process4(int *len ,int *num, size_t n);
@cpylua
cpylua / cycle-detect.c
Created December 17, 2011 14:10
CRT random number generator cycle peroid test
#include <stdlib.h>
#include <stdio.h>
/**
* Return the cycle period
*/
int brent_cycle_detect(int seed) {
int power = 1;
int lambda = 1;
int tortoise = seed;
@cpylua
cpylua / AdvancedFileServlet.java
Created December 29, 2011 06:53
This is a modified version to support Chinese characters in file name and mapping uri file name to file-system file name.
/*
* net/balusc/webapp/FileServlet.java
*
* Copyright (C) 2009 BalusC
*
* This program is free software: you can redistribute it and/or modify it under the terms of the
* GNU Lesser General Public License as published by the Free Software Foundation, either version 3
* of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
@cpylua
cpylua / f.py
Created March 16, 2012 14:58
count total number of ones in 1...n
def f(n):
cache = [0]
cnt = 0
for i in xrange(1, n+1):
remainder = i % 10
quotient = i / 10
m = cache[quotient]
if remainder == 1:
m = m + 1
cache.append(m)
@cpylua
cpylua / match.c
Created April 19, 2012 08:37
Here is a quick little wildcard matcher by Arjan Kenter
/* Here is a quick little wildcard matcher by Arjan Kenter: */
int match(char *pat, char *str)
{
switch(*pat) {
case '\0': return !*str;
case '*': return match(pat+1, str) ||
*str && match(pat, str+1);
case '?': return *str && match(pat+1, str+1);
default: return *pat == *str && match(pat+1, str+1);
@cpylua
cpylua / datediff.c
Created April 22, 2012 14:19
computing the day difference between two dates
/* this code is from http://c-faq.com/lib/calendar.br.html */
#define DayOfWeek(d,m,y) (int)(DaysElapsed(d,m,y) % 7)
long DaysElapsed(int d,int m,int y) {
static int cd[]={0,31,59,90,120,151,181,212,243,273,304,334};
long n=365L*(y-1);
if (m<3) y--;
return n+y/4-y/100+y/400+cd[m-1]+d;