-4
\$\begingroup\$

This question already has an answer here:

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
\$\endgroup\$

marked as duplicate by DJMcMayhem code-golf May 24 '17 at 16:19

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 4
    \$\begingroup\$ Seems a bit too simple. \$\endgroup\$ – orlp Apr 11 '16 at 23:18
  • 10
    \$\begingroup\$ 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. \$\endgroup\$ – Level River St Apr 12 '16 at 0:03
  • 9
    \$\begingroup\$ 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. \$\endgroup\$ – Level River St Apr 12 '16 at 0:17
  • 4
    \$\begingroup\$ @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. \$\endgroup\$ – Katenkyo Apr 12 '16 at 6:37
  • 8
    \$\begingroup\$ 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. \$\endgroup\$ – DJMcMayhem Apr 12 '16 at 20:41

29 Answers 29

8
\$\begingroup\$

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.

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

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

explanation

\$\endgroup\$
9
\$\begingroup\$

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
\$\endgroup\$
7
\$\begingroup\$

Pyth - 9 bytes

j" le "_c

Test Suite.

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

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

Python 3, 41 bytes

print(' le '.join(input().split()[::-1]))
\$\endgroup\$
  • \$\begingroup\$ Good first golf! Welcome to the site! \$\endgroup\$ – DJMcMayhem Apr 12 '16 at 2:27
  • \$\begingroup\$ @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. \$\endgroup\$ – user161778 Apr 12 '16 at 2:43
  • \$\begingroup\$ 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. \$\endgroup\$ – Dennis Apr 12 '16 at 2:49
  • \$\begingroup\$ @Dennis, darn well I'll update, but it's gonna cost me. Thanks! \$\endgroup\$ – user161778 Apr 12 '16 at 2:51
3
\$\begingroup\$

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.

\$\endgroup\$
3
\$\begingroup\$

GS2, 9 bytes

, ♦ le ♣2

Try it online!

\$\endgroup\$
  • 1
    \$\begingroup\$ Looks like 9 is the magic number for this challenge. \$\endgroup\$ – Cyoce Apr 13 '16 at 15:01
2
\$\begingroup\$

Jelly, 9 bytes

ṣ⁶Ṛj“ le 

Try it online!

\$\endgroup\$
2
\$\begingroup\$

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

\$\endgroup\$
2
\$\begingroup\$

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.

\$\endgroup\$
2
\$\begingroup\$

JavaScript (ES6), 34 32 bytes

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

Saved 2 bytes thanks to @jrich!

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

Pyke, 10 bytes

dcX"le"RdJ

Try it here!

Or 9 bytes (noncompeting, add insert)

Dd@" le":

Try it here!

\$\endgroup\$
  • \$\begingroup\$ Shouldn't it be "le" ? \$\endgroup\$ – Cyoce Apr 13 '16 at 15:02
1
\$\begingroup\$

Python, 37 bytes

lambda s:" le ".join(s.split()[::-1])
\$\endgroup\$
  • \$\begingroup\$ Won't this give code golf -> code le golf? \$\endgroup\$ – NoOneIsHere Apr 11 '16 at 23:17
  • \$\begingroup\$ @NoOneIsHere Misunderstood the problem, fixed now. \$\endgroup\$ – orlp Apr 11 '16 at 23:18
1
\$\begingroup\$

Python 2, 34 bytes

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

Expects input in quotes.

>> "code golf"
golf le code
\$\endgroup\$
1
\$\begingroup\$

Jolf, 10 bytes

Try it here!

R_pti" le 
  pti      input split by spaces
 _         reversed
R    " le  joined by that string
\$\endgroup\$
1
\$\begingroup\$

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

Go, 81 bytes

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

Perl, 18 bytes

/ /;$_="$' le $`"

This program is 17 bytes long and requires the -p switch (+1 byte).

\$\endgroup\$
1
\$\begingroup\$

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")..'')
\$\endgroup\$
1
\$\begingroup\$

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 .

\$\endgroup\$
  • \$\begingroup\$ Do you need the...! should imply match mode. (And plus who uses Match without ! except at the end anyway?) \$\endgroup\$ – CalculatorFeline Apr 13 '16 at 1:32
  • \$\begingroup\$ @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... \$\endgroup\$ – Martin Ender Apr 13 '16 at 9:40
  • \$\begingroup\$ Do it! But be careful, as M without ! does occur, but not very often (see the number groups challenge) \$\endgroup\$ – CalculatorFeline Apr 13 '16 at 16:15
1
\$\begingroup\$

Mathematica, 30 bytes

#2<>" le "<>#&@@StringSplit@#&

Not very complicated...

\$\endgroup\$
1
\$\begingroup\$

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
\$\endgroup\$
1
\$\begingroup\$

Gawk, 17 16 bytes

1 byte off thanks to muru

{$0=$2" le "$1}1

Old

{print$2" le "$1}
\$\endgroup\$
1
\$\begingroup\$

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
\$\endgroup\$
  • 1
    \$\begingroup\$ This is shorter though: s->s.replaceAll("(.+) (.+)","$2 le $1") (39 bytes) :) \$\endgroup\$ – Kevin Cruijssen Feb 23 '17 at 9:41
1
\$\begingroup\$

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 "
\$\endgroup\$
  • \$\begingroup\$ Don't forget about string compression ;) \$\endgroup\$ – Oliver May 25 '17 at 4:23
  • \$\begingroup\$ Oh, yeah; really gotta do better at remembering that! Ta, @obarakon \$\endgroup\$ – Shaggy May 25 '17 at 7:38
0
\$\begingroup\$

C# - 43 bytes

string.Join(" le ",x.Split(' ').Reverse());
\$\endgroup\$
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – LegionMammal978 Apr 14 '16 at 0:31
0
\$\begingroup\$

Ruby, 24 bytes

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

Try it online!

\$\endgroup\$
0
\$\begingroup\$

PHP, 47 Bytes

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

Try it online!

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.