#Pyth, 12 11 bytes
Pyth, 12 11 bytes
|R_1.>jQ2 1
#How?
How?
|R_1.>jQ2 1 Full program. jQ2 Convert input to a binary list. .> 1 Cyclically rotate the list above by 1 place to the right. |R_1 Substitute 0 with -1. Implicitly output.
First off, we notice that the task is just "substitute the 0
s in the binary writing with -1
s and shift to the right by 1 place." — That's exactly what we should do! The binary conversion gives us a list of 0
s and 1
s. All we should do here is to find a golfy way to convert 0
to -1
. The bitwise operator |
(bitwise OR) is our friend. The map over the binary representation shifted with |
and -1
. If the current number is 0
, it gets converted to -1
.