Skip to content

Instantly share code, notes, and snippets.

View xiaohanyu's full-sized avatar
🏠
Working from home

Xiao Hanyu xiaohanyu

🏠
Working from home
View GitHub Profile
@xiaohanyu
xiaohanyu / ome-install-package.el
Created October 18, 2013 05:26
Quickly test an el-get package for oh-my-emacs(http://github.com/xiaohanyu/oh-my-emacs)
(add-to-list 'load-path "~/.emacs.d/el-get/el-get")
(unless (require 'el-get nil 'noerror)
(with-current-buffer
(url-retrieve-synchronously
"https://raw.github.com/dimitri/el-get/master/el-get-install.el")
(let (el-get-master-branch
el-get-install-skip-emacswiki-recipes)
(goto-char (point-max))
(eval-print-last-sexp))))
@xiaohanyu
xiaohanyu / convert_encoding.rb
Created February 22, 2014 11:23
Convert file encoding
#!/usr/bin/env ruby
require 'optparse'
require 'pry'
def main
options = {to_encoding: 'utf-8'}
option_parser = OptionParser.new do |opts|
executable_name = File.basename($PROGRAM_NAME)
@xiaohanyu
xiaohanyu / nanoc_pandoc.rb
Created March 30, 2014 02:37
Improved nanoc pandoc filter
# This file provide two pandoc filters for html and pdf output
module Nanoc::Filters
class PandocHtml < Nanoc::Filter
identifier :pandoc_html
type :text => :text
def run(content, params = {})
input_format = case item[:extension]
@xiaohanyu
xiaohanyu / yaml_to_json.rb
Created September 1, 2014 06:29
convert yaml to json in ruby
require 'json'
require 'yaml'
input_filename = ARGV[0]
output_filename = input_filename.sub(/(yml|yaml)$/, 'json')
input_file = File.open(input_filename, 'r')
input_yml = input_file.read
input_file.close
@xiaohanyu
xiaohanyu / knight.org
Last active April 12, 2017 10:22
Knight problem

Problem

There’s a chess board of size $N × N$, and an piece Knight, which has the initial position $(x, y)$.

Knight can step by going forward or backward two units in one direction, and one units in the other direction.

Find a way to position $(N, N)$ for this Knight piece.

@xiaohanyu
xiaohanyu / vps-swapon.sh
Created November 16, 2015 07:06
Turn on swap memory for vps with small memory
#!/bin/bash
# run it with sudo
# https://www.digitalocean.com/community/tutorials/how-to-add-swap-on-ubuntu-14-04
SWAPFILE=/swapfile
SWAPSIZE=$1
ls $SWAPFILE && swapoff $SWAPFILE && rm $SWAPFILE
fallocate -l $SWAPSIZE $SWAPFILE
chmod 600 $SWAPFILE
@font-face {
font-family: 'Linux Libertine'; /* normal */
src: url('/static/fonts/linux-libertine/LinLibertine_R.woff') format('woff');
font-weight: normal;
font-style: normal;
}
@font-face {
font-family: 'Linux Libertine'; /* italic */
src: url('/static/fonts/linux-libertine/LinLibertine_RI.woff') format('woff');
@xiaohanyu
xiaohanyu / binary_search_demo.exs
Last active October 22, 2016 17:05
A binary search demo in Elixir(from "Programming Elixir" book, Chapter 6)
defmodule Chop do
def guess(final, range) do
low..high = range
middle = div(low + high, 2)
_guess_helper(final, low, high, middle)
end
defp _guess_helper(final, _low, _high, middle) when final === middle do
middle
end
@xiaohanyu
xiaohanyu / org-mode-babel-demo-with-python-and-latex.org
Created December 9, 2016 07:38
Org-mode's org-babel, literate programming with python and latex.
import matplotlib.pyplot as plt
import numpy as np

L = 6
x = np.linspace(0, L)
ncolors = len(plt.rcParams['axes.color_cycle'])
shift = np.linspace(0, L, ncolors, endpoint=False)
for s in shift:
    plt.plot(x, np.sin(x + s), 'o-')
@xiaohanyu
xiaohanyu / perm.hs
Created March 3, 2017 16:14
Haskell permutation generation algorithm with list comprehension.
import Data.List
perm :: (Eq a) => [a] -> Int -> [[a]]
perm _ 0 = [[]]
perm xs r | length xs < r = [[]]
| otherwise = [x:ys | x <- xs, ys <- perm (delete x xs) (r - 1)]