Skip to content

Instantly share code, notes, and snippets.

@henix
henix / putint.c
Created September 11, 2013 07:40
putint
inline void putint(int n)
{
static char buf[20];
register int pos;
register int x = n;
if (x == 0) {
putchar('0');
return;
}
if (x == INT_MIN) { // x = -x do not work for the minimal value of int, so process it first
#!/usr/bin/python2
import sys
import re
records = []
for line in sys.stdin:
records.append(re.match(r'[ ]*(\d)+ ([^ ]+)(.*)', line.rstrip()).groups())
for record in sorted(records, key=lambda d:int(d[0])*(len(d[1])+len(d[2])), reverse=True):
print "%s %s%s" % record
#include <assert.h>
#include <stdio.h>
#include <string.h>
typedef int T;
/**
* [Sorting network](http://en.wikipedia.org/wiki/Sorting_network)
*/
void sort8(T ar[], int n) {
@henix
henix / javaescn.sed
Created June 21, 2013 09:18
Encode a file into a Java string, with newline inserted at each \n
# http://stackoverflow.com/questions/1251999/sed-how-can-i-replace-a-newline-n
:a
N
$!ba
s/\\/\\\\/g
s/"/\\"/g
s/\r/\\r/g
s/\t/\\t/g
s/\f/\\f/g
s/\n/\\n" +\n"/g
@henix
henix / qpdecode.scala
Last active December 17, 2015 04:48
quoted-printable decode scala script # for manually decode an email
#!/bin/sh
REPO=~/.m2/repository
exec scala -classpath $REPO/commons-io/commons-io/2.4/commons-io-2.4.jar:$REPO/javax/mail/mail/1.4.6/mail-1.4.6.jar:$REPO/javax/activation/activation/1.1.1/activation-1.1.1.jar "$0" "$@"
!#
import java.io.InputStreamReader
import java.nio.charset.Charset
import javax.mail.internet.MimeUtility
import org.apache.commons.io.IOUtils
@henix
henix / highvote.mk
Created May 6, 2013 17:11
Download top 50 highest voted ${tag} questions on stackoverflow, and convert them to pdf. depends on web2pdf.sh
.PHONY: all
all: highvote-pdf.mk
make -j4 -f $< WEB2PDF=/home/henix/web2pdf.sh
highvote-pdf.mk: highvote.html
xmllint --html --xpath "//a[@class='question-hyperlink']/@href" $< 2> /dev/null | sed -e 's/ href="//g' -e 's/"/\n/g' | lua link2mk.lua > $@
highvote.html:
curl 'http://stackoverflow.com/questions/tagged/scala?sort=votes&pagesize=50' > $@
@henix
henix / web2pdf.css
Last active December 16, 2015 22:28
convert a web page to pdf using [prince](http://www.princexml.com/) 8.1
* {
font-family: "DejaVu Serif", "AR PL New Sung", "WenQuanYi Zen Hei", serif;
}
@page {
/*background-color: #C7EDCC;*/
background-color: #CCB995;
margin: 1.5cm;
}
@henix
henix / mklibclass.sh
Created April 26, 2013 02:18
./mklibclass > libclass ; gvim libclass ; /\.ClassName # find full class name in your project, for vimer writing scala
#!/bin/sh
find . -name "*.jar" | xargs -L 1 unzip -l | grep 'class$' | awk '{print $4}' | sed -e 's/\.class$//g' -e 's/\//./g'
@henix
henix / game24.pro
Last active December 16, 2015 11:39
A 24-game solver in Prolog
/* RPN = Reverse Polish notation */
operator(add).
operator(sub).
operator(mul).
operator(div).
doop(add, A, B, C) :- C = A + B.
doop(sub, A, B, C) :- C = A - B.
doop(mul, A, B, C) :- C = A * B.
@henix
henix / bri.sh
Created April 19, 2013 11:25
bri: show, set and store the brightness of your laptop (BTW, check the battery)
#!/bin/bash
BRIFILE=/sys/class/backlight/acpi_video0/brightness
if [ $# -lt 1 ]; then
cat $BRIFILE
else
echo $1 > $BRIFILE
echo $1 > /root/.bri
fi