Skip to content

Instantly share code, notes, and snippets.

View izgzhen's full-sized avatar
🎆
rebuild

Zhen Zhang izgzhen

🎆
rebuild
View GitHub Profile
@izgzhen
izgzhen / reordering.c
Last active August 29, 2015 14:04
Google Code Jam Round 1C 2014 - Problem B. Reordering Train Cars
// Brute force searching algorithm
// I rewrite it into a *C code* with some comments for beginners
// Originally from https://gist.github.com/autekroy/bc5ea2c82fb74a5bb2b5
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
long long ans;
int visit[101], check[500];
@izgzhen
izgzhen / ga.py
Last active February 8, 2017 06:28
Genetic Algorithm
# Genetics Algorithm's Implementation in Python
# How did this algorithm work?
# init -> iterate until certain condition -> output every generation
# iterate: select parents -> crossover -> mutation -> a new generation
#
# Core: How to encode your optimization problem into a string, i.e. chromosome?
# Which selection method would your apply? Tournament selection or roulette selection?
# How to realize the child breeding? Every pair of parent will breed a pair of children, which are the outcome of crossover effect.
@izgzhen
izgzhen / data_GA
Last active August 29, 2015 14:05
Data of GA.py
Running instance:
Population Size: 200
Crossover Threshold Probability: 0.5
Mutation Threshold Probability: 0.2
=============
35
[-254, -260, -284, -213, -174, -130, -103, -102, -102, -106, -80, -75, -61, -53, -50, -55, -48, -41, -49, -51, -30, -31, -25, -25, -14, -23, -8, -19, -17, -8, -9, -8, -19, -11, 0]
Running instance:
@izgzhen
izgzhen / AutoParser.hs
Last active October 22, 2015 15:32
Parse autoconf.mk
-- autoconf parser
module AutoParser where
import Data.List.Extra (splitOn)
import qualified Data.Map as M
isComment ('#':_) = True
isComment _ = False
isBlank = all (`elem` " \n\t\r")
@izgzhen
izgzhen / Main.hs
Last active November 18, 2015 13:40
Basic Template Haskell for Multiplate example described here https://wiki.haskell.org/Multiplate
{-# LANGUAGE TemplateHaskell #-}
import MultiPlateDerive
import Data.Generics.Multiplate
import Data.Functor.Constant
data Expr = Con Int
| Add Expr Expr
| Mul Expr Expr
| EVar Var
use self::GcCell::*;
use self::Action::*;
use std::marker;
#[derive(Clone)]
enum GcCell<T: Trace + ?Sized + Clone> {
Data(T),
Forward(*mut GcCell<T>),
}
import Control.Monad.State
import Data.List (partition)
-- Definitions
data Pattern = PVar Variable | PCon Constructor [Pattern]
newtype Constructor = Constructor { unConstructor :: String } deriving (Eq, Show)
newtype Variable = Variable { unVariable :: String } deriving (Eq, Show)
data Expr = EApp Expr Expr
| EAbs String Expr
| EVar String
| EInt Int
deriving (Show)
-- (λx.x x) (λx.x x)
unterminating = EApp (EAbs "x" (EApp (EVar "x") (EVar "x"))) (EAbs "x" (EApp (EVar "x") (EVar "x")))
type Bucket k v = [(v, [k])]
insert :: k -> v -> Bucket k v -> Bucket k v
insert k v bucket@((v0, ks):bucket')
| v == v0 = (v0, k:ks) : bucket'
| v < v0 = (v, k:[]) : bucket
| otherwise = (v0, ks) : insert k v bucket'
query :: k -> k -> Int -> Bucket k v -> [v]
query k1 k2 n bucket@((v0, ks):bucket') =
trait Hash {
fn hash(&self) -> u64;
}
impl Hash for bool {
fn hash(&self) -> u64 {
if *self { 0 } else { 1 }
}
}