Skip to content

Instantly share code, notes, and snippets.

@airekans
airekans / pitar.sh
Created November 21, 2013 04:39
Use pigz to tar and zip files and directories. Here's a simple shell script to do this. Pigz: http://www.zlib.net/pigz/
#! /bin/bash
if [ $# -lt 2 ]; then
echo Usage: $0 TARGET INPUT1 [...]
exit 0
fi
DIR=$(dirname $0)
target=$1
shift
@airekans
airekans / hex2md5.py
Created November 18, 2013 11:42
A simple python script used to encode a hex string by md5.
import md5
import sys
if __name__ == '__main__':
if len(sys.argv) < 2:
print 'Usage: %s hex_str' % sys.argv[0]
hex_str = sys.argv[1]
mem = bytearray(len(hex_str) / 2)
for i in xrange(len(hex_str) / 2):
@airekans
airekans / div.py
Created October 29, 2013 08:54
Python commandline tools
import sys
a, b = sys.argv[1:]
print float(a) / float(b)
@airekans
airekans / random_list_deep_copy.cpp
Last active December 25, 2015 03:39
[OJ] 给定一个单链表,每个节点上还有一个random指针,指向链表中随机的一个节点,或者是NULL。写一个对于这个链表的深拷贝函数。
struct Node
{
Node* next;
Node* random;
int data;
Node(const Node& n)
: next(NULL), random(NULL), data(n.data)
{}
};
@airekans
airekans / flatten.scm
Last active August 17, 2017 08:54
Interestng Scheme Snippets
(define flatten (seq)
(cond (((null? seq) '())
((pair? (car seq)) (append (flatten (car seq))
(flatten (cdr seq))))
(else (cons (car seq) (flatten (cdr seq)))))))
(define append (l1 l2)
(if (null? l1)
l2
(cons (car l1) (append (cdr l1) l2))))
@airekans
airekans / is_interleave.cpp
Created September 18, 2013 05:47
[OJ] 给定字符串a, b, c,判断c是否是a和b的interleave串。
string a;
string b;
string c;
bool is_interleave(const string& sa, const string& sb, const string& sc)
{
if (sc.size() != sa.size() + sb.size())
{
return false;
}
@airekans
airekans / infix_to_postfix.cpp
Created September 12, 2013 15:21
[OJ] infix expression to postfix expression tranformation.
// in-fix to post-fix expression
typedef vector<string> Expr;
void transform(const Expr& infix, Expr& result)
{
stack<string> ops;
for (int i = 0; i < infix.size(); ++i)
{
if (infix[i] == "*" || infix[i] == "/")
@airekans
airekans / n_bits_binary.cpp
Created September 12, 2013 13:42
[OJ] 给定n,输出0 - 2^n - 1的2进制表示,而且序列中相邻两个数只有一位不同。
typedef vector<char> BinaryNum;
void binary_num(int n, vector<BinaryNum>& results)
{
if (n < 1)
{
return;
}
BinaryNum tmp(n, 0);
@airekans
airekans / celerity.cpp
Created September 12, 2013 00:33
[OJ] Careercup, given a function knows(A, B) returning 1 if A knows B, and 0 if A doesn't know B. Find a celerity that doesn't know anyone and everyone knows him.
//
bool knows(const int i, const int j)
{
// xxxx
return true;
}
int celerity(vector<int>& person)
{
@airekans
airekans / node_count.cpp
Created September 11, 2013 14:53
[OJ] count the number of nodes in the tree
// solution 1: count the nodes of left and right subtree, and add them up
int node_count(Node* node)
{
if (node == NULL)
{
return 0;
}
return node_count(node->left) + node_count(node->right) + 1;
}