Skip to content

Instantly share code, notes, and snippets.

@xiaq
xiaq / q10.java
Last active February 15, 2017 21:48
Problem 10 of LeetCode
public class Solution {
public boolean isMatch(String s, String p) {
// m[i][j] means: does s[0..i-1] match p[0..j-1]?
// where when i = 0, s[0..i-1] is the empty string.
boolean[][] m = new boolean[s.length()+1][p.length()+1];
int i, j;
// Find m[i][j] for i = 0 (string is empty). It is only true for the leading part of p that looks like x*y*z*w*...
m[0][0] = true;
for (j = 2; j <= p.length(); j += 2) {
@xiaq
xiaq / rec-vs-dp.md
Last active February 14, 2017 20:52
Recursion vs Dynamic Programming

Recursive function:

int fib(int n) {
    if (n == 0 || n == 1) {
        return 1;
    }
    return fib(n-1) + fib(n-2);
}
def perm(a, i):
if i == len(a):
print(a)
return
for j in range(i, len(a)):
a[i], a[j] = a[j], a[i]
perm(a, i+1)
a[i], a[j] = a[j], a[i]
if __name__ == '__main__':
package main
import (
"log"
"os"
"os/signal"
"syscall"
"github.com/elves/elvish/sys"
)
@xiaq
xiaq / easy.sh
Created December 23, 2016 18:22
A task easy with shell, impossible with GUI
wget http://example.com/example.iso && test $(md5sum example.iso | cut -d' ' -f1) = e46f30a8b674feafc358ecf490335078 && dd if=example.iso of=/dev/sdb && mplayer success.mp3 && poweroff
set -s escape-time 50
if-shell "test $TERMINAL = roxterm" "set -s set-clipboard off"
elvish='$GOPATH/bin/elvish'
if-shell "test -x $elvish" "set -g default-shell $elvish"
set -g base-index 1
set -g renumber-windows on
set -g mouse
fn quick-sort [li]{
if <= (count $li) 1; then
put $@li
else
quick-sort [(each { if <= $0 $li[0]; then put $0; fi } $li[1:])]
put $li[0]
quick-sort [(each { if > $0 $li[0]; then put $0; fi} $li[1:])]
fi
}
@xiaq
xiaq / elvish-0.5-breaking-changes.md
Last active October 11, 2016 23:20
Breaking changes in elvish 0.5
  • The println builtin is now known as echo.

  • The == builtin now compares numbers -- == 1 1.0 is true, while == a a throws an exception. To compare strings, use ==s. It is part of a family of new builtins for comparing strings, <s, <=s, ==s, !=s, >s and >=s.

  • The is builtin is the new generic (shallow) equality predicate. The deep equality predicate deepeq is now known simply as eq.

  • Automatic use'ing has been removed. All modules must be used before use.

@xiaq
xiaq / get-completion.bash
Last active June 15, 2020 21:27
get-completion.bash
# Usage:
# Invoke this script with the current command line, one argument per word:
#
# bash get-completion.bash pacman -S x
#
# Exits with 1 if no completer has been found.
#
# To use this as the default completion in elvish, put the following in ~/.elvish/rc.elv:
# le:completer['']={ bash /path/to/get-completion.bash $@ }
#
symbol-for=[&ahead="↑" &behind="↓" &diverged="⥄ " &dirty="⨯" &none="◦"]
fn ok [f]{
try $f
except; false
tried
}
fn not-ok [f]{
try $f; false