回文您好,世界


29

您的任务是创建一个打印“ Greetings,Planet!”的程序。完全在控制台上。看起来很简单,对吧?好吧,这就是窍门。该代码必须是回文,这意味着它从右到左的读取方式与从左到右的读取方式相同。标准的漏洞是不是允许。


这似乎不是“ hello world”。
暴民埃里克(Erik the Outgolfer)'16年

4
@ΈρικΚωνσταντόπουλος它在所有意图和目的上都与Hello World相同。目的是打印由两个单词组成的短字符串,并且在Hello World程序中也使用常见的标点符号。除了打印Hello World的内置程序外,解决方案将完全相同(除了实际的字符串),而不管实际打印的字符串是什么。(我假设“ Greetings,Planet!”一词的选择是为了使内置插件毫无用处。)
Martin Ender

1
@MartinBüttner我认为,Hello, world!Hello World!是允许的唯一输出你好世界节目。
暴民埃里克

Answers:



16

TI-BASIC,67字节

"!tenalP ,sgniteerG"
"Greetings, Planet!"

之所以可行,是因为程序的最后一行显示在屏幕上,而第一个字符串实际上被视为注释。

每个小写字母存储为2个字节,其余字符为1个字节。如果我要弯腰规则并大写字母,那将是41个字节

"!TENALP ,SGNITEERG"
"GREETINGS, PLANET!"

您可以使用ei(数字)进一步减少字节数吗?
艾迪生·克伦普

1
我考虑了@VTCAKAVSMoACE,但它们用斜体表示,因此外观略有不同。
NinjaBearMonkey 2015年

为了清楚起见,第一行不是注释,它存储在answer变量中。
朱利安·拉希尼特

12

我知道这有点晚了(有点挑剔),但是...

> <>(鱼),47 45 47字节(如果我不使用随机方向,则为43 45)

x"!tenalP ,sgniteerG"!|o|!"Greetings, Planet!"x

这些答案彼此有点不同。有可能执行任一方向的代码

因此,通过“打印到控制台”,我假设您的意思是打印到标准输出。这会引发错误;将字符串打印到stdout之后,该错误将引发stderr。

为了证明这两种方法均有效,我使用了“随机方向”导演“ x”。Fish是一种二维语言,因此,无论导演指向哪种方式,代码都仍将(最终)被执行。

假设第一个导演指向右边,则将字符以相反的顺序加载到“堆栈”中,然后打印出相反的字符(或普通文本)。

假设两个导演都指向左侧,则再次将字符以相反的顺序加载到“堆栈”中(因为代码在这里反向加载,方向是向左),然后是相反的方向(或普通文字)。

如果随机导向器向上或向下指向,则无所谓-Fish知道循环到代码的下侧或上侧,指向随机化器。这样,它将继续与随机化器循环,直到它指向内部,指向要执行的代码。

!|o|!钻头从两侧进行以下操作:

  • !跳过下一条指令(将始终跳过|

  • |是反射器;它向内指向o

  • o 将堆栈中的当前项目作为字符输出到控制台,并将其从堆栈中删除。

因此,从本质上讲,这就是“将浴室中的两个镜子压在一起”的把戏,我一直输出到无法再用。

现在使用猫表情符号。>o<将输出无限地向内重定向,仍然会引发错误,但是我可以不用跳过反射就可以逃脱。

原来我第一次是对的-第二次尝试不是回文式的,但是反思性的

鱼(不打印到stderr),64字节(ew)

x"!tenalP ,sgniteerG"!;oooooooooooooooooo;!"Greetings, Planet!"x

这个家伙有点长。

它具有相同的随机箭头功能(以证明它可以双向运行),并且不会打印到stderr。

这里的区别是显而易见的。我从字面上打印出堆栈中的每个项目,然后以结束执行;

!;不立即结束执行,就!跳过下一个项目(端EXEC,在这种情况下),并继续直到它碰到的另一侧,其用作;!,其中它结束执行它跳过任何东西之前。

它遵循与较短答案相同的随机方向模式。


我认为您应该使用语言> <>来将其与Fish shell
Aaron

@Aaron通常假定Fish在这里表示> <>,​​但是我之前遇到过这个问题-我将其添加。谢谢!
Addison Crump 2015年

就像我猜想的那样,完整的代码是否会被执行是不可估量的。如果程序计数器卡在两个随机方向指令之间,或者只是卡在随机方向指令之一中,例如,它变小,到达底部,返回到该指令并重新开始,该怎么办?

@GLASSIC该程序将只输出一次,并且整个代码将永远不会执行。
Addison Crump

11

泡泡糖,38个字节

0000000: 73 2f 4a 4d 2d c9 cc 4b 2f d6 51 08 c8 49 cc 4b 2d 51 04  s/JM-..K/.Q..I.K-Q.
0000013: 04 51 2d 4b cc 49 c8 08 51 d6 2f 4b cc c9 2d 4d 4a 2f 73  .Q-K.I..Q./K..-MJ/s

10

通心粉0.0.2字节

print "Greetings, Planet!""!tenalP ,sgniteerG" tnirp

不使用注释的解决方案。(主要是因为通心粉没有评论...)


15
通心粉没有评论?ಠ_ಠ
亚历A.




5

GolfScript, 41 bytes

"Greetings, Planet!":"!tenalP ,sgniteerG"

Look ma, no no-ops! Try it online in Web GolfScript.

The second half of the source code stores the string in the second string.

"Greetings, Planet!":"!tenalP ,sgniteerG"
"!tenalP ,sgniteerG"

would print

Greetings, Planet!Greetings, Planet!

5

C++, 129 bytes

Trying to do this without comments led me into a deep, dark preprocessor nightmare with no way out that I could see. Instead I've just aimed to get it as short as possible.

/**/
#include<cstdio>
int main(){puts("Greetings Planet!");}//*/
/*//};)"!tenalP sgniteerG"(stup{)(niam tni
>oidtsc<edulcni#
/**/

5

Python 3, 57 bytes

print('Greetings, Planet!')#)'!tenalP ,sgniteerG'(tnirp

I fixed the issue with parenthesis.

Python 2, 53 bytes

print'Greetings, Planet!'#'!tenalP ,sgniteerG'tnirp

I used advantage of the lack of required parenthesis and spaces in Python 2, but there wasn't that big of a difference.


5

Jelly, non-competing

25 bytes This answer is non-competing, since the challenge predates the creation of Jelly.

»1ị“RsẈḄ,#ʠU“Uʠ#,ḄẈsR“ị1»

Look 'ma, no comments! Try it online!

How it works

»1ị“RsẈḄ,#ʠU“Uʠ#,ḄẈsR“ị1»  Main link. No arguments.

   “        “        “  »  Decompress all three strings; yield a list of strings.
    RsẈḄ,#ʠU               (decompresses to 'Greetings, Planet!')
             Uʠ#,ḄẈsR      (decompresses to ' WafdkmC Posited,')
                      ị1   (decompresses to 'Taarhus')

»1                         Take the maximum of the default argument (0) and 1.
  ị                        Select the string at the index to the left.

4

Stuck, 41 Bytes

"Greetings, Planet!"p"!tenalP ,sgniteerG"

Fairly similar to the CJam answer, except in Stuck if a print command is issued, automatic stack printing is suppressed.


3

Mathematica, 52 bytes

Print@"Greetings, Planet!""!tenalP ,sgniteerG"@tnirP

Also generates a Null "!tenalP ,sgniteerG"[tnirP] which doesn't get printed.


1
FYI, a lot of your answers (this one included) have been getting tossed in the Low Quality queue because they're solely a title/score and code. Though there's nothing wrong with that, you may consider adding more text (such as an explanation) so they don't get auto-flagged.
Mego

@Mego Okay, thought they were pretty self-explanatory.
LegionMammal978

1
I'm not saying the lack of an explanation is a bad thing (though answers with explanations tend to get more upvotes), I'm just letting you know that your (valid) answers were getting auto-flagged because of their length. :)
Mego

2
you can save 2 bytes with Echo in v10.3.
shrx

3

Fission, 45 43 bytes

Thanks to jimmy23013 for saving 2 bytes.

;"!tenalP ,sgniteerG"R"Greetings, Planet!";

Try it online!

R initialises an atom which moves to the right. " toggles string mode which simply prints the desired string to STDOUT before hitting ;, which destroys the atom and terminates the program. The first half is simply never executed.


@jimmy23013 I have no clue, to be honest. Thanks.
Martin Ender

3

05AB1E, 16 bytes

Code:

”!º¥,ÁÙ””ÙÁ,¥º!”

Explanation:

”!º¥,ÁÙ”          # Compressed string which results in "! Crm, Everywhere".
        ”ÙÁ,¥º!”  # Compressed string which results in "Greetings, Planet!".
                  # Top of stack is implicitly outputted.

Try it online!


2

STATA, 52 bytes

di "Greetings, Planet!"//"!tenalP ,sgniteerG" id

A slightly longer (53 byte) version that doesn't use comments is:

#d
di "Greetings, Planet!";"!tenalP ,sgniteerG" id
d#

#d [something] changes the delimiter (initially a carriage return) to ; unless [something] is cr, so the first command changes the delimiter to ;, the second prints the string, and the third (which continues until the end) is apparently a nop, though I have no idea why. I would have guessed that this would throw an error (unrecognized command "!tenalP ,sgniteerG" or something), but apparently not.


2

Japt, 29 bytes

This programming language was made after the question was posted, but was not made for this question.

`!t?ÓP ,?Ä>ÎG`;`GÎ>Ä?, PÓ?t!`

Try it online!

Each ? is an unprintable Unicode char: U+0082, U+000F, U+000F, and U+0082, respectively.

Fun fact: If Japt had been published a month sooner, it would have legitimately won this challenge.


2

APL, 41 bytes

'Greetings, Planet!'⍝'!tenalP ,sgniteerG'

In APL, the last value is printed and the lamp character (⍝) start a comment.


Hello, and welcome to PPCG! Great answer!
NoOneIsHere


2

TCL, 80 Bytes

proc unknown args {puts "Hello World!"}
}"!dlroW olleH" stup{ sgra nwonknu corp

explanation: TCL executes a global proc unknown when it encounters a call to an undefined command, the first line redefines that proc to a simple "hello world" program.

TCL's quoting rules are quite subtle, an open brace starts a quoted word that extends to the next matching close brace, allowing nested, quoted words. Braces are otherwise treated as normal characters. five words: }"!dlroW olleH", stup{, sgra, nwonknu and corp. No command named }"!dlroW olleH" has been defined, so the undefined proc from the first line is invoked instead.

A similar question was posted on the StackOverflow of antiquity; which has since been closed and deleted. I used my solution as a tongue in cheek TCL sample in this answer, and am now getting comments asking for an explanation, so I recreate my answer here.



1

Vitsy, 41 Bytes

This programming language was made after the question was posted, but was not made for this question.

"!tenalP ,sgniteerG"Z"Greetings, Planet!"

The Z character outputs everything in the stack to STDOUT.

Try it online!


1

Unefunge 98 - 49 bytes

"!tenalP ,sgniteerG<DC1>"k,@,k"<DC1>Greetings, Planet!"

The code above contains two unprintable characters with code 17 (device control 1) represented by <DC1>.


1

MSM, 73 bytes

GGreetings, Planet!.................,.................!tenalP ,sgniteerGG

The first half just before the middle , builds a reverse greeting string including the additional G on the very right. The middle , drops it and the rest is a canonical "Hello/Greeting/whatever" program. The additional characters on both ends are needed, because the message contains a ,. The left one is executed and drops the s. Both , and s need a replacement for the left concatenation dots, hence the Gs. The , on the right isn't executed, but build into the final string.


1

CoffeeScript, 53 bytes

Similar to Ruby and Lua and pretty much all the variants here.

alert 'Greetings, Planet!'#'!tenalP ,sgniteerG' trela


1

Bash, 52 48 bytes

echo Greetings, Planet!||!tenalP ,sgniteerG ohce

Also works in ksh, zsh, yash, dash. But not tcsh.

$ cat x.sh
echo Greetings, Planet!||!tenalP ,sgniteerG ohce
$ bash x.sh
Greetings, Planet!
$ ksh x.sh
Greetings, Planet!
$ zsh x.sh
Greetings, Planet!
$ yash x.sh
Greetings, Planet!
$ dash x.sh
Greetings, Planet!
$ tcsh x.sh
tenalP: Event not found.
$

1
Remove all quotes.
manatwork

1
Oh dear, a bash answer was shorter than python =/. Back to the drawing board!
Ashwin Gupta

1

Lua, 52 Bytes

print"Greetings, Planet!"--"!tenalP ,sgniteerG"tnirp

In Lua terminal, it is only 44 bytes with

="Greetings, Planet!"--"!tenalP ,sgniteerG"=

My bad. Thanks for the alert!
Digital Veer

Remove all parentheses. For functions with a single string parameter they are optional.
manatwork

Lua 5.3 do not require = in terminal, so it is 2 bytes off there. I think it existed in 2015, not sure tho.
val says Reinstate Monica

1

Milky Way 1.5.10, 43 41 39 bytes

"Greetings, Planet!"!tenalP ,sgniteerG"

Explanation

"Greetings, Planet!"                       # push the string to the stack
                    !                      # output the TOS
                     tenalP ,sgniteerG"    # throws an error and exits

Usage

python3 milkyway.py <path-to-code>

Did you really need a comment? If this is stack based (without implicit output), then just outputting the stack should work.
Addison Crump

You're right! Thanks. @FlagAsSpam
Zach Gates
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.