The Binary Binary Expansion
Description:
Normally, we decompose a number into binary digits by assigning it with powers of 2, with a coefficient of 0
or 1
for each term:
25 = 1*16 + 1*8 + 0*4 + 0*2 + 1*1
The choice of 0
and 1
is... not very binary. We shall perform the true binary expansion by expanding with powers of 2, but with a coefficient of 1
or -1
instead:
25 = 1*16 + 1*8 + 1*4 - 1*2 - 1*1
Now this looks binary.
Given any positive number n
, expand it using the true binary expansion, and return the result as an array, from the most significant digit to the least significant digit.
true_binary(25) == [1,1,1,-1,-1]
It should be trivial (the proofs are left as an exercise to the reader) to see that:
- Every odd number has infinitely many true binary expansions
- Every even number has no true binary expansions
Hence, n
will always be an odd number, and you should return the least true binary expansion for any n
.
Also, note that n
can be very, very large, so your code should be very efficient.
Similar Kata:
Stats:
Created | Aug 31, 2017 |
Published | Aug 31, 2017 |
Warriors Trained | 1198 |
Total Skips | 93 |
Total Code Submissions | 1315 |
Total Times Completed | 213 |
Python Completions | 207 |
Ruby Completions | 14 |
Total Stars | 40 |
% of votes with a positive feedback rating | 93% of 59 |
Total "Very Satisfied" Votes | 52 |
Total "Somewhat Satisfied" Votes | 6 |
Total "Not Satisfied" Votes | 1 |
Total Rank Assessments | 6 |
Average Assessed Rank | 4 kyu |
Highest Assessed Rank | 3 kyu |
Lowest Assessed Rank | 6 kyu |