Skip to content

Instantly share code, notes, and snippets.

View ichaos's full-sized avatar

Jin Chen ichaos

  • Fudan University
View GitHub Profile
@ichaos
ichaos / chinamap.c
Last active August 29, 2015 14:00
Print China map
/*
* Magic code to print china map.
* The original idea is from International Obfuscated C Code test, check more here.
* http://stackoverflow.com/questions/3533348/how-does-this-code-generate-the-map-of-india
*/
#include <stdio.h>
int main(){int a,b,c; for (b = c = 10; (a =
"-FIGURE?,China *CJ Hello Folks,\
b&M\"D(G*>/#\";,B1:0=12<924;554D\
*76`<W#!?UEZ>YCWEWFTJOP$%CXAX@Z?\
@ichaos
ichaos / duffs_device.c
Created December 5, 2013 03:39
Duffs Device, C implementation
#include <stdio.h>
//duff's device
void duffs_device(const char *src, int length, char *dst) {
int n = (length + 7) / 8;
switch (length % 8) {
case 0: do { *dst++ = *src++;
case 7: *dst++ = *src++;
case 6: *dst++ = *src++;
case 5: *dst++ = *src++;
@ichaos
ichaos / ConsumeNumber.cpp
Last active December 29, 2015 15:29
Google protocol buffer code reading
/**
* Lessons Learned:
* 1. Encapsulate basic and usual operations so that
* we can write code like write English sentence
* 2. Using template, metaprogramming
* 3. readable code !
*/
//Eat one character of special type
template<typename CharacterClass>
@ichaos
ichaos / levenshtein_histogram.cpp
Created October 25, 2013 05:27
check if a string is within a given levenshtein distance to any string in a dictionary
/*
* check if a string is within a given levenshtein distance to any string in a dictionary
* credit: Leo Polovets
* see more on quora: https://www.quora.com/Algorithms/Which-is-the-best-programming-algorithm-that-you-have-ever-created
*/
#include <iostream>
#include <string>
#include <algorithm>
#include <cstdlib>
@ichaos
ichaos / findPrimes.cpp
Created October 4, 2013 11:17
find all the primes from 1 to n quickly.
/* the Sieve of Eratosthenes */
#include <cmath>
#include <vector>
using namespace std;
bool[] findPrimes(int n) {
vector<bool> isPrimes(n + 1, true);
isPrimes[0] = false;
isPrimes[1] = false;
@ichaos
ichaos / customCompare.cpp
Last active December 23, 2015 14:28
c++ custom sort compare function
#include <algorithm>
using namespace std;
struct people {
int s;
int b;
int i;
bool operator<(const people &o) const {
@ichaos
ichaos / isPerfectSquare.cpp
Created September 14, 2013 15:01
Check if a integer is perfect square, i.e. its root is also a integer.
bool isPerfectSquare(int x) {
int sqr = Math.sqrt(x);
return x == sqr * sqr;
}
bool isPerfectSquareFaster(int x) {
if (x < 0) return false;
int h = x & 0xf;
if (h == 0 || h == 0x1 || h == 0x4 || h == 0x9) {

Divide and conquer

The maximum subarray problem

Brute-force: just try every possible pair of buy and sell dates in which buy dates precedes the sell dates
then find the maximum. O(n2)

Divide and conquer

Linear method

@ichaos
ichaos / The-ascent-of-money.md
Last active December 18, 2015 05:39
观影评价. Remarks on video, like film, documentary.

Chapter 1: What's money?
Money is trust. Money means you trust the men or entity who print the papers and
believe later they will receive your papers and
give you the equal value commodity you want.

Chapter 2: What's bond?
It's still trust. And it also means borrowing money from future.
规模和 diversification of risk

@ichaos
ichaos / array_is_not_pointer.c
Last active December 18, 2015 00:19
subtle and funny c code
/**
* Array is different with pointer, although it is used like a pointer.
* Here is an example
*/
#include <stdio.h>
int main() {
int a[5];
printf("%p\n", a);