Biorąc pod uwagę ciąg wejściowy zawierający tylko alfanumeryczne znaki ASCII i rozpoczynający się na literę, zamień każdą kolejną literę na następną cyfrę.
Prowadzony jest ciągiem następujących po sobie liter lub cyfr. Zauważ, że w przypadku, gdy ciąg wejściowy kończy się ciągiem liter, bieg ten pozostaje nietknięty.
Przykład przejścia
Na przykład, biorąc pod uwagę ciąg wejściowy uV5Pt3I0
:
- Oddzielne serie liter i cyfry:
uV 5 Pt 3 I 0
- Zidentyfikuj pary przebiegów:
(uV 5) (Pt 3) (I 0)
- Zamień pary biegów:
(5 uV) (3 Pt) (0 I)
- Powiązać:
5uV3Pt0I
Przykłady
uV5Pt3I0 -> 5uV3Pt0I
J0i0m8 -> 0J0i8m
abc256 -> 256abc
Hennebont56Fr -> 56HennebontFr
Em5sA55Ve777Rien -> 5Em55sA777VeRien
nOoP -> nOoP
To jest golf golfowy, więc wygrywa najkrótsza odpowiedź w bajtach. Wyjaśnienia są zachęcane.
(a,(b,y))<-span(<':')<$>span(>'9')s
.(a,(b,y):_)<-lex<$>span(>'9')s
: Try it online!lex
works, so I'm gonna refrain from including that for now. In any case, good to know that there's something like that in PreludeJavaScript (ES6), 34 bytes
Try it
źródło
Pyth, 15 bytes
Explanation
Test suite.
źródło
Python 2, 49 bytes
Every non-regex solution I tried was longer. :P
Try it online!
źródło
Japt (v2.0a0), 16 bytes
Test it online!
Note: this is an unstable alpha, so if this link breaks, you can use a slightly longer version in v1.4.4: Test it online!
Explanation
źródło
ò
.CJam,
323028 bytesCJam has no regex and no "split into digits and letters" or whatnot, so this was kind of painful.
Try it online!
Explanation
źródło
Gema, 11 characters
Sample run:
źródło
Java 8, 38 bytes
Not much to explain. Uses the same method as @Okx's Retina answer, which can't be any shorter in Java.
Try it here.
źródło
Japt, 18 bytes
Test it
źródło
Sed, 29 bytes
Run with -r.
Uses capture groups and replaces them in the opposite order.
źródło
[A-Za-z]
to[^0-9]
. However, you have to count the flag as part of your code.sed <command>
andsed -r <command>
, so three bytes.sed -f filename
andsed -rf filename
(or betweensed -e 'command'
andsed -re 'command'
): a single byte.s/([a-z]+)([0-9]+)|([0-9]+)([a-z]+)/\2\1\4\3/gi
at 48 bytes. Otherwise, much the same.Jelly, 12 bytes
Try it online!
Explanation:
źródło
PHP, no regex, 73 bytes
Run as pipe with
-nR
or test it online.breakdown
źródło
~
instead ofa&
PHP, 45 bytes
Try it online!
źródło
C#, 71 bytes
Just a shame regular expressions are so long in C#.
Try it online!
Full/Formatted version:
źródło
Clojure,
10488 bytesOh regex is really handy... anyway (TIO):
partition-by
splits into consecutive runs based on the return value of that function,partition-all
splits into partitions of 2 (the pairs we'll be swapping),map reverse
reverses them,flatten
gets rid of nested list structure and finally we'll output a string. Ifpartition
was used instead ofpartition-all
and we had odd number of chunks then the last one would be discarded.Original used verbose but fun
(juxt second first)
and(set"0123456789")
instead ofreverse
and ASCII integer ranges.źródło
QuadR, 15 bytes
Try it online!
Explanation blatantly stolen from Okx:
This replaces the regex
(\D+)(\d+)
with\2\1
. Let's break that down if you don't know what that means.The
\D
means 'match anything that isn't a number'.\d
means 'match everything that is a number'. The+
sign means 'match this at least once but try to match it as many times as possible'. The brackets define a group. The first group is(\D+)
and the second is(\d+)
In the second line we say that we want to put whatever was matched by the second group, followed by whatever was matched by the first group. This effectively swaps the letter and digit runs.
źródło
PowerShell, 40 bytes
Try it online!
PowerShell is pretty ideal for this, seeing that it supports regex search-and-replace out of box. Props go to @Okx for the regex solution.
źródło
Pip, 17 bytes
Takes input as a command-line argument. Try it online!
Explanation
This uses the standard regex-replacement strategy, somewhat golfed.
The regex is
-C+XL.C+XD
, which evaluates to`(?i)([a-z]+)(\d+)`
:The replacement is
{c.b}
, a callback function that concatenates the second group (c
) and the first group (b
). (The first argument to the function,a
, contains the whole match.)This is three bytes shorter than the naive
aR`(\D+)(\d+)``\2\1`
.źródło
brainfuck, 98 bytes
Try it online!
Explanation
This program maintains a queue of letters than haven't been output yet, and outputs them when appropriate.
The key to this program is
>>----[---->+<<<-[>]>]
. The three cells right of the input cell start out at zero. If the input is a code point between 1 and 63 inclusive, this moves the pointer one space right and places the input two spaces right of this new position. Otherwise, the pointer moves two spaces right, the cell one space right of the new position becomes 63, and the same 63 is subtracted from the input cell. This neatly divides the input into letters (65-122) and digits (48-57).źródło
Ruby, 31 bytes
Try it online!
źródło
Mathematica, 129 bytes
źródło