# How to build PDFsam enhanced on Linux

PDFsam (PDF Split and Merge) is a free and open source desktop utility designed to perform pdf documents manipulation (pdf merge, pdf split, page rotation etc). PDFsam Enhanced is the Pro version. It has additional features including encryption/decryption of pdf files, extracting attached files, mixing two pdf files, etc. This post tells you how to download the source code, compile and use it for free.… More

# Java.io in nutshell: 22 case studies

This post attempts to cover a comprehensive set of operations in java.io. Compared with other books and blogs related to this topic, my motivation is to show “how-to” through case studies. As once a Java student, I realize the most effective way of learning a new program language is through examples: copy and paste a piece of codes, run it to see results, then try to modify and re-run it step-by-step. Therefore, I assume this post will be helpful.

It is worth noting that this post will not touch anything related to java.nio because I think it is quite a different topic.

# Recommend – LaTeX symbol classifier

When I am working with LaTeX, one difficulty is to memorize a symbol. Finding it via Internet is not easy either. Detexify is quite useful. You can simply draw the symbol and it will do OCR and search the matched one.

More

# MathJax with blogger.com

MathJax in blogger.com is useful when I need to input equation in my blog. This is a instruction and test of using MathJax in blogger.com.

1. set blogger.com template to “Simple”. “Dynamic Views” seems not working.
2. edit HTML by adding the following code between <head> and </head>: <script src='http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML' type='text/javascript'/>
3. type math equation in the blog.
4. More

# Given a binary tree, create a list of all the nodes at each depth — Recursive version

Another FAQ on the programming interview.

### Question

Given a binary search tree, design an algorithm which creates a linked list of all the nodes at each depth (eg, if you have a tree with depth D, you’ll have D linked lists).

### Solution

Cracking the Coding Interview (4ed) (see page 126) provided a non-recursive algorithm. For me, a recursive version is neater and much easier to understand.… More

# Sort a stack in ascending order in O(n log n)

Another FAQ on the programming interview.

### Question

Write a program to sort a stack in ascending order. You should not make any assumptions about how the stack is implemented. The following are the only functions that should be used to write this program: push | pop | peek | isEmpty.

### Solution

Cracking the Coding Interview (4ed) (see page 121) provided an O(N^2) algorithm by using one more stack. The idea is to pull an item from the original stack and push it on the other stack. If pushing this item would violate the sort order of the new stack, the algorithm removes enough items from it so that it’s possible to push the new item. Because of the copyright, I won’t paste her code here, but you can easily find it out online (:P).

Can we do better? I think we can mimic the way of quicksort by picking a pivot from the stack (using pop) first. Then we conduct the partition operation by pushing all elements in the stack with values less than the pivot into one stack, while all elements with values greater than the pivot into the other. After that, we recursively sort two sub-stacks … More

# Two algorithms to find the nth to last element of a singly linked list

This is one of frequently asked questions on the programming interview. There are two kinds of solution: iteration-based and recursion-based.

### Data structure

We first define a toy class for the single linked list.

class LinkedListNode {

int            data;

this.data = data;
this.next = next;
}
}


### Iteration-based algorithm

The iteration-based solution is quite tricky but I really like it. In Cracking the Coding Interview (4ed), the solution uses two pointers p1 and p2. Her algorithm first increaments p2 by (n-1) times, and then moves both p1 and p2 until p2.next reaches the tail of the list. As the result, p1 points to the nth to last element. Because of the copyright, I won’t reproduce her code in this post. If you are interested, please see page 106 for the solution.

### Recursion-based algorithm

The recursion version is straightforward too. We first recursively call a function until we reach the tail of the list. Then we start to count from 0 and increment the counter when a called function is returned. Essentially, it counts the number of popped function calls on the call stack. When the counter equals to n, we … More

# 函数式编程的另类指南(10)

## 模式匹配

int fib(int n) {
if(n == 0) return 1;
if(n == 1) return 1;

return fib(n - 2) + fib(n - 1);
}


# How to fix Gedit LaTex Plugin error while saving BibTex

Gedit (3.4.1) gives the error below while saving a BibTex file (Ubuntu 12.04). The saving works fine, apart from the error popping up.

'bibtex-error'

Traceback (most recent call last):
File "/usr/lib/gedit/plugins/latex/util.py", line 116, in decorated_function
return function(*args, **kw)
File "/usr/lib/gedit/plugins/latex/bibtex/editor.py", line 141, in __parse
self.remove_markers("bibtex-error")
File "/usr/lib/gedit/plugins/latex/editor.py", line 493, in remove_markers
type_record = self._marker_types[marker_type]
KeyError: 'bibtex-error'


How to fix it?More

# 函数式编程的另类指南(8)

## 惰性求值

String s1 = somewhatLongOperation1();
String s2 = somewhatLongOperation2();
String s3 = concatenate(s1, s2);