Python 3/><>, 177 173 172 167 Bytes
Thanks to @mathmandan for shaving 5 bytes off!
Well this was an experience, and a trying one, too. Any golf suggestions are welcome, since this is pretty long. I tried my best to reuse text, but it was quite difficult.
Technically, it would be Python 3 that this program should output (and I could change that if I didn't meet the specs -- but in the example Python/C output Python
was listed).
aa=" ni nettirw t'nsaw margorp sihT\"\""
v="><>!" #v "><>"r~/
a=", it was built for "+v#\a
print(aa[-3::-1]+"Pytho" +"n"+a)
# .4b;!?lor"!nohtyP r"~/
Try it on an online ><> interpreter and a Python 3 interpreter (the ><> interpreter requires you to input the code manually)
Returns
This program wasn't written in ><>, it was built for Python!
in ><> and
This program wasn't written in Python, it was built for ><>!
in Python.
Explanation (Python)
For the Python side of things, it's pretty simple. Here's the code that we care about (basically the code without comments, which are denoted by a #
in Python). Note that in Python \
is an escape character when used in strings, so \"
evaluates to "
in the string.
aa=" ni nettirw t'nsaw margorp sihT\"\""
v="><>!"
a=", it was built for "+v
print(aa[-3::-1]+"Pytho" +"n"+a)
What we care most about here is the operations performed on the variable aa
:
aa[-3::-1]: reverses the string and chops off the quotation marks (thanks to @mathmandan)
The print statement thus evaluates to
"This program wasn't written in " + "Pytho" + "n" + ", it was built for ><>!"
Explanation (><>)
Now we get to the more difficult part. Once again, here's the code with the unnecessary bits removed.
aa=" ni nettirw t'nsaw margorp sihT\"\
v "><>"r~/
a=", it was built for "+v \a
.4b;!?lor"!nohtyP r"~/
Line 1:
aa=" ni nettirw t'nsaw margorp sihT\"\
aa= pushes 1 onto the stack (evaluates 10==10, basically)
" ni ... \" pushes the first part plus a \ onto the stack.
\ deflects the pointer downwards
The stack right now (if printed): \This program wasn't written in
Line 2:
Note that line 2 begins at the /
because of the position of the pointer from line 1, and moves right to left.
v "><>"r~/
/ deflects the pointer leftwards
~r pops the / off the stack and then reverses it
"><>" pushes ><> onto the stack
v deflects the pointer downwards
The stack right now: ><> ni nettirw t'nsaw margorp sihT
Line 3:
Like the previous line, this one begins at the \
, which is where line 2 sends the pointer. Note that because the pointer wraps around the line when it reaches the first a
I'll be writing my explanation in order of where the pointer goes (and thus what is executed)
a=", it was built for "+v \a
\aa= deflect and push 1 onto the stack
", i ... " push the string onto the stack
+v sum the last two values pushed and deflect
The stack right now(x
is the character formed by the addition of "r" and a space. -- it is not the actual character, just a placeholder from me):
xof tliub saw ti ,><> ni nettirw t'nsaw margorp sihT
Line 4:
The pointer simply continues downwards so this line warrants no further explanation.
Line 5:
Starting at /
and going leftwards.
.4b;!?lor"!nohtyP r"~/
~"r Python!" pops x off and adds back r and a space
r reverses the stack
o pops and prints a character
l?!; pushes the length of the stack and stops if it's 0
b4. pushes 11 then 4 then moves to that location (where o is)
The stack right now (the output reversed):
!nohtyP rof tliub saw ti ,><> ni nettirw t'nsaw margorp sihT
And that should be it for the explanation. Let me know if there is any inconsistency between the explanation/code or if I did anything wrong; I golfed down my code some more while I was in the middle of writing the explanation so I might have mixed bits of old and new code up.
argv[0]
? \$\endgroup\$