Personal Programming Notes

To err is human; to debug, divine.

Analytic Functions in MySQL

MySQL has traditionally lagged behind in support for the SQL standard. Unfortunately, from my experience, MySQL is often used as the sandbox for SQL code challenges and interviews. If you are used to work with Vertica SQL, writing SQL statements in MySQL can be challenging exercises, NOT necessarily in a good way, because many useful features are not supported.

Convert Python Objects to JSON (Ordered Keys)

In the JSON output shown in the last post, the keys are printed out of order since they are unordered in the internal dictionary __dict__. In theory, it does not matter to machines when converting to/from JSON. However, it sometimes makes sense to humans for the keys to be pretty-printed in order, especially when we need to look for two keys in JSON next to each other or one key before another.

Convert Python Objects to JSON

In this post, we looks into converting a plain, simple Python object into JSON. JSON serialization in Java is also provided as an example. In the following post, we will look into a more advanced method of conversion with attributes pretty-printed in order, like in the Java example.

Rabin-Miller Primality Test

In Qualification Round of Google Code Jam 2016, there is an interesting Coin Jam problem. The summarized problem statement is as follows:

A jamcoin is a string of N ≥ 2 digits with the following properties:

1) Every digit is either 0 or 1.
2) The first digit is 1 and the last digit is 1.
3) If you interpret the string in any base between 2 and 10, inclusive, the resulting number is not prime.

Can you produce J different jamcoins of length N, along with proof that they are legitimate?

For example, for the jamcoin 1001, a possible set of nontrivial divisors for the base 2 through 10 interpretations of the jamcoin would be: 3, 7, 5, 6, 31, 8, 27, 5, and 77, respectively.

Best Friend Forever

BFF is the name of the problem C in Google Code Jam 2016, Round 1A. The summarized problem statement is as follows:

Every kid in your class has a single best friend forever (BFF).
You want to form the largest possible circle of kids such that each kid in the circle is sitting directly next to their BFF, either to the left or to the right.
Give a line that contains N integers F1, F2, ..., FN, where Fi is the student ID number of the BFF of the kid with student ID i, find the greatest number of kids that can be in the circle.

Bash Trap

In this post, we discuss common usage of bash trap to ensure proper cleanup operations in Bash scripts. It also discusses a common idiom trap cleanup INT TERM EXIT where other signals such as INT and TERM is also trapped in addition to EXIT. While such idiom could be valid in some Unix system, it is usually redundant and can be simply wrong (duplicate executions) in most cases, as shown on Mac. A simple test is provided to verify if such idiom is applicable in your current system.