57
6
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.
Can we receive the input as an array of single character strings? Can we assume that the number will never be greater than 9? – fəˈnɛtɪk – 2017-04-07T13:29:51.077
@fəˈnɛtɪk Regarding the input format: I'd say no, unless this is the only acceptable format for your language. We're dealing with single digits rather than numbers. So yes: it's guaranteed to be <= 9 but you may encounter several digits in a row. – Arnauld – 2017-04-07T13:38:18.853
Would
1bbab
be a valid input (with expected output ofabbab
)? In other words, can the references wrap around the string? – Luke – 2017-04-07T13:41:47.353@Luke Good point. No,
1bbab
is not valid. I've added a clarification about that. – Arnauld – 2017-04-07T13:48:08.787