In this challenge, your task is to decipher a string. Luckily, the algorithm is pretty simple: reading from left to right, each encountered digit N (0 to 9) must be replaced with the character which is N+1 positions before it.
Example
The input string "Prog2am0in6"
would be decoded this way:
Hence, the expected output is "Programming"
.
Clarifications and rules
- The input string will contain ASCII characters in the range 32 - 126 exclusively. You can assume that it will never be empty.
- The original deciphered string is guaranteed not to contain any digit.
- Once a character has been decoded, it may in turn be referenced by a subsequent digit. For instance,
"alp2c1"
should be decoded as"alpaca"
. - References will never wrap around the string: only previous characters can be referenced.
- You can write either a full program or a function, which either prints or outputs the result.
- This is code golf, so the shortest answer in bytes wins.
- Standard loopholes are forbidden.
Test cases
Input : abcd
Output: abcd
Input : a000
Output: aaaa
Input : ban111
Output: banana
Input : Hel0o W2r5d!
Output: Hello World!
Input : this 222a19e52
Output: this is a test
Input : golfin5 3s24o0d4f3r3y3u
Output: golfing is good for you
Input : Prog2am0in6 Puz0les7&1Cod74G4lf
Output: Programming Puzzles & Code Golf
Input : Replicants 4re3lik448ny3oth8r5mac6in8.8T64y'r371it9376a1b5n1fit7or2a1h2z17d.
Output: Replicants are like any other machine. They're either a benefit or a hazard.
1bbab
be a valid input (with expected output ofabbab
)? In other words, can the references wrap around the string? \$\endgroup\$1bbab
is not valid. I've added a clarification about that. \$\endgroup\$