Dans le chat, nous sommes souvent rapides et ne regardons pas vraiment l' ordre des lettres avant de poster un message. Puisque nous sommes paresseux, nous avons besoin d’un programme qui permute automatiquement les deux dernières lettres de nos mots, mais comme nous ne voulons pas répondre trop tard, le code doit être court.
Votre tâche, si vous souhaitez l'accepter, consiste à écrire un programme qui retourne les deux dernières lettres de chaque mot d'une chaîne donnée (de sorte que le mot Thansk
se transforme en Thanks
). Un mot est une séquence de deux lettres ou plus en alphabet anglais délimitées par un seul espace.
La chaîne / liste de caractères que vous recevez en entrée ne contient que des caractères alphabétiques et des espaces (ASCII [97 - 122], [65 - 90] et 32).
Vous pouvez entrer et fournir des sorties à l'aide de n'importe quelle méthode standard , dans n'importe quel langage de programmation , tout en notant que ces failles sont interdites par défaut.
La sortie peut avoir un espace de fin et / ou une nouvelle ligne.
L'entrée contiendra toujours uniquement des mots (et les espaces correspondants) et consistera en au moins un mot.
C'est du code-golf, donc la soumission la plus courte (en octets), dans chaque langue, gagne!
Cas de test
Notez que les chaînes sont entourées de guillemets pour plus de lisibilité.
Entrée -> Sortie "Thansk" -> "Merci" "Youer welcoem" -> "Vous êtes les bienvenus" "Ceci est une pomme" -> "Thsi si na appel" "Flippign Lettesr Aroudn" -> "Retourner les lettres" "L'AUTRE ÉCHANGE AVEC LES ÉCHANGEURS" -> "L'ÉCHANGE ÉQUITABLE AVEC DES ÉCHANGEURS"
Ou, pour la commodité de la suite de tests, voici les entrées et leurs sorties correspondantes séparément:
Thansk Youer welcoem Ceci est une pomme Flippign Lettesr Aroudn L'AUTRE CHALLEGEN AVEC Swappde LettesR
Merci Vous êtes les bienvenus Thsi si na appel Retourner les lettres autour TOUT CHALLEGENGE AVEC DES LETTRES ÉCHANGÉES
Merci à DJMcMayhem pour le titre. C'était à l'origine un CMC .
Réponses:
V, 4
5bytesTry it online!
||
denotes the cursorThe buffer starts with
|w|ord and more words
and the cursor being on the first character.Recursively
ò
aller au
e
bout d'un motwor|d| and more words
enlève
X
le caractère à gauche du curseurwo|d| and more words
p
éviter le prochain caractèrewod|r| and more words
Implicit ending
ò
, repeat the same process for other words until the end of the buffer is reachedla source
ò
command github.com/DJMcMayhem/V/wiki/Normal-Mode-CommandsJelly, 7 bytes
A monadic link taking and returning lists of characters
Try it online!
How?
la source
Ḳ2œ?ЀK
also works and uses a single quick.Brain-Flak, 122 bytes
Try it online!
The worst language for the job :)
ReadableSlightly more readable version:la source
Haskell, 40 bytes
Try it online! Usage example:
(f=<<).words $ "abc xyz"
yields"acb xzy "
.la source
Retina, 13 bytes
Try it online! Link includes test cases.
la source
Python 3, 50 bytes
Try it online!
This answer abuses Python 3's behavior of print: Multiple arguments are printed with a single space between them. Of course, we can't just give it multiple arguments because we don't know how many words will be in the input. So we use the splat operator. Basically
is exactly the same thing as
Abusing that makes a full program turn out shorter than a function/lambda where we'd have to use
' '.join
or something similar.la source
for w in input().split():print w[:-2]+w[:-3:-1],
. In Python 3, extracting the last two characters would work well withprint(*(''.join(a)+c+b for*a,b,c in input().split()))
except thata
needs to be remade into a string.Matlab (R2016b),
5150 bytesSaved
4950 (!) bytes thanks to @Giuseppe.And my previous answer:
Matlab (R2016b), 100 bytes
(Just for the fun of it :P)
Explanation:
la source
regexprep
work here? Something likeregexprep(a,'(\w*)(\w)(\w)','\1\3\2')
?$1
, and not\1
, so it would beregexprep(a,'(\w*)(\w)(\w)','$1$3$2')
.function s(a),regexprep(a,'(\w)(\w)( |$)','$2$1 ')
is still another byte shorter!C,
625854 bytesThanks to @Dennis for saving
foureight bytes!Try it online!
la source
Prolog (SWI), 60 bytes
Try it online!
Explanation
First we define the base case:
This means that the last two letters will always be swapped.
Then we define what happens if we are right next to a space:
Two strings match if right before a space the letters before the space are swapped and the remainder if the strings match. We then use
!
to cut.Our last case is if we are not next to a space the first two letters need to match.
la source
Wolfram Language, 117 bytes
Try it online!
Applied to the test strings.
la source
R,
1115141 bytesCourtesy of @Giuseppe, a regex approach which blows my old method out of the water.
la source
APL (Dyalog Classic), 28 bytes
⎕ML
and⎕IO
are both1
,Try it online!
Explanation
... (,⊂⍨⊣=,) ...
Split (while keeping borders, and appending a border to the beginning) ...... ⍞
... the input ...... ' ' ...
... at spaces.... ( ... )¨ ...
Then, to each element of that:... , ...
Concatenate ...... (¯2↓⊢) ...
... every item except the last two ...... 2↑⌽ ...
... with the reverse of the last two elements.1↓∊ ...
Finally, return all but the first element of the flattened result.la source
Funky, 34 bytes
Try it online!
la source
Haskell, 45 bytes
-2 bytes thanks to H.PWiz.
Try it online!
la source
J,
20 1911 bytesCredit to @Bolce Bussiere
Try it online!
la source
(1&A.&.>)&.;:
Alice, 24 bytes
Try it online!
Explanation
This forms a loop where the loop body is a linear Ordinal snippet and we execute
' o
in Cardinal mode between every two loop iterations. The latter just prints a space.Unfolding the zigzag structure of the Ordinal code, the linear loop body actually looks like this:
Breaking this down:
la source
h~Z
) instead of four (e10x
), but I'm not seeing a way to adjust the layout to actually save a byte overall with that.brainfuck,
109100 bytesEdit: don’t have to handle one letter words
Try it online!
Prints a trailing space
How It Works
Previous version, 109 bytes
Try it online!
la source
QuadR, 8 bytes
Try it online!
la source
PHP,
119107 bytesEdit: thanks to totallyhuman
Try it online!
la source
$word
a single character variable name?fgets(STDIN)
can be omitted or replaced by$x
too, like not all answers do count the input to their answerstrim()
should be unnecessary.Haskell, 41 bytes
Try it online!
Outputs with a trailing space.
The repeated
' ':r
looks wasteful. Buta%(b:t@(' ':r))=b:a:t
is the same length anda%(b:t)|' ':_<-t=b:a:t
is one byte longer.Haskell, 41 bytes
Try it online!
la source
sed,
2017+1 (-r) = 18 bytesTry it online!
la source
|$
. It's not doing anything. (For it to do what you expect you'd need(.)(.)(\b|$)
, but that's not necessary because\b
already matches the end of the string.)PHP, 65 bytes
requires PHP 7.1 (or later)
takes sentence as separate command line arguments. Run with
-nr
.working on a single string, 77+1 bytes:
Run as pipe with
-nR
.... or try them online.
la source
Java 8, 35 bytes
Port of @TaylorScott's Google Sheets answer, after I golfed two bytes. EDIT: I see it's now a port of Neil's Retina answer after my two golfed bytes.
Explanation:
Try it online.
la source
Google Sheets, 33 Bytes
Anonymous worksheet function that takes input from cell
A1
and outputs to the calling cell-2 Bytes Thanks to @KevinCruijssen for the use of
(.)
over(\w)
la source
(\w)
can be golfed to(.)
if I'm not mistaken. The\b
is already an indication to look for words only. (Not entirely sure though, but it works in Java.)JavaScript (Node.js),
383632 bytesTry it online!
RegExp approach courtesy @Giuseppe (although I thought of this independently), assuming words separated by only one space
-2 for only considering 1 space and add trailing space
-4 Thanks @Shaggy
la source
s=>s.replace(/(.)(.)( +|$)/g,"$2$1$3")
.s=>s.replace(/(.)(.)(\s|$)/g,"$2$1$3")
ab abc abcd abcde abcdef
doesab_
,bc_
,cd_
,de_
,___
,ef_
,___
F=s=>s.replace(/(.)(.)(?!\w)/g,"$2$1")
same lengthK (oK),
2322 bytesTry it online!
Example:
Explanation:
Port of FrownyFrog's solution to save 1 byte.
I'll come back to this.
Previous solution:
" "/-2{(x_y),|x#y}'" "\
23 bytesla source
05AB1E, 7 bytes
Try it online!
-1 thanks to Magic Octopus Urn.
Prints one trailing space.
la source
`
.Jelly, 10 bytes
Try it online!
la source
SNOBOL4 (CSNOBOL4),
136119 bytesTry it online!
Prints with a trailing space.
You know you've done something wrong when a language is a backronym for StriNg Oriented and symBOlic Language and your code is longer than Brain-Flak :(now it's slightly better.Line
B
takesI
and replaces(alphabetic characters saved as Y)(some number of spaces)
with the empty string.The following line extracts the last 2 characters of
Y
asZ
and replaces them asZ
reversed, then the next line concatenatesO
,Y
, and a single space character.Finally, it prints when
I
no longer matches the required pattern in lineB
.la source
Perl 5, 19 + 1 (
-p
) = 20 bytesTry it online!
la source