Skip to content

Instantly share code, notes, and snippets.

@LCamel
LCamel / a.pl
Created June 7, 2012 15:30
find longest sequence
# $ perl a.pl 1 1 1 1 1 3 4 4 4 4 4
# len: 5 nums: 4 1
# $ perl a.pl 1 1 1 1 1 3 4 4 4 4
# len: 5 nums: 1
sub f {
my ($x, @xs) = @_;
if (not @xs) {
(1, $x, 0, ());
} else {
@LCamel
LCamel / a.pl
Created June 7, 2012 15:30
find longest sequence
# $ perl a.pl 1 1 1 1 1 3 4 4 4 4 4
# len: 5 nums: 4 1
# $ perl a.pl 1 1 1 1 1 3 4 4 4 4
# len: 5 nums: 1
sub f {
my ($x, @xs) = @_;
if (not @xs) {
(1, $x, 0, ());
} else {
@LCamel
LCamel / a.hs
Created June 7, 2012 18:06
find longest sequence
f :: [Int] -> [Int]
f [x] = [1, x, 0]
f (x:xs) =
if h == x then
if hl + 1 > yl then [hl + 1, h, 0]
else [hl + 1, h, yl] ++ ys
else
if hl == yl then [1, x, yl] ++ h:ys
else if hl > yl then [1, x, hl, h]
else [1, x, yl] ++ ys
tmp=$(cat <<EOPY
import sys
for line in sys.stdin:
print line.replace( 'asdf', 'ASDF' )
print(line)
EOPY)
echo "asdf.asdf.asdf.e" | python -c "$tmp"
@LCamel
LCamel / gist:2940243
Created June 16, 2012 06:45
regex 3-SAT
# https://www.facebook.com/LCamel/posts/380886841959001
print join(",", "xxxx;x,x,x,x,x,x,x," =~ /^
(x?)(x?)(x?)(x?) .* ;
(?: x\1 | \2 | \4 ),
(?: \1 | x\3 | \4 ),
(?: \1 | x\2 | x\4 ),
(?: \2 | x\3 | x\4 ),
(?: x\1 | \3 | x\4 ),
(?: \1 | \2 | \3 ),
#include <stdio.h>
unsigned char f(int i, int j) {
unsigned char c1 = -1 << i;
unsigned char c2 = ~(-1 << ++j);
unsigned char c3 = c1 & c2;
printf("%d\n", c1);
printf("%d\n", c2);
printf("%d\n", c3);
}
// for: https://www.facebook.com/qingwang/posts/10151427723443879
var output = ["剪刀", "石頭", "布"];
var who = ["女孩", "男孩"];
function foo(depth) {
if (depth == 0) {
return ["女孩想出石頭", 1];
} else {
var other = foo(depth - 1);
var my = (other[1] + 3 - 1) % 3;
return [who[depth % 2] + "想: (" + other[0] + "), 所以自己出" + output[my], my];
@LCamel
LCamel / ImpossiblePuzzle.scala
Created June 3, 2013 16:55
My Solution for "Impossible Puzzle"
object ImpossiblePuzzle extends App {
type XY = (Int, Int)
val xys0 = for {
x <- 1 to 100
y <- 1 to 100
if 1 < x && x < y && x + y < 100
} yield (x, y)
def sum(xy: XY) = xy._1 + xy._2
def prod(xy: XY) = xy._1 * xy._2
@LCamel
LCamel / adjust_time.pl
Created October 28, 2013 05:17
adjust time
my $delta = shift; # ms
while (<>) {
if (/^(..):(..):(..),(...) --> (..):(..):(..),(...)/) {
my $t1 = conv($1, $2, $3, $4, $delta);
my $t2 = conv($5, $6, $7, $8, $delta);
print "$t1 --> $t2\n";
} else {
print;
}
# This is the default .slate file.
# If no ~/.slate file exists this is the file that will be used.
config defaultToCurrentScreen true
config nudgePercentOf screenSize
config resizePercentOf screenSize
# Resize Bindings
#bind right:alt resize +10% +0
#bind left:alt resize -10% +0