您的任务是创建一个打印“ Greetings,Planet!”的程序。完全在控制台上。看起来很简单,对吧?好吧,这就是窍门。该代码必须是回文,这意味着它从右到左的读取方式与从左到右的读取方式相同。标准的漏洞是不是允许。
您的任务是创建一个打印“ Greetings,Planet!”的程序。完全在控制台上。看起来很简单,对吧?好吧,这就是窍门。该代码必须是回文,这意味着它从右到左的读取方式与从左到右的读取方式相同。标准的漏洞是不是允许。
Answers:
"!tenalP ,sgniteerG"
"Greetings, Planet!"
之所以可行,是因为程序的最后一行显示在屏幕上,而第一个字符串实际上被视为注释。
每个小写字母存储为2个字节,其余字符为1个字节。如果我要弯腰规则并大写字母,那将是41个字节:
"!TENALP ,SGNITEERG"
"GREETINGS, PLANET!"
e
和i
(数字)进一步减少字节数吗?
我知道这有点晚了(有点挑剔),但是...
x"!tenalP ,sgniteerG"!|o|!"Greetings, Planet!"x
这些答案彼此有点不同。有可能执行任一方向的代码。
因此,通过“打印到控制台”,我假设您的意思是打印到标准输出。这会引发错误;将字符串打印到stdout之后,该错误将引发stderr。
为了证明这两种方法均有效,我使用了“随机方向”导演“ x”。Fish是一种二维语言,因此,无论导演指向哪种方式,代码都仍将(最终)被执行。
假设第一个导演指向右边,则将字符以相反的顺序加载到“堆栈”中,然后打印出相反的字符(或普通文本)。
假设两个导演都指向左侧,则再次将字符以相反的顺序加载到“堆栈”中(因为代码在这里反向加载,方向是向左),然后是相反的方向(或普通文字)。
如果随机导向器向上或向下指向,则无所谓-Fish知道循环到代码的下侧或上侧,指向随机化器。这样,它将继续与随机化器循环,直到它指向内部,指向要执行的代码。
该!|o|!
钻头从两侧进行以下操作:
!
跳过下一条指令(将始终跳过|
)
|
是反射器;它向内指向o
。
o
将堆栈中的当前项目作为字符输出到控制台,并将其从堆栈中删除。
因此,从本质上讲,这就是“将浴室中的两个镜子压在一起”的把戏,我一直输出到无法再用。
现在使用猫表情符号。>o<
将输出无限地向内重定向,仍然会引发错误,但是我可以不用跳过反射就可以逃脱。
原来我第一次是对的-第二次尝试不是回文式的,但是反思性的。
x"!tenalP ,sgniteerG"!;oooooooooooooooooo;!"Greetings, Planet!"x
这个家伙有点长。
它具有相同的随机箭头功能(以证明它可以双向运行),并且不会打印到stderr。
这里的区别是显而易见的。我从字面上打印出堆栈中的每个项目,然后以结束执行;
。
在!;
不立即结束执行,就!
跳过下一个项目(端EXEC,在这种情况下),并继续直到它碰到的另一侧,其用作;!
,其中它结束执行它跳过任何东西之前。
它遵循与较短答案相同的随机方向模式。
感谢Dennis提供了三个字节。
"!tenalP ,sgniteerG";"Greetings, Planet!"
!tenalP ,sgniteerG"Greetings, Planet!
saves a few bytes.
This can run in any browser.
alert("Greetings, Planet!")//)"!tenalP ,sgniteerG"(trela
print/tnirp
with alert/trela
, it works in other engines, too.
alert`Greetings, Planet!`//`!tenalP ,sgniteerG`trela
is 52 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!
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#
/**/
print('Greetings, Planet!')#)'!tenalP ,sgniteerG'(tnirp
I fixed the issue with parenthesis.
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.
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!
»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.
Print@"Greetings, Planet!""!tenalP ,sgniteerG"@tnirP
Also generates a Null "!tenalP ,sgniteerG"[tnirP]
which doesn't get printed.
Echo
in v10.3
.
Thanks to jimmy23013 for saving 2 bytes.
;"!tenalP ,sgniteerG"R"Greetings, Planet!";
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.
Code:
”!º¥,ÁÙ””ÙÁ,¥º!”
Explanation:
”!º¥,ÁÙ” # Compressed string which results in "! Crm, Everywhere".
”ÙÁ,¥º!” # Compressed string which results in "Greetings, Planet!".
# Top of stack is implicitly outputted.
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.
This programming language was made after the question was posted, but was not made for this question.
`!t?ÓP ,?Ä>ÎG`;`GÎ>Ä?, PÓ?t!`
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.
'Greetings, Planet!'⍝'!tenalP ,sgniteerG'
In APL, the last value is printed and the lamp character (⍝) start a comment.
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.
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.
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 G
s. The ,
on the right isn't executed, but build into the final string.
Similar to Ruby and Lua and pretty much all the variants here.
alert 'Greetings, Planet!'#'!tenalP ,sgniteerG' trela
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.
$
print"Greetings, Planet!"--"!tenalP ,sgniteerG"tnirp
In Lua terminal, it is only 44 bytes with
="Greetings, Planet!"--"!tenalP ,sgniteerG"=
=
in terminal, so it is 2 bytes off there. I think it existed in 2015, not sure tho.
"Greetings, Planet!"!tenalP ,sgniteerG"
"Greetings, Planet!" # push the string to the stack
! # output the TOS
tenalP ,sgniteerG" # throws an error and exits
python3 milkyway.py <path-to-code>