Length-encode a string
JavaScript (ES6), 61 bytes
f=(s,d=2)=>s?d>s.length?s[0]+f(s.slice(1),d-2):'*'+f(s,d*2):s
Recursive function that does the following:
If
d
is less than or equal to remaining string length divided by 2:Append
*
to output and multiplyd
by 2Else:
Shift the string and append to output, subtract 1 from
d
.
See it in action:
f=(s,d=2)=>s?d>s.length?s[0]+f(s.slice(1),d-2):'*'+f(s,d*2):s
input.oninput = e => output.innerHTML = f(input.value);
<input id="input" type="text"/>
<p id="output"></p>
Pyth, 29 27 (Noticed broken) 27 26 25 bytes
+*\*sKllzXJ-^2.EKlzz?J\*k
Explanation to come.
Test Suite
Pyth (36 27 bytes)
Thanks to Jakube for a 9 byte improvement! Currently not as good as muddyfish's answer, but whatever
KlzJ1VzWgKyJp\*=yJ)pN=tK=tJ
Test Suite
Translation to python:
| z=input() #occurs by default
Klz | K=len(z)
J1 | J=1
Vz | for N in z:
WgKyJ | while K >= J*2:
p\* | print("*", end="")
=yJ | J=J*2
) | #end inside while
pN | print(N, end="")
=tK | K=K-1
=tJ | J=J-1