This gist is part of a blog post. Check it out at:
http://jasonrudolph.com/blog/2011/08/09/programming-achievements-how-to-level-up-as-a-developer
" 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] |
/* | |
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. |
This gist is part of a blog post. Check it out at:
http://jasonrudolph.com/blog/2011/08/09/programming-achievements-how-to-level-up-as-a-developer
#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 |
/* 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); |
#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; |
/* | |
* 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 |
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) |
/* 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); |
/* 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; |