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 / 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 / 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 / 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 / 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 / powerset-in-haskell-and-python.org
Created March 6, 2017 15:08
A demo powerset function implemented in Haskell and Python.

Here’re two powerset function implemented in Python and Haskell.

import copy

def powerset(s):
    if s == []:
        return [[]]
    elif len(s) == 1:
        return [[], s]
@xiaohanyu
xiaohanyu / binary_search_with_simple_unittest.py
Created March 7, 2017 16:30
Simple binary search with simple unit test in Python.
import unittest
def binary_search(array, t):
l = 0
h = len(array) - 1
while (l <= h):
m = (l + h) // 2
if (array[m] == t):
return m
@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 / udp_test.erl
Created August 13, 2017 11:48
UDP server and client for factorial function.
%% udp test client and server, from joe armstrong's "programming erlang, second
%% edition"
-module(udp_test).
-export([start_server/0, client/1]).
start_server() ->
spawn(fun() -> server(4000) end).
%% the server
server(port) ->