Skip to main content
Tweeted twitter.com/StackCodeGolf/status/882341322576756737
edited tags
Link
DJMcMayhem
  • 59.6k
  • 17
  • 196
  • 348
Source Link
Jim
  • 1.5k
  • 9
  • 15

Swap letter and digit runs

Given an input string containing only alphanumeric ASCII characters and starting with a letter, swap each letter run with the digit run which follows.

A run is a sequence of consecutive letters or digits. Note that in the case where the input string ends with a run of letters, this run is left untouched.

Walk-through example

For instance, given the input string uV5Pt3I0:

  1. Separate runs of letters and runs of digits: uV 5 Pt 3 I 0
  2. Identify pairs of runs: (uV 5) (Pt 3) (I 0)
  3. Swap pairs of runs: (5 uV) (3 Pt) (0 I)
  4. Concatenate: 5uV3Pt0I

Examples

uV5Pt3I0 -> 5uV3Pt0I
J0i0m8 -> 0J0i8m
abc256 -> 256abc
Hennebont56Fr -> 56HennebontFr
Em5sA55Ve777Rien -> 5Em55sA777VeRien
nOoP -> nOoP

This is so the shortest answer in bytes wins. Explanations are encouraged.