Skew Binary
InterviewTime limit1sMemory limit128 MB
Convert each decimal number into its unique skew binary representation, printed as the sorted ranks of its nonzero digits.
- Level
Easy3 of 10
- Topics
- Math, Greedy, Implementation, Combinatorics
- Solved
- No attempts yet
Problem
A group of functional-programming enthusiasts are especially fond of skew binary numbers because they are easy to increment and decrement. To help prepare banners and other material for their anniversary party, write a program that converts decimal integers into the skew binary format they like.
A number representation is a list of digits. Call the lowest-order digit the rank-0 digit, the next the rank-1 digit, and so on. For example, in decimal the digits are 0-9, the rank-0 digit has weight 1, the rank-1 digit has weight 10, and the rank- digit has weight . In binary the digits are 0 and 1, and the rank- digit has weight .
In skew binary the digits are 0, 1, and 2, and the rank- digit has weight .
Allowing the digit 2 means a number may have several skew binary representations. By convention, the digit 2 may appear only as the lowest-rank nonzero digit; this makes the representation unique.
In this problem you write a skew binary number as the list of ranks of its nonzero digits. A digit equal to 2 is written by listing its rank twice. Because of the uniqueness rule, only the two smallest ranks in the list may be equal. Ranks are listed in increasing order, each rank is a decimal integer, ranks are separated by commas (,), and the whole list is enclosed by [ and ]. For example, decimal 5 has the skew binary digits "12" (a 1 in rank 1 and a 2 in rank 0), so it is written [0,0,1]. Decimal 0 is the empty list [].
Convert each given decimal number into this skew binary list format.
Input
The first line contains an integer (), the number of test cases. Each of the next lines contains a single decimal number with no leading or trailing whitespace, in the range to inclusive.
Output
For each test case, print one line containing the input decimal number (with no leading zeros or spaces), a single space, and the skew binary equivalent in the list format described above (with no leading or trailing spaces; within the list each rank has no extra leading zeros and no surrounding spaces).