Skip to content

Instantly share code, notes, and snippets.

View chaoxu's full-sized avatar
😀

Chao Xu chaoxu

😀
  • University of Electronic Science and Technology of China
  • Chengdu, Sichuan, China
View GitHub Profile
@chaoxu
chaoxu / texfilter.php
Created July 14, 2011 00:28
texfilter php
<?php
function texfilter($text){
//text mode matches
$s[] = '/\\\\begin{theorem}\\[(.*?)\\]\\s/';
$r[] = '<blockquote class="theorem"><strong>Theorem ($1)</strong> ';
$s[] = '/\\\\begin{lemma}\\[(.*?)\\]\\s/';
$r[] = '<blockquote class="theorem"><strong>Lemma ($1)</strong> ';
$s[] = '/\\\\begin{problem}\\[(.*?)\\]\\s/';
$r[] = '<blockquote class="problem"><strong>Problem ($1)</strong> ';
$s[] = '/\\\\begin{corollary}\\[(.*?)\\]\\s/';
@chaoxu
chaoxu / Contest2011.dyalog
Created August 22, 2011 15:57
Solution for Dyalog competition in 2011
:Namespace Problem1
⎕IO ⎕ML ⎕WX ⎕RL←1 0 0 1093457855
adjacency←{
a←(⍺[;1]⍳⍵) ⍝ edge indexed by numbers
mat←(2⍴(⍴⍺[;1]))⍴0 ⍝ create a empty matrix
mat[↓a⍪⌽a]←1 ⍝ set adjacent edges to 1
mat
}
import Data.List
rec :: Num a => [a] -> [a] -> [Int] -> [a]
rec c b m = a
where a = c++rest
rest = next [] 0 m
next xs k (m:ms)
| k == m = next (a:xs) k ms
| otherwise = val ++ next (map tail xs) (k+1) (m:ms)
where val = if (k<length c) then [] else [sum $ zipWith (*) (reverse (map head xs)) b]
@chaoxu
chaoxu / PartitionNumbers.hs
Created December 6, 2011 13:05
Partition number in Haskell, using https://gist.github.com/1438136
integers = 0 : concat [[x,(-x)] | x <- [1..]]
generalizedPentagonalNumbers = [(3 * n^2 - n) `div` 2|n<-integers]
partitionNumbers = rec [1] (cycle [1,1,-1,-1]) (tail generalizedPentagonalNumbers)
@chaoxu
chaoxu / flavourIceCream.py
Created December 15, 2011 06:24
icecream
import cgi
form = cgi.FieldStorage()
# Create the "head" of our web page
print "Content-type: text/html"
print
print """<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
@chaoxu
chaoxu / Flushing.java
Created August 9, 2012 22:40
Flushing Coins
import java.io.*;
import java.util.*;
public class Flushing {
int[] payment; //what each person must pay
int[][] c; //ith person have c[i][j] number of d[j] coins
int[] d = {1,5,10,20}; //the value of coins can take. It is assumed the denominator are sorted
int[] capacity; //sum of each person's coins*denominator - payment
int n; //number of people;
public int[][] put;
@chaoxu
chaoxu / Coins.java
Created August 18, 2012 20:42
Flushing Coins
package FlushingCoin;
import java.util.ArrayList;
import java.util.*;
public class Coins {
int[] d;
int[] c;
int[][] k;
int[][] x;
import Data.Array
lcs :: String -> String -> Int
lcs s t = a!(n,m)
where a = array ((0,0),(n,m)) [((x,y), f x y)|x<-[0..n],y<-[0..m]]
n = length s
m = length t
s'= array (0,n-1) $ zip [0..n-1] s
t'= array (0,m-1) $ zip [0..m-1] t
f :: Int->Int->Int
f i j
@chaoxu
chaoxu / lcs.hs
Created December 15, 2012 06:24 — forked from srayuws/lcsIO.hs
--ST Monad instead!
lcs :: (Eq a) => [a] -> [a] -> [a]
lcs s' t' = reverse $ build n m
where
a = runST $ do
b <- newArray ((0,0),(n,m)) 0
mapM_ (f b) $ range ((0,0),(n,m))
unsafeFreeze b
n = length s'
m = length t'
@chaoxu
chaoxu / spiral.hs
Last active December 13, 2015 20:28
Given a 2D array, print out(find the list) all the elements by a spiral from the outside. The array need to have dimension at least 1x1.
import Data.List
spiral xs = (take (n*m) . concat . concat . transpose) $ zipWith (build . ($ xs)) f v
where f = [id, init . reverse . transpose . tail, map (reverse . init) . reverse, map reverse . transpose . init]
v = [n,m-1,n-1,m-2]
n = length (head xs)
m = length xs
build s t = zipWith (\x l-> take (t-2*x) . drop x $ l) [0..] s