'abc' และ 'cba'


28

งานของคุณเป็นเรื่องง่ายเขียนโค้ดเดียวว่าเมื่อดำเนินการในหนึ่งผลภาษาเดียวสตริงและเมื่อดำเนินการในผลภาษาอื่นเพียงสตริง'abc' 'cba'โปรแกรมไม่ควรรับอินพุต

นี่คือความท้าทาย


4
ฉันไม่คิดว่านี่เป็นคนหลอกลวงเลย ความจริงที่ว่าสตริงถูกย้อนกลับนั้นแตกต่างจากการพิมพ์สองสตริงที่แตกต่างกัน ฉันจะไม่เปิด (ลงคะแนน) อีกครั้งเนื่องจากจะมีผลทันที
Luis Mendo

4
ฉันโหวตให้เปิดโพสต์นี้อีกครั้งเนื่องจาก IMO กำลังพิมพ์ด้านหลังของสตริงและสตริงแตกต่างจาก 2 แบบมาก ไม่มีคำตอบที่สามารถแก้ไขได้เล็กน้อยเพื่อให้เหมาะกับความท้าทายนี้ คำตอบของฉันใช้ techinque กลับสมมาตรเมื่อเปรียบเทียบกับคำตอบที่นั่น ฉันเห็นด้วยกับ @LuisMendo
Mr. Xcoder

2
คุณสามารถพิมพ์ABCแทนabc
Oliver Ni

5
ฉันลงคะแนนเพื่อเปิดใหม่คำตอบบางอย่างใช้ความจริงcbaก็คือabcถอยหลัง ลิงค์ , ลิงค์ , ลิงค์ , ลิงค์ , ลิงค์ , ลิงค์ , ลิงค์ , ลิงค์ , ลิงค์
Oliver Ni

2
'ABC'และ'CBA'ก็โอเคนำหน้า / ช่องว่างต่อท้าย / ขึ้นบรรทัดใหม่ตกลง แต่ต้องเหมือนกันสำหรับเอาต์พุตทั้งสอง
Chris_Rands

คำตอบ:



21

MATLAB / Octave, 41 bytes

disp(flip('abc',size(randsample(2,2),2)))

In MATLAB randsample(2,2) gives a 2×1 vector, so size(...,2) is 1. Thus flip is applied along the first dimension, which is a singleton, so the original string 'abc' is displayed:

enter image description here

In Octave randsample(2,2) gives a 1×2 vector, so size(...,2) is 2. Thus flip is applied along the second dimension, that is, the string is flipped from left to right:

enter image description here


I think this would be a few bytes shorter if you use the less interesting version variant.
Stewie Griffin

@StewieGriffin Thanks. I think it's too late to change now. Maybe post it yourself?
Luis Mendo

Nah, it's a boring version of this one... Also, I don't have MATLAB anymore so I won't be able to test it.
Stewie Griffin

15

25 bytes

print(1/2and'cba'or'abc')

Try it online! (Python 2)

Try it online! (Python 3)


-1 byte by using exit instead of print (although this applies to every answer as of now).
notjagan

@notjagan. exit prints to stderr, doesn't it? Not sure if it would be a valid answer.

@ThePirateBay Outputing to STDERR is allowed as per meta consensus.
notjagan

The challenge says code snippet and print though, it's not using default rules.
xnor


10

-1 byte if I make ==0 into >0 but that's already another answer

Python 2, 26 bytes

print('acbbca'[1/2==0::2])

Try it online!


Python 3, 26 bytes

print('acbbca'[1/2==0::2])

Try it online!

1/2 gives 0 (floordiv) in Python 2 and 0.5 (truediv) in Python 3. Thus, 1/2==0 gives 1 in Python 3 and 0 in Python 2 (actually, booleans, but those are just integers), so 'acbbca'[1::2] => 'cba' is given for Python 3 and 'acbbca'[0::2] => 'abc' is given for Python 2.


10

Excel / Google Sheets, 41 28 27 24 Bytes

Anonymous worksheet formula that takes no input and outputs "ABC" to the calling cell in Excel and "CBA" to the calling cell in Google Sheets

=IfError(M("CBA"),"ABC")

In Google Sheets, M(...) is an alias for and autoformatted to T(...) (short for Text()). This call returns the text value of the passed variable, "CBA". "CBA" is not caught as an error, so "CBA" is returned by IfError(...,"ABC")

In Excel, there is no M(...) function, and M(...) is not an alias and therefore M("CBA") returns the formula not found error, #NAME?. This is caught by IfError(...,"ABC"), which in turn returns "ABC".


Previous Versions, 27, 28, 41 Bytes

See edits for explanations

=If(IsErr(A()),"ABC","CBA")
=If(IsErr(GT()),"ABC","CBA")
=IfError(If(Info("NUMFILE"),"ABC"),"CBA")

1
Neat! ....You can save 2 bytes by using iserr instead of iferror and 1 byte by using "SYSTEM" instead of "NUMFILE": =IF(ISERR(INFO("SYSTEM")),"cba","abc")
Adam

8

CJam / 05AB1E, 6 bytes

"abc"R

Try it online:

How it works in CJam

"abc"    Push this string
R        Push variable R, predefined to the empty string
         Implicitly display stack

How it works in 05AB1E

"abc"    Push this string
R        Reverse
         Implicitly display top of the stack

8

With apologies to @HyperNeutrino for stealing most of his answer (I don't have the reputation to comment yet)

Python 2, 25 bytes

print('acbbca'[1/2>0::2])

Try it online!

Python 3, 25 bytes

print('acbbca'[1/2>0::2])

Try it online!


Umm it's actually <1 btw.
Erik the Outgolfer

@EriktheOutgolfer No that's truthy for both languages
HyperNeutrino

As @notjagan suggested, you can replace print with exit (which is allowed by our rules) and therefore save 1 byte.

3
You should add the second language to your answer (Python3 I assume)
Zacharý

@Zacharý Thanks, I didn't realize the problem had been updated (the original was only python 2 and pyhon 3)
reffu


7

JavaScript (NodeJS) and PHP, 46 bytes

<!--
strrev=console.log//--><?=
strrev("abc");

Prints abc in JS and cba in PHP.

Try the JS online!

Try the PHP online! (note that TIO doesn't hide the HTML comments (<!--...-->)


1
How is the <!-- interpreted in Node?
Esolanging Fruit

@Challenger5 It's apparently interpreted as a one-line comment, exactly like // (source). Works that way in browser JS as well.
Justin Mariner

6
That is weird...
Esolanging Fruit

--> blah blah is valid comment in some JavaScript interpreter, you may just remove //
tsh

1
@JustinMariner I had tested on Node v8.1.3. And the behavior defined in ES6 specification Annex B, which means all browsers that support ES6 should accept it as comment.
tsh


5

Python 2 and Python 3, 42 bytes

try:exec("print'abc'")
except:print('cba')

Try it online! (Python 2)

Try it online! (Python 3)

Thought I'd try something different...


I like this one, not the shortest but quite a generic framework, can be shortened a bit by using try:long;print('abc')
Chris_Rands

Or better still try:cmp;print('abc')
Chris_Rands


That's not what I wrote, parantheses () still required around print
Chris_Rands

5

Excel/Google Sheets, 28 bytes

Inspired by @TaylorScott, who used a function that only exists in Excel, I found an even shorter function that only exists in Google Sheets. Conveniently, it is designed to return strings:

=iferror(join(,"cba"),"abc")

How it works

In Google Sheets, join([arg1], arg2, arg3,...argk) will concatenate arg2 -> argk, optionally using the separator specified in arg1. In this case, it successfully returns "cba."

Excel has no join function, so iferror sees a problem and returns "abc"


1
My first ever submission - hope I am doing it right....
Adam

Nice solution :)
Taylor Scott


4

Java 8 & C, 95 bytes

//\
interface a{static void main(String[]s){System.out.print("abc"/*
main(){{puts("cba"/**/);}}

Try it in Java 8 - resulting in "abc".
Try it in C - resulting in "cba".

Explanation:

//\
interface a{static void main(String[]s){System.out.print("abc"/*
main(){{puts("cba"/**/);}}

As you can see in the Java-highlighted code above, the first line is a comment due to //, and the C-code is a comment due to /* ... */, resulting in:

interface a{static void main(String[]s){System.out.print("abc");}}

//\
interface a{static void main(String[]s){System.out.print("abc"/*
main(){{puts("cba"/**/);}}

Not sure how to correctly enable C-highlighting, because lang-c results in the same highlighting as Java.. But //\ will comment out the next line, which is the Java-code, resulting in:

main(){{puts("cba");}}

4

Python 2 / Python 3, 28 bytes

print('abc'[::int(1/2*4)-1])

In Python 2 int(1/2*4)-1 evaluates to -1 and so prints cba. - TiO

In Python 3 it evaluates 1 so prints abc. - TiO


2
Welcome to Programming Puzzles and Code Golf
Евгений Новиков

4

C and C++, 115, 78, 58, 56 bytes

#include<stdio.h>
main(){puts(sizeof('x')>1?"abc":"cba");}

78 bytes, thanks to challenger5.

58 bytes, thanks to aschepler.

56 bytes, thanks to hvd

Try it - C++!

Try it - C!


1
1) You can collapse the two #ifdefs to make a single one. 2) You can remove the space in #include <stdio.h>. 3) You can change printf("%s", to puts(. Try it online!
Esolanging Fruit

2
Or there's always the good old sizeof('x')>1?"abc":"cba" trick.
aschepler

@Challenger5 Thanks for the comment
Ivan Botero

@aschepler Thanks for the trick, i've made the changes 58 bytes :)
Ivan Botero

1
sizeof's operand does not need parentheses, it's not a function.
hvd

4

R/Cubix, 20 bytes

cat("abc")#u@o;o;o(;

R - Try it online!

Cubix - Try it online!

For R, cat("abc") then shameless abuse of comments. For Cubix

    c a
    t (
" a b c " ) # u
@ o ; o ; o ( ;
    . .
    . .
  • "abc" Pushs a, b ad c onto the stack
  • )# Increment the c, pushs number of element in stack
  • u U-turn to the right
  • ;( Remove the count, Decrement the c
  • o;o;o@ Output cba and exit

Pushs the number on in stack


2
I am strangely pleased by the way that cat( is totally ignored by Cubix.
Giuseppe





3

Ly / ><>, 20 19 bytes

"abc"&&ov
;     oo<

Try it with ><>!

Try it with Ly!

These languages are very similar, as Ly is based off ><>. However, Ly does not have 2D execution and interprets & differently, which I took advantage of here.

Both languages will start by pushing abc to the stack.

For ><>, the & instruction moves values to and fro the register. Two in a row will push a value to the register and then take it straight back, essentially a NOP.

For Ly, & is a modifier that makes an instruction perform its function on the entire stack.

o means the same thing for both languages, but since it is modified by & in Ly, it will print the whole stack, outputting abc. In ><>, it will only output c (as it is printed from the top down)

v is a NOP in Ly, which skips it and goes straight to ;, ending execution. ><> will instead treat it as a pointer, sending the IP downwards.

It then hits another arrow, sending the IP left. Here, it meets two o signs, outputting b and a.

EDIT: Saved a byte (and fixed ><> crashing)


1
You can save a byte by moving the ; to the second line. This also has the benefit that the ><> IP doesn't wrap around and go through the second line again, which causes an error.
Esolanging Fruit

shouldn't there be a ; for ><>? it wouldn't take any more bytes, just replace one of the spaces
Destructible Lemon

How about "abc"&&ooo;? It makes Ly crash, but only after printing "abc".
Not a tree

…or "abc"&&o!;o< for 1 extra byte, if you want to avoid crashing.
Not a tree



2

05AB1E and 2sable, 6 bytes

…CBAžR

Prints ABC (OP said it was allowed) in 05AB1E and CBA in 2sable, using the fact that 2sable was similar to 05AB1E but the žR was added to 05AB1E after 2sable was abandoned.

Try it online! (05AB1E)

Try it online! (2sable)


The specification states that it must be "abc" or "cba". By my word, I'd say that this is invalid, but I can ask OP.
HyperNeutrino

I asked the OP and he hasn't responded. If this turns out to be invalid, I will remove it.
Oliver Ni

@OliverNi Umm, if it's invalid you can just append a l btw.
Erik the Outgolfer

2

PHP + JavaScript, 29 28 bytes

This works because PHP interprets '0' (same as the integer number 0) as being falsy, while JavaScript assumes it is simply a non-empty string which is truthy.

'0'?alert('cba'):print(abc);

This is meant to run with -r on PHP. In Javascript, just paste it in the console.


Thanks to @Justin Mariner for saving me 1 byte!


You could also use "0" instead of +![]: it becomes 0 (falsy) in PHP and is a string (truthy) in JS.
Justin Mariner

@JustinMariner You're right, but that's 1 byte longer.
Ismael Miguel

1
Isnt it one byte shorter? +![]?print(abc):alert('cba'); -> "0"?alert('cba'):print(abc);
Justin Mariner

@JustinMariner OH!!! That way!!! Yes, it is 1 byte shorter. Thank you!
Ismael Miguel


2

Julia and Octave/Matlab, 27 bytes

if'a'=="a""abc"else"cba"end

In Octave, both 'a' and "a" represent the same string, therefore 'a'=="a" is true. However, in Julia, 'a' is a single character while "a" is a one-character string. In Julia, "cba" is the output.

Ungolfed version:

if 'a'=="a"
  "abc"
else
  "cba"
end

โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.