Skip to content

Instantly share code, notes, and snippets.

@phg1024
phg1024 / read_numbers.hs
Created November 2, 2015 03:03
Read several numbers
main = do
n <- readLn :: IO Int
nums <- forM (take n [1..]) (\x -> do
readLn :: IO Int)
print nums
@phg1024
phg1024 / eigen_matlab.cpp
Created October 28, 2015 00:12
Eigen-MATLAB reference
// A simple quickref for Eigen. Add anything that's missing.
// Main author: Keir Mierle
#include <Eigen/Dense>
Matrix<double, 3, 3> A; // Fixed rows and cols. Same as Matrix3d.
Matrix<double, 3, Dynamic> B; // Fixed rows, dynamic cols.
Matrix<double, Dynamic, Dynamic> C; // Full dynamic. Same as MatrixXd.
Matrix<double, 3, 3, RowMajor> E; // Row major; default is column-major.
Matrix3f P, Q, R; // 3x3 float matrix.
@phg1024
phg1024 / bfs_recursive.py
Created October 20, 2015 15:46
Recursive BFS
class Node(object):
def __init__(self, val, leftchild=None, rightchild=None):
self.val = val
self.left = leftchild
self.right = rightchild
def bfs_iterative(tree, visitor):
q = []
q.append((tree, 0))
@phg1024
phg1024 / simple_vector.cpp
Created October 17, 2015 07:05
Simple vector
#include <iostream>
#include <vector>
using namespace std;
const int trunk_size = 8 * 1024 * 1024;
class A {
public:
static int counter;
A() { id = counter++; cout << "alloc " << id << " @ " << this << endl; trunk.resize(trunk_size); }
@phg1024
phg1024 / nextperm.cpp
Created June 1, 2015 00:46
Sample implementation of next permutation
#include <iostream>
#include <algorithm>
using namespace std;
template <typename Container>
bool nextperm(Container &s) {
using T = Container;
typename T::iterator first = s.begin();
typename T::iterator last = s.end() - 1;
typename T::iterator i = last;
@phg1024
phg1024 / cachetest.cpp
Last active August 29, 2015 14:15
effect of cache demonstration
#include <iostream>
#include <time.h>
#include <vector>
using namespace std;
int main(int argc, char **argv) {
const int MAX = 10000;
vector<vector<double>> A(MAX, vector<double>(MAX));
vector<double> x(MAX), y(MAX);
for(int i=0;i<MAX;++i)
for /l %%x in (11, 1, 27) do (
echo 0%%x
:: do something here
)
#include <iostream>
#include <vector>
#include <stack>
#include <stdlib.h>
#include <algorithm>
#include <cfloat>
using namespace std;
struct Point {
Point():x(0), y(0){}
vertices :: Integer -> Integer -> Integer -> [(Integer, Integer)]
vertices 1 w h = [(quot (w+1) 2, h)]
vertices n w h = foldl (\acc v -> acc ++ [((fst v) - dx, snd v), (fst v, (snd v) - dy), ((fst v) + dx, snd v)]) [] p
where p = vertices (n-1) w h
dx = quot (w+1) (2^n)
dy = quot h (2^(n-1))
inrange :: Integer -> Integer -> Integer -> Bool
inrange 0 x y = abs (x - 32) < y
inrange n x y = inrange (n-1) x y && (foldl (\acc v -> acc && (not (abs (x-(fst v)) <= (snd v) - y && (snd v) - y < dy))) True verts)
@phg1024
phg1024 / test_unique_ptr.cpp
Created July 15, 2014 19:54
demo use of unique pointer
#include <iostream>
#include <vector>
#include <memory>
using namespace std;
struct dummy_t {
dummy_t(){}
dummy_t(int v):val(v){
cout << "dummy " << val << " constructred." << endl;
}