Skip to content

Instantly share code, notes, and snippets.

View liuxinyu95's full-sized avatar

Liu Xinyu liuxinyu95

View GitHub Profile
@liuxinyu95
liuxinyu95 / init.lua
Last active June 10, 2022 05:59
Resume Emacs Key bindings for Outlook/Word in OS X
--- Emacs key bindings for Outlook/Word
print("Resume Emacs bindings over outlook/word")
function isKeyStroke(event, modifiers, chars)
return event:getFlags():containExactly(modifiers) and event:getCharacters(true) == chars
end
function targetApp()
local app = hs.application.frontmostApplication()
@liuxinyu95
liuxinyu95 / purchase.py
Last active October 27, 2019 06:03
find the lowest cost purchase solution
# purchase.py
# Copyright (C) 2019 Liu Xinyu (liuxinyu95@gmail.com)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
@liuxinyu95
liuxinyu95 / Test.java
Last active April 15, 2017 07:00
Improve a customized filter program
/*
* 有读者在《算法新解》群问,为什么下述程序的slow函数很慢?
* 主要原因是slow方法采用了性能不佳的双重循环搜索,它对n个元素数组中的每个元素,都逐一在含有m个元素数组中查找,复杂度是O(n*m)
* 次要原因是每次循环中都重复的做split操作,split实际使用了正则表达式匹配,是个比较昂贵的运算。
*
* 为此,最主要改进是,把含有m个元素的数组,转换成效率较高的字典,例如Hash set或者Tree set。
* 用Hash set的空间较大,但是每次查找时间较快,为O(1),故而总体性能为O(n)。变为线性时间的算法;
* 用Tree set会节省空间,但是每次查找的时间为O(lg m),故而总体性能为O(n * lg m),仍然能大大提高性能
* 下面的例子fast()函数中,我们也可以使用函数式风格的collect或者reduce,为了照顾不熟悉的读者,我们使用了传统的风格。
*/
/*Given an integral number in [0, 999 billion), print the English phrase for this number. */
import java.util.*;
import java.lang.*;
import java.io.*;
class NumToEnglish {
static final String names[] = {"zero", "one", "two", "three", "four", "five",
"six", "seven", "eight", "nine", "ten", "eleven",
"twelve", "thirteen", "fourteen", "fifteen", "sixteen",
"seventeen", "eighteen", "nineteen"};
/* problem 1, A generic Fibonacci number lookup program.*/
/*
imperative solution.
fibo n
F: static array of n, with F[0] = F[1] = 1
i = 2
while i < n and F[i] != NIL do
++i
for i to n-1
F[i] = F[i-1] + F[i-2]