Skip to main content
added 437 characters in body
Source Link
Dada
  • 8.5k
  • 1
  • 18
  • 44

Perl 5, 34 bytes

33 bytes of code + -p flag.

s/\d/substr$_,-$&-1+pos,1/e&&redo

Try it online!

s/\d/.../e replace the first digit by ... evaluated as Perl code. (with ... being substr$_,-$&-1+pos,1 in that case. substr$_,-$&-1+pos,1 returns the substring of $_ of length 1 at index -$&-1+pos, where $& is the number just matched, and pos is the index of the start of the match. We just need to redo if the replace was successful in order to replace every digit. (and the result is implicitly printed thanks to -p flag).


Old approach, 47 bytes:

44 bytes of code + -F flag.

map{$F[$i]=$F[$i-$_-1]if/\d/;++$i}@F;print@F

Try it online!

Quite straight forward actually. -F flag splits the inputs on each character into @F. map{...}@F iterates through @F (ie. every character of the input). If the character if a digit (/\d/), then we replace it by the character at index $i-$_-1. The $i is the current index variable (that we maintain by incrementing at each character seen).

Perl 5, 34 bytes

33 bytes of code + -p flag.

s/\d/substr$_,-$&-1+pos,1/e&&redo

Try it online!


Old approach, 47 bytes:

44 bytes of code + -F flag.

map{$F[$i]=$F[$i-$_-1]if/\d/;++$i}@F;print@F

Try it online!

Quite straight forward actually. -F flag splits the inputs on each character into @F. map{...}@F iterates through @F (ie. every character of the input). If the character if a digit (/\d/), then we replace it by the character at index $i-$_-1. The $i is the current index variable (that we maintain by incrementing at each character seen).

Perl 5, 34 bytes

33 bytes of code + -p flag.

s/\d/substr$_,-$&-1+pos,1/e&&redo

Try it online!

s/\d/.../e replace the first digit by ... evaluated as Perl code. (with ... being substr$_,-$&-1+pos,1 in that case. substr$_,-$&-1+pos,1 returns the substring of $_ of length 1 at index -$&-1+pos, where $& is the number just matched, and pos is the index of the start of the match. We just need to redo if the replace was successful in order to replace every digit. (and the result is implicitly printed thanks to -p flag).


Old approach, 47 bytes:

44 bytes of code + -F flag.

map{$F[$i]=$F[$i-$_-1]if/\d/;++$i}@F;print@F

Try it online!

Quite straight forward actually. -F flag splits the inputs on each character into @F. map{...}@F iterates through @F (ie. every character of the input). If the character if a digit (/\d/), then we replace it by the character at index $i-$_-1. The $i is the current index variable (that we maintain by incrementing at each character seen).

added 437 characters in body
Source Link
Dada
  • 8.5k
  • 1
  • 18
  • 44

Perl 5, 4734 bytes

33 bytes of code + -p flag.

s/\d/substr$_,-$&-1+pos,1/e&&redo

Try it online!


Old approach, 47 bytes:

44 bytes of code + -F flag.

map{$F[$i]=$F[$i-$_-1]if/\d/;++$i}@F;print@F

Try it online!

Quite straight forward actually. -F flag splits the inputs on each character into @F. map{...}@F iterates through @F (ie. every character of the input). If the character if a digit (/\d/), then we replace it by the character at index $i-$_-1. The $i is the current index variable (that we maintain by incrementing at each character seen).

Perl 5, 47 bytes

44 bytes of code + -F flag.

map{$F[$i]=$F[$i-$_-1]if/\d/;++$i}@F;print@F

Try it online!

Quite straight forward actually. -F flag splits the inputs on each character into @F. map{...}@F iterates through @F (ie. every character of the input). If the character if a digit (/\d/), then we replace it by the character at index $i-$_-1. The $i is the current index variable (that we maintain by incrementing at each character seen).

Perl 5, 34 bytes

33 bytes of code + -p flag.

s/\d/substr$_,-$&-1+pos,1/e&&redo

Try it online!


Old approach, 47 bytes:

44 bytes of code + -F flag.

map{$F[$i]=$F[$i-$_-1]if/\d/;++$i}@F;print@F

Try it online!

Quite straight forward actually. -F flag splits the inputs on each character into @F. map{...}@F iterates through @F (ie. every character of the input). If the character if a digit (/\d/), then we replace it by the character at index $i-$_-1. The $i is the current index variable (that we maintain by incrementing at each character seen).

Source Link
Dada
  • 8.5k
  • 1
  • 18
  • 44

Perl 5, 47 bytes

44 bytes of code + -F flag.

map{$F[$i]=$F[$i-$_-1]if/\d/;++$i}@F;print@F

Try it online!

Quite straight forward actually. -F flag splits the inputs on each character into @F. map{...}@F iterates through @F (ie. every character of the input). If the character if a digit (/\d/), then we replace it by the character at index $i-$_-1. The $i is the current index variable (that we maintain by incrementing at each character seen).