Flippign Lettesr Aroudn


33

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 Thanskse 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 .


Pouvons-nous sortir un tableau de mots?
Shaggy

@Shaggy Non, la sortie doit être une chaîne (ou une liste de caractères par défaut)
M. Xcoder

Pouvons-nous demander un espace de fin sur chaque entrée?
FlipTack

@FlipTack Cela a été autorisé dans la version initiale, mais j'ai supprimé cette règle avant toute réponse qui aurait utilisé celle-ci. (en partie parce que certains utilisateurs du chat m'ont dit que je simplifiais les choses autrement, et que je suis d'accord avec eux). Non, ce n'est pas permis.
M. Xcoder

1
@Fabian Un mot est une séquence de deux lettres ou plus
M. Xcoder

Réponses:


16

V, 4 5 bytes

òeXp

Try it online!

|| denotes the cursor

The buffer starts with |w|ord and more words and the cursor being on the first character.

Recursively ò

aller au ebout d'un mot

wor|d| and more words

enlève Xle caractère à gauche du curseur

wo|d| and more words

péviter le prochain caractère

wod|r| and more words

Implicit ending ò, repeat the same process for other words until the end of the buffer is reached


2
The right language for the task :)
DJMcMayhem

Do you mean "Repeatedly" instead of "Recursively"?
NieDzejkob

@NieDzejkob The V wiki uses the word "recursively" to describe the ò command github.com/DJMcMayhem/V/wiki/Normal-Mode-Commands
Kritixi Lithos

10

Jelly, 7 bytes

Ḳœ?@€2K

A monadic link taking and returning lists of characters

Try it online!

How?

Ḳœ?@€2K - Link: list of characters
Ḳ       - split at spaces
     2  - literal two
    €   - for €ach:
   @    -   with sw@pped arguments:
 œ?     -     nth permutation (the 2nd permutation has the rightmost elements swapped)
      K - join with spaces

That's a nice abuse of permutations. Alternative
Mr. Xcoder

@Mr.Xcoder Ḳ2œ?ЀK also works and uses a single quick.
Dennis

7

Brain-Flak, 122 bytes

{(({})[((((()()){}){}){}){}])((){[()](<{}>)}{}){{}<>(({}({}))[({}[{}])])(<>)}{}({}<>)<>}<>(({}({}))[({}[{}])]){({}<>)<>}<>

Try it online!

The worst language for the job :)

Readable Slightly more readable version:

{
    (({})[((((()()){}){}){}){}])((){[()](<{}>)}{})

    {
        {}
        <>

        (({}({}))[({}[{}])])

        (<>)
    }
    {}

    ({}<>)<>

}<>

(({}({}))[({}[{}])])

{

    ({}<>)
    <>
}<>

I can't believe this is longer than the Brainfuck version...
Pureferret

@pureferret Brain-flak tends to be longer than brainfuck. Mostly cause it requires two bytes per primitive command, where brain-flak requires two.
DJMcMayhem

7

Haskell, 40 bytes

(f=<<).words
f[a,b]=b:a:" "
f(x:r)=x:f r

Try it online! Usage example: (f=<<).words $ "abc xyz" yields "acb xzy ".


So you're telling me the shortest approach is both the approaches combined? >_<
totallyhuman


6

Python 3, 50 bytes

print(*(w[:-2]+w[:-3:-1]for w in input().split()))

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

print(*[a,b,c])

is exactly the same thing as

print(a,b,c)

Abusing that makes a full program turn out shorter than a function/lambda where we'd have to use ' '.join or something similar.


Looks like Python 2 saves 2 bytes by writing for w in input().split():print w[:-2]+w[:-3:-1],. In Python 3, extracting the last two characters would work well with print(*(''.join(a)+c+b for*a,b,c in input().split())) except that a needs to be remade into a string.
xnor

5

Matlab (R2016b), 51 50 bytes

Saved 49 50 (!) bytes thanks to @Giuseppe.

function s(a),regexprep(a,'(\w)(\w)( |$)','$2$1 ')

And my previous answer:

Matlab (R2016b), 100 bytes

(Just for the fun of it :P)

function s(a),a=regexp(a,' ','split');for i=1:length(a),fprintf('%s ',a{i}([1:end-2 end end-1])),end

Explanation:

function s(a) % Defining as a function...
a=regexp(a,' ','split'); % Splits the input string at the spaces
for i=1:length(a) % Loops through each word
    fprintf('%s ',a{i}([1:end-2 end end-1])) % And prints everything with the last two characters swapped.
end

1
one character words can't happen, as a word is defined to be at least two characters.
Giuseppe

would regexprep work here? Something like regexprep(a,'(\w*)(\w)(\w)','\1\3\2')?
Giuseppe

D= This. Was. Epic! I think you should post this answer, since it's totally different from mine. The only thing is that Matlab references the matches with $1, and not \1, so it would be regexprep(a,'(\w*)(\w)(\w)','$1$3$2').
Thiago Oleinik

1
you should post it as a separate answer / in this answer; it's always good to see if a regex would help or not on a string challenge! Besides, I clearly don't understand MATLAB's regex engine, so it wouldn't quite be fair for me to take credit for it.
Giuseppe

1
function s(a),regexprep(a,'(\w)(\w)( |$)','$2$1 ') is still another byte shorter!
Giuseppe

5

C,  62   58  54 bytes

Thanks to @Dennis for saving  four  eight bytes!

f(char*s){s[1]>32||(*s^=s[-1]^=*s^=s[-1]);*++s&&f(s);}

Try it online!


ah, the xor-based swap
Snowbody

4

Prolog (SWI), 60 bytes

[A,B]+[B,A].
[A,B,32|U]+[B,A,32|Y]:-U+Y,!.
[A|U]+[A|Y]:-U+Y.

Try it online!

Explanation

First we define the base case:

p([A,B],[B,A]).

This means that the last two letters will always be swapped.

Then we define what happens if we are right next to a space:

p([A,B,32|U],[B,A,32|Y]):-p(U,Y),!.

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.

p([A|U],[A|Y]):-p(U,Y).

4

Wolfram Language, 117 bytes

StringReplace[RegularExpression["\\b[[:alpha:]]{2,}\\b"]:>StringDrop[StringInsert["$0",StringTake["$0",{-1}],-3],-1]]

Try it online!

Applied to the test strings.

StringReplace[
  RegularExpression["\\b[[:alpha:]]{2,}\\b"] :> 
   StringDrop[StringInsert["$0", StringTake["$0", {-1}], -3], -1]] /@
 {"Thansk", "Youer welcoem", "This is an apple", 
  "Flippign Lettesr Aroudn", "tHe oDd chALlEneg wiht swappde lettesR"} // Column
Thanks
Youre welcome
Thsi si na appel
Flipping Letters Around
teH odD chALlEnge with swapped letteRs

4
Welcome to PPCG!
Steadybox

@Steadybox Thanks.
Edmund

4

R, 111 51 41 bytes

Courtesy of @Giuseppe, a regex approach which blows my old method out of the water.

cat(gsub("(.)(.)\\b",'\\2\\1',scan(,"")))

1
regex are much more efficient here: Try it online!
Giuseppe

(not that I don't appreciate the guts it takes to do a pure string manipulation approach in R)
Giuseppe


@Giuseppe Wow, nice work! I've edited them into my answer, although if you'd prefer to make your own answer please go ahead!
rturnbull

1
nah, don't worry about it. I golfed down another 10 bytes: porting another regex approach, and a 70 byte of your old approach
Giuseppe

4

APL (Dyalog Classic), 28 bytes

1↓∊((¯2↓⊢),2↑⌽)¨' '(,⊂⍨⊣=,)⍞

⎕ML and ⎕IO are both 1,

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.

return all but the first
Adám



3

J, 20 19 11 bytes

Credit to @Bolce Bussiere

1&A.&.>&.;:

Try it online!

       &.;:      on words
    &.>          on each
  A.             apply the permutation
1&               number 1, swap the last two elements

1
13 Bytes with (1&A.&.>)&.;:
Bolce Bussiere

@BolceBussiere perfect
FrownyFrog

Could you add an explanation? Wondering if I can port it to K to reduce the embarrassing byte count of my solution!
streetster

3

Alice, 24 bytes

/0RR'.%$1\' o
\ix*o ne@/

Try it online!

Explanation

/...\' o
\.../

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:

iR*' %e10xRo.n$@

Breaking this down:

i     Read all input. On subsequent iterations, this will push an empty string.
R     Reverse.
*     Join. On the first iteration, this joins the input to an implicit empty string,
      which does nothing. On subsequent iterations, it will join the empty string to
      the word on top of the string, thereby getting rid of the empty string.
' %   Split around spaces. On the first iteration, this will split the input
      into individual words. On subsequent iterations, this does nothing.
e10   Push "10".
x     Use this to permute the (reversed) word on top of the stack. In
      particular, the word is rearranged with the same permutation that is
      required to sort the string "10", which means the first two letters
      get swapped (which correspond to the last two letters of the actual
      word).
R     Reverse the swapped word.
o     Print it.
.n$@  If there are no words left on the stack, terminate the program.

Just noticed that the letter swap can be done in three bytes (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.
Martin Ender

2

brainfuck, 109 100 bytes

Edit: don’t have to handle one letter words

,[>++++[-<-------->],]>+[-<[>++++[<++++++++>-]<[->>+<<]<]<<[->>+<<]>[[-<+>]>]<<[>+>+>]-<]>>>>>>>[.>]

Try it online!

Prints a trailing space

How It Works

,[>++++[-<-------->],] Puts input on the tape and subtracts 32 from each character
                       This separates each word

>+[- Start the loop
   <[>++++[<++++++++>-]<[->>+<<]<] Add 32 to each letter of the word
                                   Skip this on the first iteration for the last word

   <<[->>+<<]>[[-<+>]>] Swaps the last two letters of the word
   <<[>+>+>]- If there is another word to the left continue loop
              Also set up to add a space to the end of the word
 <] End loop
 >>>>>>>[.>] Print the modified string

Previous version, 109 bytes

,[>++++[-<-------->],]>+[-<[>++++[<++++++++>-]<[->>+<<]<]<<[[->>+<<]>[[-<+>]>]<<[<]]>[>]<[>+>+>]-<]>>>>>>[.>]

Try it online!



1

PHP, 119 107 bytes

Edit: thanks to totallyhuman

<?php foreach(explode(" ",trim(fgets(STDIN)))as$w)echo substr($w,0,strlen($w)-2).strrev(substr($w,-2))," ";

Try it online!


1
Can't you make $word a single character variable name?
totallyhuman

@totallyhuman Yup! I wrote the full version and then compressed it, but didn't notice that. Thanks you.
Zerquix18

PHP open tags can be omitted in the answer saving you 6 bytes.
Daniel W.

I wonder if fgets(STDIN) can be omitted or replaced by $x too, like not all answers do count the input to their answers
Daniel W.

trim() should be unnecessary.
Titus

1

Haskell, 41 bytes

foldr(%)" "
a%(b:' ':r)=b:a:' ':r
a%s=a:s

Try it online!

Outputs with a trailing space.

The repeated ' ':r looks wasteful. But a%(b:t@(' ':r))=b:a:t is the same length and a%(b:t)|' ':_<-t=b:a:t is one byte longer.


Haskell, 41 bytes

f(a:b:t)|t<"A"=b:a:f t|1>0=a:f(b:t)
f e=e

Try it online!


1

sed, 20 17+1 (-r) = 18 bytes

s/(.)(.)\b/\2\1/g

Try it online!


The TIO link does not match your posted code. The TIO link is a few bytes longer.
Xcali

Whoops, fixed the link
Noskcaj

You can remove |$. 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.)
Jordan

Whoops, meant to get rid of that. Thanks,
Noskcaj

1

PHP, 65 bytes

requires PHP 7.1 (or later)

for(;$s=$argv[++$i];$s[-1]=$s[-2],$s[-2]=$c,print"$s ")$c=$s[-1];

takes sentence as separate command line arguments. Run with -nr.


working on a single string, 77+1 bytes:

foreach(explode(" ",$argn)as$s){$c=$s[-1];$s[-1]=$s[-2];$s[-2]=$c;echo"$s ";}

Run as pipe with -nR.


... or try them online.


1

Java 8, 35 bytes

s->s.replaceAll("(.)(.)\\b","$2$1")

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.

s->                           // Method with String as both parameter and return-type
   s.replaceAll("(.)(.)       //  Replace any two characters,
                       \\b",  //  with a word-boundary after it (space or end of String)
                "$2$1")       //  With the two characters swapped

1

Google Sheets, 33 Bytes

Anonymous worksheet function that takes input from cell A1 and outputs to the calling cell

=RegExReplace(A1,"(.)(.)\b","$2$1

-2 Bytes Thanks to @KevinCruijssen for the use of (.) over (\w)


Both (\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.)
Kevin Cruijssen

@KevinCruijssen - You are absolutely correct, it can be. Thank you!
Taylor Scott

1

JavaScript (Node.js), 38 36 32 bytes

s=>s.replace(/(.)(.)( |$)/g,"$2$1 ") 
s=>s.replace(/(.)(.)\b/g,"$2$1")

Try 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


Doesn't matter if there are more spaces, I think
l4m2

@l4m2 But if there are more spaces then it will become a 38 for s=>s.replace(/(.)(.)( +|$)/g,"$2$1$3").
Shieru Asakoto

@l4m2 BTW my original answer was s=>s.replace(/(.)(.)(\s|$)/g,"$2$1$3")
Shieru Asakoto

ab abc abcd abcde abcdef does ab_, bc_, cd_, de_, ___, ef_, ___
l4m2

1
F=s=>s.replace(/(.)(.)(?!\w)/g,"$2$1") same length
l4m2

1

K (oK), 23 22 bytes

" "/{x@prm[!#x]1}'" "\

Try it online!

Example:

" "/{x@prm[!#x]1}'" "\"Hello World"
"Helol Wordl"

Explanation:

Port of FrownyFrog's solution to save 1 byte.

I'll come back to this.

" "/{prm[x]1}'" "\ / the solution
              " "\ / split input on " "
    {       }'     / apply lambda to each
     prm[x]        / permute input x
           1       / and take the 2nd result
" "/               / join with " "

Previous solution:

  • " "/-2{(x_y),|x#y}'" "\ 23 bytes

1

05AB1E, 7 bytes

#vy`sðJ

Try it online!

-1 thanks to Magic Octopus Urn.

Prints one trailing space.


This is 11 bytes
Daniel W.

2
@DanFromGermany No, 05AB1E has a code page in which this can be represented as 8 bytes.
Erik the Outgolfer

Can you run the program represented in 8 bytes?
Daniel W.

@DanFromGermany Yes, the 05AB1E interpreter can run this program from a file in the 05AB1E encoding.
Erik the Outgolfer

1
@MagicOctopusUrn It's not a list though, it's after `.
Erik the Outgolfer


0

SNOBOL4 (CSNOBOL4), 136 119 bytes

	I =INPUT
B	I SPAN(&LCASE &UCASE) . Y ARBNO(' ') =:F(O)
	Y RPOS(2) REM . Z =REVERSE(Z)
	O =O Y ' '	:(B)
O	OUTPUT =O
END

Try 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 takes I 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 as Z and replaces them as Z reversed, then the next line concatenates O, Y, and a single space character.

Finally, it prints when I no longer matches the required pattern in line B.


En utilisant notre site, vous reconnaissez avoir lu et compris notre politique liée aux cookies et notre politique de confidentialité.
Licensed under cc by-sa 3.0 with attribution required.