# Convert phrases to reverse style [duplicate]

### The Challenge

Convert a phrase of two words to reverse style: candy bar -> bar le candy.

### Specifications

• There will be one word (only a-zA-Z0-9 + -), a space (), and then another word.
• For equal length, score is used to determine winner.

### Test cases:

candy bar -> bar le candy
code golf -> golf le code
programming puzzles -> puzzles le programming

• Seems a bit too simple. – orlp Apr 11 '16 at 23:18
• surely it would be (LE) bar DE candy for it to bear any resemblance to actual french. I find the idea that this resembles french to be quite grating. – Level River St Apr 12 '16 at 0:03
• Next-o you-o will-o be telling-o me-o that this is Spanish-o style. It's not French style. It's the style of a meme written by someone who knows nothing about French. If you put bar DE candy it would at least have some degree of authenticity. – Level River St Apr 12 '16 at 0:17
• @NoOneIsHere Hello, World! has a different purpose, it's a pretty good reference of codegolfed source about one of the most common exercise there is. Also, if you really want to point out to this challenge, look at how the specs are specified for something so trivial, it is actually a question of great quality. – Katenkyo Apr 12 '16 at 6:37
• To Closevoters: How is this unclear? It's trivial, sure, but that's not what close votes are for. That's what downvotes are for. – James Apr 12 '16 at 20:41

# CJam, 9 bytes

rr" le "@


Try it online!

Pushes first 2 tokens and then the " le " literal to stack, and then rotates the stack, moving the first word to the end of the stack. Output of the resulting stack is implied.

• Alternatively, " le "rro. – Martin Ender Apr 12 '16 at 8:18
• Too bad CJam doesn't do implicit input as well. – Cyoce Apr 13 '16 at 6:40

# Reng v.3.3, 55 bytes

Try it here!

1#zaií1ø
):Weq!vz1+#z
br[zÀz/]; !o
le"Wro/ !ob"
~;/


I can probably golf it by taking input until a space is found, as this approach takes all input at once.

## Explanation

### Input & init

1#zaií1ø


1#z stores 1 to z. a is a one-sided mirror from the left side, meaning you can pass through it from left-to-right, but not otherwise. i takes a char of input, and í mirrors iff there is input on the input stack. Otherwise, it proceeds. 1ø goes to the next line.

### Print first word

):Weq!vz1+#z


) rotates the stack left one, : duplicates it, We checks for the equality with a space, and !v goes down iff it is a space and goes to the next phase. z1+#z increments z otherwise.

(more to come)

Here's a GIF:

# Vim 9 keystrokes

dwA le <esc>p


I'm so close to beating pyth and Cjam! Unfortunately, I can't figure out how to take any more off, so I'll have to be satisfied with tying. =)

Explanation:

dw             'Delete a word
A le <esc>   'Enter " le " at the end of the current line
p  'Paste the previously deleted text


# Pyth - 9 bytes

j" le "_c

• Ninja'd me and I didn't know you could have implicit z? – orlp Apr 11 '16 at 23:21
• @orlp its implicit Q, i'm taking input in quotes. – Maltysen Apr 11 '16 at 23:22
• This kinda looks like french, "j'le _c" :P – Downgoat Apr 11 '16 at 23:55

# MATL, 12 bytes

Yb'le'h7L)Zc


Try it online!

Yb       % Take input implicitly. Split by spaces. Gives a cell array
'le'     % Push this string
h        % Concatenate horizontally into cell array
7L       % Push [2 3 1]
)        % Apply as index, to change order
Zc       % Join with spaces. Gives a string. Display implicitly

• Wait... How does 7L -> [2 3 1] ? – Cyoce Apr 13 '16 at 6:44
• @Cyoce It's the default content of level 7 of clipboard L. You can see them all here, table 2, pae 15 – Luis Mendo Apr 13 '16 at 9:33

## Python 3, 41 bytes

print(' le '.join(input().split()[::-1]))

• Good first golf! Welcome to the site! – James Apr 12 '16 at 2:27
• @DrGreenEggsandHamDJ thanks! I wasn't sure if it was appropriate because it's so similar to the other two Python answers, but this game seems really fun. – user161778 Apr 12 '16 at 2:43
• Welcome to Programming Puzzles & Code Golf! Unfortunately, this will only produce output inside a REPL. By default, we require functions or full programs, which use these methods for I/O. – Dennis Apr 12 '16 at 2:49
• @Dennis, darn well I'll update, but it's gonna cost me. Thanks! – user161778 Apr 12 '16 at 2:51

# R, 30 bytes

cat(rev(scan(,"")),sep=" le ")


This is a full program that accepts reads a string from STDIN and prints to STDOUT. The scan function implicitly separates space-delimited input into elements of an array. We then just reverse the array and print it, using le as a separator between elements in the output.

# GS2, 9 bytes

, ♦ le ♣2


Try it online!

• Looks like 9 is the magic number for this challenge. – Cyoce Apr 13 '16 at 15:01

# Jelly, 9 bytes

ṣ⁶Ṛj“ le


Try it online!

# ><>, 35 bytes

i:" "=?v
ov?(0:i<
"\~~r" el
o>l?!;


The first line reads a character of the first word and put it on the stack, then goes to the second line if it encounters " " or otherwise loops.
The second line reads a character of the second word and prints it right away, then goes to the third line when if encounters EOF or otherwise loops.
The third line removes the space and the EOF from the stack, reverts it and pushes " le " backward, then goes to the fourth line.
The fourth line stops execution if the stack is empty, otherwise it prints a character of the stack (poping it) and loops.

# IPOS, 13 bytes

rS!r%S" le "R


Could < 10 bytes if I had implemented all the builtins that I have planned...

### Explanation

r         # Reverse the input
S!r%      # Reverse every word -> Word order is now swapped
S" le "R  # insert a " le " between the two words by replacing the space


### Non-competing 9-byte version

This only works with the version I pushed just now (implemented C).

SC"le"@Sj


This works by splitting the input on spaces to obtain the two words (SC), pushing the literal le, rotating the stack (@) so the words are swapped and the le is between them and finally joining the stack on spaces (SJ).

Again, could be shorter since I also plan to add commands for joining and splitting on spaces which would make this 7 bytes. But since those are probably gonna be non-ASCII characters and I haven't fully figured out the codepage I am gonna use, those have to wait a bit.

# JavaScript (ES6), 34 32 bytes

s=>(a=s.split )[1]+" le "+a[0]


Saved 2 bytes thanks to @jrich!

• s=>(a=s.split )[1]+" le "+a[0] – jrich Apr 13 '16 at 1:07
• Wow, just now, I independently came up with this. – NoOneIsHere Feb 22 '17 at 19:14

## Pyke, 10 bytes

dcX"le"RdJ


Try it here!

Or 9 bytes (noncompeting, add insert)

Dd@" le":


Try it here!

• Shouldn't it be "le" ? – Cyoce Apr 13 '16 at 15:02

# Python, 37 bytes

lambda s:" le ".join(s.split()[::-1])

• Won't this give code golf -> code le golf? – NoOneIsHere Apr 11 '16 at 23:17
• @NoOneIsHere Misunderstood the problem, fixed now. – orlp Apr 11 '16 at 23:18

## Python 2, 34 bytes

a,b=input().split();print b,"le",a


Expects input in quotes.

>> "code golf"
golf le code


# Jolf, 10 bytes

Try it here!

R_pti" le
pti      input split by spaces
_         reversed
R    " le  joined by that string


# BATCH file, 14 bytes

@ECHO %2 le %1


Save to a file and invoke as notFrench word1 word2.

C:\Users\Conor O'Brien\Documents\Programming\PPCG>notFrench code golf
golf le code

C:\Users\Conor O'Brien\Documents\Programming\PPCG>

• I'm not sure if that's allowed. You're basically making the shell split the string for you. – Dennis Apr 12 '16 at 3:33
• @Dennis I see. Gimme a moment to revise. – Conor O'Brien Apr 12 '16 at 3:36
• Can't you use echo instead of @ECHO? – NoOneIsHere Apr 12 '16 at 18:22
• @NoOneIsHere Nope, this well also output the command. – Conor O'Brien Apr 12 '16 at 18:40

# Go, 81 bytes

import."strings"
func f(s string)string{h:=Split(s," ")
return h[1]+" le "+h[0]}


# Perl, 18 bytes

/ /;$_="$' le $"  This program is 17 bytes long and requires the -p switch (+1 byte). ## Lua, 46 Bytes a=(...):gsub("(%w+) (%w+)","%2 le %1")print(a)  I save the result of gsub to a variable because it returns 2 values, and would therefore print a 1 which is the number of changes made to the string. The other work around is about disallowing gsub's return to unpack. Also, it is actually longer by 1 byte: print((...):gsub("(%w+) (%w+)","%2 le %1")..'')  ## Retina, 14 bytes Byte count assumes ISO 8859-1 encoding. M!r\S+ ¶ le  Try it online! (Slightly modified to process all test cases at once.) The first stage reverses the two words and separates them by a linefeed (this is done by matching them with right-to-left mode and printing all matches). The second stage replaces the linefeed (¶) with  le . • Do you need the...! should imply match mode. (And plus who uses Match without ! except at the end anyway?) – CalculatorFeline Apr 13 '16 at 1:32 • @CatsAreFluffy You're right, a lot of options are currently exclusive to certain stage types, so they could imply the stage type, but I think I'd rather leave the option of reusing the characters for different options in different modes at some point. As for defaulting to ! before the final stage, that might be worth thinking about... – Martin Ender Apr 13 '16 at 9:40 • Do it! But be careful, as M without ! does occur, but not very often (see the number groups challenge) – CalculatorFeline Apr 13 '16 at 16:15 # Mathematica, 30 bytes #2<>" le "<>#&@@StringSplit@#&  Not very complicated... ## Seriously, 13 bytes "le"' ,so/' j  Try it online Explanation: "le"' ,so/' j ' ,s split input on space "le" o/ stick "le" between words ' j join on space  # Gawk, 17 16 bytes 1 byte off thanks to muru {$0=$2" le "$1}1


### Old

{print$2" le "$1}


# Java 8, 41 39 bytes

A simple lambda, for a simple challenge

s->s.replaceAll("(.+) (.+)","$2 le$1")


Update

• -3 [17-02-23] Thanks to @Kevin
• This is shorter though: s->s.replaceAll("(.+) (.+)","$2 le$1") (39 bytes) :) – Kevin Cruijssen Feb 23 '17 at 9:41

# Japt, 9 8 bytes

Note the trailing space.

¸w q ¤

• Saved a byte thanks to a reminder from obarakon.

Try it online

## Explanation

        :Implicit input of string U
¸       :Split to array on space
w       :Reverse
q.      :Join to string...
¤     : with the compressed string " le "

• Don't forget about string compression ;) – Oliver May 25 '17 at 4:23
• Oh, yeah; really gotta do better at remembering that! Ta, @obarakon – Shaggy May 25 '17 at 7:38

# C# - 43 bytes

string.Join(" le ",x.Split(' ').Reverse());

• This is not a program or function; this is a snippet. However, you can add x=> to the beginning to make it a lambda function. – LegionMammal978 Apr 14 '16 at 0:31

# Ruby, 24 bytes

->s{s=~/ /;"#$' le #$"}


Try it online!

# PHP, 47 Bytes

<?=preg_filter("#(.+) (.+)#","$2 le$1",\$argn);
`

Try it online!