Jelly, 18 bytes
I ended up stealing Erik's ṾṖ$İƑ¡
for this one (otherwise I'd also have 19)...
ṖṖv0ḢṾṖ$İƑ¡,Ạ¡”boḢ
A full program which prints the result.
Try it online! Or see the test-suite.
###How?
How?
ṖṖv0ḢṾṖ$İƑ¡,Ạ¡”boḢ - Main Link: list of characters S
Ṗ - discard right-most (of S)
Ṗ - discard right-most
0 - literal zero
v - evaluate as Jelly code with right argument (0)
- ... b is covert-to-base, so "nb0" gives [n]
Ḣ - head ([n]->n or n->n)
¡ - repeat...
Ƒ - ...# of times: is invariant under:
İ - reciprocation (n->1/n)
$ - ...action: last two links as a monad:
Ṿ - un-evaluate (-1->"-1" or 1->"1")
Ṗ - discard right-most ("-1"->"-" or "1"->"")
¡ - repeat...
Ạ - ...# of times: all?
, ”b - ...action: pair with a 'b' character
o - logical OR with:
Ḣ - head (S) (i.e. if we end with 0 use the 1st character of the input)
- implicit print