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 / 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
@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 / 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 / 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 / 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 / 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))))