Skip to content

Instantly share code, notes, and snippets.

View cwhy's full-sized avatar

Chen Yu cwhy

View GitHub Profile
#include<stdio.h>
void main(){
return 0;
}
@cwhy
cwhy / R_essentials.R
Last active September 29, 2015 08:40
CWhy's R starter file
rm(list = ls())
# Set SPSS-like contrast
options(contrasts=c(unordered="contr.sum", ordered="contr.poly"))
# plug-in operators
`%+%` <- function(a, b) paste(a, b, sep="")
`%beginwith%` <- function(s.main, s.sub){
bl <- nchar(s.sub)
return(substr(s.main,1,bl)==s.sub)
}
@cwhy
cwhy / SuperDualNumBros.py
Created December 23, 2014 03:51
SuperDualNumBros
#!/usr/bin/python
# SuperDualNumBros v0.9 by CWhy@guokr
# Python3
from math import ceil, sqrt
Min, Max = 2, 99
def getSameSum(nums):
x, y = nums
@cwhy
cwhy / 3Dtest.m
Created March 13, 2015 03:26
Matlab 3D testing vectorization
% Use case:
% we have test function m = f(x, y, z), where we vary x, y, and z to get different ms
% we want the result M where M(x, y, z) = f(x, y, z)
% but vectorized with one for loop
% Example: xs = 1:2
% ys = 3:5
% zs = 6:9
[X,Y,Z] = meshgrid(xs, ys, zs);
test_instances = [X(:) Y(:) Z(:)];
@cwhy
cwhy / realTimePlot.m
Created August 6, 2015 13:14
Multichannel Real Time Plot for Matlab
close all;
f = figure('Name', 'Visualization','Color', [1 1 1]);
set(f, 'menubar', 'none');
ax = axes();
hold(ax,'on')
set(ax, 'Unit', 'normalized');
set(ax, 'Position', [0.07 0.05 0.9 0.9])
import numpy as np
import numpy.random as rd
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
def get_shells(center, radius, N):
# from http://stats.stackexchange.com/questions/7977
r = radius
@cwhy
cwhy / windows_file_operation.py
Created October 24, 2015 10:59
Example for Windows file operation using Python
import os
import glob
import errno
import pandas as pd
import subprocess as sp
si = sp.STARTUPINFO()
si.dwFlags |= sp.STARTF_USESHOWWINDOW
<a href="javascript:{_l=window.location;window.location='http://'+_l.hostname+'.ezlibproxy1.ntu.edu.sg/'+_l.pathname;};">NTU Libraries Proxy Bookmarklet</a>
@cwhy
cwhy / classDict.py
Created March 14, 2016 05:32
Class-like dictionary in python
# It is not very pythonic but I don't like a lot of [[[['''']]]]'''' symbols
class Fake_dict:
def __iter__(self):
for attr in dir(Fake_dict):
if not attr.startswith("__"):
yield attr
(defn argmax [l] (key (apply max-key val (into {} (map-indexed vector l)))))