19
\$\begingroup\$

Your task is simple. The program reads in a line of text from the standard input, and it prints out the same text in a character-reversed form. It is not allowed to print anything else.

For example:

input: "Hello!", output: "!olleH"

The catch is, your program has to be able to do the exact same thing if the source code itself is character-reversed!

Scoring: standard code-golf scoring applies, with the following modification to limit the boring

//margorp
program//

style answers: any solution which is a palindrome will incur a +25% penalty to the score, rounded up. This penalty still applies, if you, for example, insert characters into the program which do not have any useful effects, just to break the palindrome.

\$\endgroup\$
1
  • 2
    \$\begingroup\$ "Useful effects" probably can't be objectively specified. E.g. in GolfScript, what about -1%#%1-/1 or -1%#%(0? \$\endgroup\$ Commented Jul 27, 2013 at 21:25

21 Answers 21

14
\$\begingroup\$

APL, 5

⌽⍞⍝⍞⊖

This reverses (, along the last axis) the input (), and is followed by a comment ( marks a line comment). Reversed, the program reverses (, along the first axis) the input, and is followed by a comment. Because the input is always going to be one dimensional, in this case and are functionally equivalent. It is perhaps even sneakier than the GolfScript solution, so here's a cleaner solution (no comments), which scores 9.

⍬,⌽,⍞,⊖,⍬

This first grabs an empty vector (), flattens it (monadic ,), and reverses it (, along the first axis). This still leaves an empty vector. Then it concatenates (dyadic ,) to the input (), leaving the input untouched. It then flattens (monadic ,) the already-flat input and reverses it (, along the last axis). Then it concatenates (dyadic ,) another empty vector () to the reversed input. This does nothing, and leaves the reversed input behind. Reversed, this program does the same thing, based again on the fact that and are functionally equivalent with one-dimesional arguments.

You really couldn't say I'm adding useless characters to break the palindrome (the two different reverse functions are different with two-or-more-dimensional input; I'm just taking advantage of the fact they act the same with one-dimensional input)

\$\endgroup\$
14
\$\begingroup\$

Unix shell - 8 + 25% = 10

rev||ver

Previous answer of cat|tac didn't actually work, tac reverses the order of lines, not the order of characters in a line.

\$\endgroup\$
11
\$\begingroup\$

Haskell, 53

main=niam
niam=interact reverse
esrever tcaretni=main

Technically not a palindrome, but, since function declaration order doesn't matter, once reversed you have exactly the same program.

\$\endgroup\$
3
  • \$\begingroup\$ Nice (+1). I didn't believe that this actually did anything, before I've tried it (Link). \$\endgroup\$ Commented Jul 30, 2013 at 14:33
  • \$\begingroup\$ move the main=niam declaration to the mid, and you get a palindrome. \$\endgroup\$ Commented Sep 20, 2013 at 12:05
  • \$\begingroup\$ @JohannesKuhn Yes, I admit it is still the "boring" palindrome solution, with a different appearance. \$\endgroup\$
    – lortabac
    Commented Sep 20, 2013 at 12:34
10
\$\begingroup\$

Ruby, 37

eval <<1.reverse#
esrever.steg stup
1

Reversed:

1
puts gets.reverse
#esrever.1<< lave
\$\endgroup\$
1
  • \$\begingroup\$ Herestrings, sneaky! +1 \$\endgroup\$
    – Doorknob
    Commented Aug 1, 2013 at 9:46
7
\$\begingroup\$

Tcl, 78

puts [string rev [gets stdin]];#\
]]nidts steg[ ver gnirts[ stup;# tixe emaner

Not a palindrome.

\$\endgroup\$
3
  • \$\begingroup\$ Idone original - reversed \$\endgroup\$ Commented Jul 27, 2013 at 20:27
  • \$\begingroup\$ What useful effect does "rename exit" have in this program? (I don't know Tcl) \$\endgroup\$
    – vsz
    Commented Jul 27, 2013 at 20:32
  • \$\begingroup\$ renames the command exit to # (the # is a parameter, not the start of a comment). \# in the next line will execute # (not start of the comment because escaped) which is the new exit. \$\endgroup\$ Commented Jul 27, 2013 at 20:35
6
\$\begingroup\$

gs2, 2

(Yes, this language was made before the challenge. No, it isn't a joke or a loophole. ASCII space (0x20) is reverse.)

EDIT: aw, man, this question is super old? If only I'd commited sooner. :< I'll leave this answer up just because it's too good to pass up, though.

\$\endgroup\$
3
\$\begingroup\$

Golfscript, 9 (7 * 1.25 = 8.75)

-1%#%1-

dirty, dirty, dirty, but extremely short. -1% means "select each element (character), backwards" and # means "line comment". The rest is just a bit of GolfScript's I/O magic.

This is the shortest "clean" (no comments) solution I've found (14 * 1.25 = 17.5):

'';-1%..%1-;''

meaning: push an empty string, drop it, reverse the input, clone it twice, split it by itself (consuming two copies and producing an empty array), remove all ones from the empty array, drop the emtpy array, push an empty string and (implicitly) print the stack.

\$\endgroup\$
5
  • \$\begingroup\$ I should have expected this. Even an 500% penalty would have a high chance to make it a winner. \$\endgroup\$
    – vsz
    Commented Jul 27, 2013 at 20:43
  • \$\begingroup\$ @vsz I am looking for a no-comment (i.e. clean) solution in golfscript, however. \$\endgroup\$ Commented Jul 27, 2013 at 20:46
  • \$\begingroup\$ If the input does not contain 1's.. \$\endgroup\$ Commented Jul 27, 2013 at 20:55
  • \$\begingroup\$ @JohannesKuhn you are right. I have dropped that "solution". \$\endgroup\$ Commented Jul 27, 2013 at 20:58
  • \$\begingroup\$ @vsz a more interesting challenge is to forbid comment characters (and still penalise palindromes?) My contestant for this has 14 characters, quite a lot I guess. \$\endgroup\$ Commented Jul 27, 2013 at 21:00
3
\$\begingroup\$

Ruby, 44

puts gets.reverse#esrever.steg stup

Just a comment at the end of a normal program :P

35 characters, +25% = 44

\$\endgroup\$
2
\$\begingroup\$

Tcl, 75.25

This time a palindrome.

puts [string rev [gets stdin]]]]nidts steg[ ver gnirts[ stup
\$\endgroup\$
1
  • \$\begingroup\$ (between ]] and ]] is the \x1A character.) \$\endgroup\$ Commented Jul 27, 2013 at 20:37
2
\$\begingroup\$

Python 3, 42

print(input()[::-1]);#)]1-::[)(tupni(tnirp

Credits

  • Initial version with score of 49 + 25% = 61.25 created by me
  • Thanks to arshajii for improving the score from 61.25 to 51.25
  • New version with score of 42 (:-)) created by me
\$\endgroup\$
3
  • \$\begingroup\$ You can use print(input()[::-1])#)]1-::[)(tupni(tnirp in Python 3 instead to cut the score down to 51.25. \$\endgroup\$
    – arshajii
    Commented Jul 30, 2013 at 13:29
  • 1
    \$\begingroup\$ Thats not cutting... \$\endgroup\$
    – Oliver Ni
    Commented Jul 30, 2015 at 14:46
  • \$\begingroup\$ "any solution which is a palindrome will incur a +25% penalty to the score, rounded up. This penalty still applies, if you, for example, insert characters into the program which do not have any useful effects, just to break the palindrome." I'd argue that the ; is such an insertion that does not have any useful effect. \$\endgroup\$
    – Oliver Ni
    Commented Jul 31, 2015 at 1:23
2
\$\begingroup\$

Rebmu: 9 (w/penalty) or 13 (without)

The boring Rebmu solution is 9 and bears the palindromic penalty. I'll show it anyway "just because":

rnRVaVRnr

Using the unmushing trick of noticing capital runs of letters are separate words, and the lack of a leading capital run means we're not making a set-word, we produce five ordinary words:

rn rv a vr nr

Which is a shorthand for the equivalent code (also legal Rebmu):

return reverse a vr nr

The fact that vr and nr are meaningless doesn't matter, because despite not being assigned to anything they are valid words. So the evaluator only runs the return reverse a...it works both ways. But this is analogous in a sense to the boring cheat: the code isn't commented out, but it's dead and not executed on one path.

For something more exciting that doesn't incur the penalty, how about this 13 character solution:

a VR :rv AvrA

Let's look at how this is processed on its forward and reverse paths, when expanded. Forward:

a               ; evaluate a, as it is a string it has no side effects
vr: :reverse    ; "set" vr to mean what a "get" of reverse means now
a: vr a         ; assign a to calling "vr" on a, effectively reversing
                ;   ^-- result of assign is last expression, the answer!

Backwards as ArvA vr: RV a:

a: reverse a    ; assign A to its reversal
vr: rv: a       ; make the abbreviation vr equal to assignment of a to rv
                ;   ^-- result of assign is last expression, the answer!

On the downside, the backwards variant is overwriting the abbreviation for reverse. But hey, it's not a palindrome, and it's a mere 13 characters. :-)

(Note: This assumes you're running Rebmu in the /args mode, where a is the default argument to the program passed to the interpreter on the command line and you accept the result. If reading from standard input is actually a requirement, things grow e.g. from 9 to 11 characters for the simple solution: rnRVrArVRnr. And if you have to print to standard output from within the program instead of accepting the expression output of the interpreter that would add a couple characters too.)

\$\endgroup\$
2
\$\begingroup\$

JavaScript, 62 * 1.25 = 78

i.split('').reverse().join('')//)''(nioj.)(esrever.)''(tilps.i

Not too creative, but the best I could come up with. (assumes that the input is stored in variable i)

I did get this:

63 chars, no palindrome

i.split('').reverse().join('')//)a(nioj.)(esrever.)''=a(tilps.i

but it felt too much like cheating. :P I could do many more trivial changes (such as using i['split'] instead of i.split), but all those still feel like cheating :P

\$\endgroup\$
1
  • \$\begingroup\$ assumes that the input is stored in variable i then (p=a=>a&&p()==`(p=${p})(i)`?i:i.split``.reverse().join``)(i) (60 bytes) does that thing. \$\endgroup\$ Commented Oct 3, 2019 at 8:59
2
\$\begingroup\$

Pyke, 2 (+25%), non-competing

_

Pyke takes input implicitly and outputs implicitly.

_ is the reverse function.

\$\endgroup\$
1
\$\begingroup\$

Tcl, 99

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

If a command that does not exist is called, a special unknown command is called that could load the command. Or do other funny stuff.

\$\endgroup\$
1
\$\begingroup\$

T-SQL, 86*1.25=108

Here's a boring palindromic entry for SQL Server 2008 (and newer) just to show that it's possible.

DECLARE @ VARCHAR(MAX)='Hi'PRINT REVERSE(@)--)@(ESREVER TNIRP''=)XAM(RAHCRAV @ ERALCED

@ holds the input text, the example string being "Hi". This entry's char count is for a two-char input string.

\$\endgroup\$
1
\$\begingroup\$

Keg, ceil(1+0.25)=2 bytes

?

This takes input and reverses, and implicit output outputs it literally. TIO

\$\endgroup\$
1
\$\begingroup\$

Befunge-98 (PyFunge), 17 bytes

#[~   #<
 [:!k@,v

Try it online!

Try it online! (Reversed)

\$\endgroup\$
0
\$\begingroup\$

QBIC, 14 + 25% = 18 bytes

;?_fA|#|Af_?;

Employs a palindromic source code. Using two distinct ways to do this takes somewhat longer (34 bytes):

;[_lA|,1,-1|Z=Z+mid$(A,a,1)#|Af_?;

Explanation:

Read the input string from the command line as A$
;

FOR len(A$); a >= 1; a--
[        Starts a FOR loop
_l...|   returns the length the argument(here, A$); sets the starting point of the loop
,1       set the end point of the loop
,-1      FOR loop incrementer (or decrementer in this case)
|        Ends the argument list for the FOR loop

On each FOR iteration, add the right-most character of the input to Z$
Z=Z+mid$(A,a,1)

The cheat:
# starts a string literal, hiding the flipped source.
    The literal is closed implicitly at the end-of-file.
    The FOR loop is closed implicitly at the end-of-file.
    Z$ gets printed implicitly at the end of the program if it's non-empty

The flipped source is the same as my palindrome-code:
;       reads in A$ from the cmd line   
 ?      PRINT
  _fA|  The reversed version of A$
      # And hide the rest of our source in a literal  
\$\endgroup\$
0
\$\begingroup\$

Pushy, 7 bytes (5 + 25%)

@"i"@

Try it online!

This incurs the 25% penalty as it's a palindrome, but it's the best I could do. No matter which way around the program is run, it does these 3 commands:

@     \ Reverse the input
 "    \ Print
  i   \ Exit

Substituting i for c (clear stack) or \ (comment) has the same effect.

\$\endgroup\$
0
\$\begingroup\$

05AB1E, 1 + 25% = 2 bytes

R

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ You need to round up, so this is (1+0.25) = 1.25 rounded up = 2 bytes. \$\endgroup\$
    – user85052
    Commented Oct 3, 2019 at 6:19
  • \$\begingroup\$ Thanks! I fixed the title. \$\endgroup\$
    – Sam
    Commented Oct 3, 2019 at 6:33
0
\$\begingroup\$

R, 65 bytes

EDIT: Ditched the 25% penalty thanks to a trivial, yet unseen to me, change by Grimy.

stringi::stri_reverse(scan(,""))#))'',(nacs(esrever_irts::ignirts

Try it online!

Say hello to R's abysmal ability to handle strings, even when you use packages designed for strings like stringi ... barf

\$\endgroup\$
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.