Cheating a multiple choice test, part 2
05AB1E, 19 18 16 bytes
Code:
žH+b¦2äøC’c‰±’sè
Uses the CP-1252 encoding. Try it online!
Explanation:
First, we add 65536
to the number (žH
is a constant defined to 65536
), which is also 10000000000000000
in binary. This is to pad the number with zeroes. Let's take the number 14234
as an example. 14234 + 65536
is equal to 79770
. Which in binary is:
10011011110011010
We remove the first character, resulting in:
0011011110011010
We split the string into two pieces using 2ä
:
00110111, 10011010
After that, we zip the array with ø
:
01, 00, 10, 11, 01, 10, 11, 10
Converting them back into decimal (using C
) results in:
1, 0, 2, 3, 1, 2, 3, 2
Now, we only need to index it with the string cbad
. The compressed version for this string is ’c‰±’
, which can also be tested here. Finally, we get the characters at the index of the above array. For the above example, this results in:
1, 0, 2, 3, 1, 2, 3, 2
b c a d b a d a
JavaScript (ES6), 55 48 bytes
f=(n,i=8)=>i--?"CBAD"[n>>i&1|n>>i+7&2]+f(n,i):''
console.log(f(14234)); // BCADBADA
console.log(f(38513)); // ABBDCAAB
console.log(f(0)); // CCCCCCCC
console.log(f(120)); // CBBBBCCC
console.log(f(65535)); // DDDDDDDD
console.log(f(39253)); // ABCDABCD
Non-recursive version (55 bytes)
Using a regular expression, we can do:
n=>"76543210".replace(/./g,i=>"CBAD"[n>>i&1|n>>+i+7&2])
Python 2, 53 bytes
f=lambda n,k=8:k*'_'and f(n/2,k-1)+'CBAD'[n>>7&2|n&1]
Test it on Ideone.