Convert phrases to reverse style

-4

0

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

NoOneIsHere

Posted 2016-04-11T23:14:26.670

Reputation: 1 916

Question was closed 2017-05-24T16:19:58.940

4Seems a bit too simple. – orlp – 2016-04-11T23:18:22.743

@orlp I want to see how short it can be in long languages, like Java or languages with bad string parsing. – NoOneIsHere – 2016-04-11T23:29:07.807

I haven't studied French for over 30 years, but surely the le belongs before the noun, not after? – Neil – 2016-04-11T23:45:56.423

1@Neil This isn't supposed to be actual French. It is just reversing the adjective and noun, with a 'le' in between. – NoOneIsHere – 2016-04-11T23:46:54.603

10surely 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 – 2016-04-12T00:03:50.277

@LevelRiverSt It is 'French style' not 'French'. It is just reversing the adjective and noun, with a 'le' in between. – NoOneIsHere – 2016-04-12T00:12:12.057

9Next-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 – 2016-04-12T00:17:09.117

1Downvoting because of how trivial it is. – Downgoat – 2016-04-12T03:39:21.920

1@Downgoat Have you downvoted "Hello, World"? – NoOneIsHere – 2016-04-12T04:17:35.067

4

@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 – 2016-04-12T06:37:18.187

2

Everybody arguing about French, chat here.

– NoOneIsHere – 2016-04-12T15:02:35.390

The specification talks about adjectives, but there isn't a single adjective in any of the test cases. What on Earth is the challenge? – Peter Taylor – 2016-04-12T17:31:05.747

@PeterTaylor convert a two word phrase (e.g. code golf) into it'sself reversed, with a ' le ' inbetween (so code golf -> golf le code). – NoOneIsHere – 2016-04-12T18:12:54.067

8To 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 – 2016-04-12T20:41:52.993

1@DrGreenEggsandHamDJ I agree, I think the example is perfectly clear. The close votes started around the time of Peter's comment, and I think they are about the fact that this has very little to do with real french (though the title claims it does.) Technically Peter is right, the first word in the examples are not adjectives. But they are nouns functioning as adjectives. – Level River St – 2016-04-12T21:58:17.853

@cat wizzwizz4 added an edit to the queue and I accepted it. – NoOneIsHere – 2016-04-16T01:22:04.967

@NoOneIsHere Better! :D – cat – 2016-04-16T01:22:50.180

I VTR'd because this is sufficiently different than the newer question. – OldBunny2800 – 2017-05-24T20:50:43.647

Answers

8

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.

Zwei

Posted 2016-04-11T23:14:26.670

Reputation: 484

3Alternatively, " le "rro. – Martin Ender – 2016-04-12T08:18:17.177

Too bad CJam doesn't do implicit input as well. – Cyoce – 2016-04-13T06:40:41.963

11

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

Conor O'Brien

Posted 2016-04-11T23:14:26.670

Reputation: 36 228

9

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

James

Posted 2016-04-11T23:14:26.670

Reputation: 54 537

7

Pyth - 9 bytes

j" le "_c

Test Suite.

Maltysen

Posted 2016-04-11T23:14:26.670

Reputation: 25 023

Ninja'd me and I didn't know you could have implicit z? – orlp – 2016-04-11T23:21:07.200

1@orlp its implicit Q, i'm taking input in quotes. – Maltysen – 2016-04-11T23:22:16.560

6This kinda looks like french, "j'le _c" :P – Downgoat – 2016-04-11T23:55:05.133

4

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

Luis Mendo

Posted 2016-04-11T23:14:26.670

Reputation: 87 464

Wait... How does 7L -> [2 3 1] ? – Cyoce – 2016-04-13T06:44:05.587

@Cyoce It's the default content of level 7 of clipboard L. You can see them all here, table 2, pae 15

– Luis Mendo – 2016-04-13T09:33:23.130

4

Python 3, 41 bytes

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

user161778

Posted 2016-04-11T23:14:26.670

Reputation: 149

Good first golf! Welcome to the site! – James – 2016-04-12T02:27:48.757

@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 – 2016-04-12T02:43:04.800

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 – 2016-04-12T02:49:14.647

@Dennis, darn well I'll update, but it's gonna cost me. Thanks! – user161778 – 2016-04-12T02:51:28.017

3

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.

Alex A.

Posted 2016-04-11T23:14:26.670

Reputation: 23 761

3

GS2, 9 bytes

, ♦ le ♣2

Try it online!

Dennis

Posted 2016-04-11T23:14:26.670

Reputation: 196 637

1Looks like 9 is the magic number for this challenge. – Cyoce – 2016-04-13T15:01:12.663

2

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.

Denker

Posted 2016-04-11T23:14:26.670

Reputation: 6 639

2

JavaScript (ES6), 34 32 bytes

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

Saved 2 bytes thanks to @jrich!

user81655

Posted 2016-04-11T23:14:26.670

Reputation: 10 181

s=>(a=s.split` `)[1]+" le "+a[0] – jrich – 2016-04-13T01:07:10.117

Wow, just now, I independently came up with this. – NoOneIsHere – 2017-02-22T19:14:30.483

2

Jelly, 9 bytes

ṣ⁶Ṛj“ le 

Try it online!

Dennis

Posted 2016-04-11T23:14:26.670

Reputation: 196 637

2

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

Aaron

Posted 2016-04-11T23:14:26.670

Reputation: 3 689

2

Pyke, 10 bytes

dcX"le"RdJ

Try it here!

Or 9 bytes (noncompeting, add insert)

Dd@" le":

Try it here!

Blue

Posted 2016-04-11T23:14:26.670

Reputation: 26 661

Shouldn't it be "le" ? – Cyoce – 2016-04-13T15:02:11.067

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

NonlinearFruit

Posted 2016-04-11T23:14:26.670

Reputation: 5 334

1This is shorter though: s->s.replaceAll("(.+) (.+)","$2 le $1") (39 bytes) :) – Kevin Cruijssen – 2017-02-23T09:41:58.080

1

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 "

Shaggy

Posted 2016-04-11T23:14:26.670

Reputation: 24 623

Don't forget about string compression ;) – Oliver – 2017-05-25T04:23:16.907

Oh, yeah; really gotta do better at remembering that! Ta, @obarakon – Shaggy – 2017-05-25T07:38:53.047

1

Python, 37 bytes

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

orlp

Posted 2016-04-11T23:14:26.670

Reputation: 37 067

Won't this give code golf -> code le golf? – NoOneIsHere – 2016-04-11T23:17:27.330

@NoOneIsHere Misunderstood the problem, fixed now. – orlp – 2016-04-11T23:18:31.070

1

Python 2, 34 bytes

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

Expects input in quotes.

>> "code golf"
golf le code

xnor

Posted 2016-04-11T23:14:26.670

Reputation: 115 687

1

Jolf, 10 bytes

Try it here!

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

Conor O'Brien

Posted 2016-04-11T23:14:26.670

Reputation: 36 228

1

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>

Conor O'Brien

Posted 2016-04-11T23:14:26.670

Reputation: 36 228

2I'm not sure if that's allowed. You're basically making the shell split the string for you. – Dennis – 2016-04-12T03:33:16.287

@Dennis I see. Gimme a moment to revise. – Conor O'Brien – 2016-04-12T03:36:00.067

Can't you use echo instead of @ECHO? – NoOneIsHere – 2016-04-12T18:22:24.507

@NoOneIsHere Nope, this well also output the command. – Conor O'Brien – 2016-04-12T18:40:07.893

1

Go, 81 bytes

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

EMBLEM

Posted 2016-04-11T23:14:26.670

Reputation: 2 179

1

Perl, 18 bytes

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

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

Dennis

Posted 2016-04-11T23:14:26.670

Reputation: 196 637

1

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")..'')

Katenkyo

Posted 2016-04-11T23:14:26.670

Reputation: 2 857

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 .

Martin Ender

Posted 2016-04-11T23:14:26.670

Reputation: 184 808

Do you need the...! should imply match mode. (And plus who uses Match without ! except at the end anyway?) – CalculatorFeline – 2016-04-13T01:32:38.960

@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 – 2016-04-13T09:40:10.110

Do it! But be careful, as M without ! does occur, but not very often (see the number groups challenge) – CalculatorFeline – 2016-04-13T16:15:04.893

1

Mathematica, 30 bytes

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

Not very complicated...

LegionMammal978

Posted 2016-04-11T23:14:26.670

Reputation: 15 731

1

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

Mego

Posted 2016-04-11T23:14:26.670

Reputation: 32 998

1

Gawk, 17 16 bytes

1 byte off thanks to muru

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

Old

{print$2" le "$1}

NoOneIsHere

Posted 2016-04-11T23:14:26.670

Reputation: 1 916

0

Ruby, 24 bytes

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

Try it online!

G B

Posted 2016-04-11T23:14:26.670

Reputation: 11 099

0

PHP, 47 Bytes

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

Try it online!

Jörg Hülsermann

Posted 2016-04-11T23:14:26.670

Reputation: 13 026

0

C# - 43 bytes

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

jzm

Posted 2016-04-11T23:14:26.670

Reputation: 369

1This 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 – 2016-04-14T00:31:40.513