Substitute numbers by their respective letter
Perl, 39 38 bytes
(1 byte added for the -p
flag.)
$"="|";s/@{[65..90,97..122]}/chr$&/ge
s/ / replace...
@{[ ]} this string, treated as a regex:
join"|",65..90,97..122 65|66|67|68|...|121|122
/ge ...with this string, eval()'d:
$& the entirety of the last match
chr convert to char from ASCII code
Right Tool for the Job™.
The explanation is outdated after one small optimization (thanks dev-null!) that makes it a single byte shorter (but a bit less elegant): the $"
variable represents what to join
on when interpolating an arrray into a string, so setting $"="|"
removes the need for join
.
Demo:
llama@llama:~$ perl -pe '$"="|";s/@{[65..90,97..122]}/chr$&/ge'
1234567
12345C
3456789
345CY
9865432
bA432
6566676869707172737475767778798081828384858687888990
ABCDEFGHIJKLMNOPQRSTUVWXYZ
6711110010100071111108102
Code000Golf
Javascript, 80 bytes
s=>s.replace(/6[5-9]|[78]\d|9[0789]|1[01]\d|12[012]/g,x=>String.fromCharCode(x))
See regex in action here: https://regex101.com/r/iX8bJ2/1
f=
s=>s.replace(/6[5-9]|[78]\d|9[0789]|1[01]\d|12[012]/g,x=>String.fromCharCode(x))
document.body.innerHTML = '<pre>' +
"f('1234567')\n" + f('1234567') + '\n\n' +
"f('3456789')\n" + f('3456789') + '\n\n' +
"f('9865432')\n" + f('9865432') + '\n\n' +
"f('6566676869707172737475767778798081828384858687888990')\n" + f('6566676869707172737475767778798081828384858687888990') + '\n\n' +
"f('6711110010100071111108102')\n" + f('6711110010100071111108102') +
'</pre>'
Just for curiosity, one thing I learned here:
I can't change x=>String.fromCharCode(x)
to String.fromCharCode
because ...
CJam, 22 bytes
q{+'[,_el^{_is@\/*}/}*
Try it online!
Background
Simply replacing all occurrences of digit groups with the corresponding letters (in whatever order we may choose) will fail to comply with the left-to-right rule.
Instead, we can generate all prefixes of the input string, and attempt to make all possible substitutions while we're generating them. Since no code point is contained in another code point, the order of these attempts is not important.
For example:
67466
6 -> 6
67 -> C
C4 -> C4
C46 -> C46
C467 -> C4B
C4B
How it works
q Read all input from STDIN.
{ }* Fold; push the first character, and for each subsequent
character, push it and do the following:
+ Append the character to the string on the stack.
'[,_el^ Push the string of all ASCII letters.
See: http://codegolf.stackexchange.com/a/54348
{ }/ For each ASCII letter:
_ Push a copy of the letter.
i Convert to integer, i.e., compute its code point.
s Cast to string.
@\ Rotate and swap.
/ Split the modified input characters at occurrences (at
most one) of the string repr. of the code point.
* Join, separating by the corresponding letter.