22
\$\begingroup\$

Just because the reverse and palindrome were not too challenging individually :)

Write a program (full program or function) that satisfies the following: given a string input,

  • the program as-is outputs the input string unchanged.
  • the program reversed outputs the input string reversed.
  • the program palindromized outputs the input string palindromized.
  • the program reversed and then palindromized outputs the input string reversed and then palindromized.

In this challenge, palindromize is defined as "concatenate the reverse on the right" (lambda s: s + s[::-1] in Python).

For example, if your code is abc, when given "xyz" as input,

  • abc should output "xyz".
  • cba should output "zyx".
  • abccba should output "xyzzyx".
  • cbaabc should output "zyxxyz".

You may assume that the input string is nonempty and entirely consists of printable ASCII (32-126, no newlines).

Standard rules apply. The shortest code in bytes wins.

\$\endgroup\$
1

10 Answers 10

16
\$\begingroup\$

05AB1E, 3 bytes

?R\

Explanation:

?      # Output the (implicit) input-string (without trailing newline)
 R     # Reverse the (implicit) input-string
  \    # Discard it from the stack
       # (don't output anything implicitly, since we already had an explicit print)
\      # No-op to discard the top of the stack
 R     # Reverse the (implicit) input-string
  ?    # Pop and output this reversed input
       # (don't output anything implicitly, since we already had an explicit print)
?R\    # Same as the first program above, outputting the input as is
   \R? # Same as the second program above, outputting the input reversed
\R?    # Outputs the input reversed
   ?R\ # Outputs the input as is
\$\endgroup\$
13
\$\begingroup\$

Charcoal, 3 bytes

θ⮌ω

Try it online! Reversed:

ω⮌θ

Try it online! Palindromised:

θ⮌ωω⮌θ

Try it online! Palindomised Reversed:

ω⮌θθ⮌ω

Try it online! Explanation:

θ

Print the input.

⮌ω

Print the reverse of the empty string.

Reversed explanation:

ω

Print the empty string.

⮌θ

Print the reverse of the input.

Palindromised and reversed palindromised follow trivially.

\$\endgroup\$
13
\$\begingroup\$

JavaScript, 70 bytes


(x=>x.at?x:y=>y+z(y))//)y+)y(z>=y:``nioj.)(esrever.]x...[?ta.x>=x=z(

Try it online!

\$\endgroup\$
10
\$\begingroup\$

Python, 51 bytes

-2 bytes, thanks to tsh
-2 bytes, thanks to Jakque

s=input()#)]1-::[s=dne(tnirp
print(end=s)#)(tupni=s

Attempt This Online!


Python, 55 bytes

def f(s):#
 print(end=s)#)]1-::[s=dne(tnirp 
#:)s(f fed

function accepting string as argument

Attempt This Online!

\$\endgroup\$
2
  • \$\begingroup\$ Maybe you can save bytes by changing def f(s): into s=input(). link \$\endgroup\$
    – tsh
    Sep 27, 2023 at 11:26
  • 2
    \$\begingroup\$ 51 by reusing the comment Attempt This Online! \$\endgroup\$
    – Jakque
    Sep 28, 2023 at 11:30
7
\$\begingroup\$

Vyxal, 3 bytes

₴Ḃ_

Try!

!yrT

Try!!yrT

!yrTTry!

+1 very hard

You uh sure about that?

Explained

₴Ḃ_­⁡​‎‎⁡⁠⁡‏‏​⁡⁠⁡‌⁢​‎‏​⁢⁠⁡‌­
₴    # ‎⁡Print without newline
# ‎⁢The rest has no impact
💎
_Ḃ₴­⁡​‎⁠‎⁡⁠⁢‏⁠‎⁡⁠⁣‏‏​⁡⁠⁡‌­
 Ḃ₴  # ‎⁡Print the reverse of the input
💎
₴Ḃ__Ḃ₴­⁡​‎‎⁡⁠⁡‏⁠⁠‏​⁡⁠⁡‌⁢​‎‎⁡⁠⁢⁡‏⁠‎⁡⁠⁢⁢‏‏​⁡⁠⁡‌­
₴       # ‎⁡Print the normal input
    Ḃ₴  # ‎⁢Print the reverse
💎
_Ḃ₴₴Ḃ_­⁡​‎‎⁡⁠⁢‏⁠‎⁡⁠⁣‏‏​⁡⁠⁡‌⁢​‎‎⁡⁠⁤‏‏​⁡⁠⁡‌­
 Ḃ₴     # ‎⁡Print reversed input
   ₴    # ‎⁢And then normal input
💎

Created with the help of Luminespire.

\$\endgroup\$
4
  • \$\begingroup\$ "You uh sure about that?" Answer: Yes. \$\endgroup\$ Sep 27, 2023 at 17:43
  • 2
    \$\begingroup\$ @TheEmptyStringPhotographer if it can be solved in 3 bytes, then clearly it isn't that hard :p \$\endgroup\$
    – lyxal
    Sep 27, 2023 at 23:49
  • \$\begingroup\$ The number of bytes it takes has no correlation with how hard it is (correlation factor = 0). \$\endgroup\$ Sep 28, 2023 at 6:36
  • \$\begingroup\$ Someone should run a statistical study on this! \$\endgroup\$
    – Someone
    Sep 29, 2023 at 21:38
6
\$\begingroup\$

R, 92 88 86 bytes

Edit: -2 bytes thanks to Jakque's comment

x=utf8ToInt(scan(,''))#)))x(ver(8ftUoTtni(tac
cat(intToUtf8(x))#))'',(nacs(tnIoT8ftu=x

Attempt This Online!

Full program that reads input from and outputs to the R console.

Note that the program(s) should be saved to a file & run in R using source("myprog.r"): copy-pasting into a REPL environment will result in incorrect parsing of the input (the program will try to read it's own lines!).
The header/footer in the "Attempt This Online" link mimic this behaviour, instead of the default of running code in a REPL setting.


R, 118 115 bytes

if(!exists('f'))f=\(x)c();body(f)[T<-T+1]=parse(t='x')#)')x(ver'=t(esrap=]1+T-<T[)f(ydob;)(c)x(\=f))'f'(stsixe!(fi

Attempt This Online!

Code that defines a function, f, that accepts an input of a vector of characters and returns it unchanged.
Reversed code defines f to return its input reversed, palindromized code defines f to return it palindromized, and reverse-palindromized code defines f to return it reverse-palindromized.
It's not completely straightforward to amend an already-complete R function definition, but it can be achieved (as here) by assigning directly into the function body if the function already exists.

\$\endgroup\$
4
\$\begingroup\$

Bash, 17 bytes


rev $1 # 1$ tac

Try it online!

\$\endgroup\$
7
  • 10
    \$\begingroup\$ Surely this is a snippet, not a program, since it requires that the input is pre-loaded into variable x? \$\endgroup\$ Sep 27, 2023 at 11:23
  • \$\begingroup\$ @DominicvanEssen More precisely, file x. \$\endgroup\$
    – Neil
    Sep 27, 2023 at 14:17
  • \$\begingroup\$ @DominicvanEssen codegolf.meta.stackexchange.com/a/2452 but comments say filename should be input \$\endgroup\$
    – l4m2
    Sep 27, 2023 at 15:52
  • 1
    \$\begingroup\$ [@DominicvanEssen ](tio.run/##S0oszvifmpyRr6Cbp6CemJScoq5gp1DBBRZS/…) \$\endgroup\$
    – l4m2
    Sep 28, 2023 at 0:46
  • 2
    \$\begingroup\$ Shouldn't the canonical (non-reversed) program be cat $1 # 1$ ver? \$\endgroup\$
    – Vi.
    Sep 28, 2023 at 13:40
4
\$\begingroup\$

C# Interactive Compiler, 80 77 bytes

Based on @bsoelch's Python answer.
-3 bytes implicitly thanks to the suggested golf of @Jakque on the ported Python answer.

var s=ReadLine();//;))(yarrAoT.)(esreveR.s(etirW
Write(s);//;)(eniLdaeR=s rav

Try it online.

var s=ReadLine();//;)s(etirW
Write(s.Reverse().ToArray());//;)(eniLdaeR=s rav

.enilno ti yrT

var s=ReadLine();//;))(yarrAoT.)(esreveR.s(etirW
Write(s);//;)(eniLdaeR=s ravvar s=ReadLine();//;)s(etirW
Write(s.Reverse().ToArray());//;)(eniLdaeR=s rav

Try it online..enilno ti yrT

var s=ReadLine();//;)s(etirW
Write(s.Reverse().ToArray());//;)(eniLdaeR=s ravvar s=ReadLine();//;))(yarrAoT.)(esreveR.s(etirW
Write(s);//;)(eniLdaeR=s rav

.enilno ti yrTTry it online.

Explanation:

// is a single-line comment and everything after it are no-ops. So if we'll ignore those comments, all four of the REPL-programs will:

  1. Start with var s=ReadLine();: Read the input as string into variable s
    1. Write(s);: Write string s to the output-buffer
    2. Write(s.Reverse().ToArray());: Reverse string s to a reversed character-iteratator (Enumerable.ReverseIterator<char>); then convert it to a character-array (char[]); then write all characters in this character-array separately to the output-buffer
    3. Write(s);Write(s.Reverse().ToArray());: 1 and 2 combined
    4. Write(s.Reverse().ToArray());Write(s);: 2 and 1 combined
  2. After which the output-buffer is output implicitly as single string without trailing newline
\$\endgroup\$
3
\$\begingroup\$

Pip, 11 10 bytes


.:a  aR.

This answer exploits the fact that .a does nothing significant, whereas a.b is concatenation.

Attempt This Online!

[\n].:a  aR.[\n]­⁡​‎‎⁡⁠⁡‏⁠‎⁡⁠⁢‏⁠‎⁡⁠⁣‏⁠‎⁡⁠⁤‏⁠‎⁡⁠⁣⁤‏⁠‎⁡⁠⁤⁡‏⁠‎⁡⁠⁤⁢‏⁠‎⁡⁠⁤⁣‏‏​⁡⁠⁡‌⁢​‎‎⁡⁠⁢⁡‏⁠‎⁡⁠⁢⁢‏‏​⁡⁠⁡‌⁣​‎‎⁡⁠⁢⁣‏⁠‎⁡⁠⁢⁤‏‏​⁡⁠⁡‌⁤​‎‎⁡⁠⁣⁡‏⁠‎⁡⁠⁣⁢‏⁠‎⁡⁠⁣⁣‏‏​⁡⁠⁡‌­
[\n]        [\n]  # ‎⁡Newlines for Palindormize/Reverse Palindromize
    .:a           # -, Push input (`:` lowers precedence for Reverse Palindromize)
                  # ‎⁣Comment ("  "), ignore the following characters
         aR.      #
💎

Reversed:

[\n].Ra  a:.[\n]­⁡​‎‎­⁡​‎‎⁡⁠⁢⁡‏⁠‎⁡⁠⁢⁢‏⁠‎⁡⁠⁢⁣‏⁠‎⁡⁠⁢⁤‏⁠‎⁡⁠⁣⁡‏⁠‏​⁡⁠⁡‌⁢​‎‎⁡⁠⁣⁢‏⁠‎⁡⁠⁣⁣‏⁠‎⁡⁠⁣⁤‏⁠‎⁡⁠⁤⁡‏‏​⁡⁠⁡‌­
    .Ra           # ‎⁡-, Push input reversed
         a:.      # ‎⁢Comment
💎

Palindromized:

[\n].:a  aR.[\n][\n].Ra  a:.[\n]­⁡​‎‎⁡⁠⁢⁡‏⁠‎⁡⁠⁢⁢‏⁠⁠‏​⁡⁠⁡‌⁢​‎‎⁡⁠⁢⁢⁢‏⁠‎⁡⁠⁢⁢⁣‏⁠‎⁡⁠⁢⁢⁤‏⁠‎⁡⁠⁢⁣⁡‏⁠‎⁡⁠⁢⁣⁢‏‏​⁡⁠⁡‌⁣​‎‎⁡⁠⁢⁣‏⁠‎⁡⁠⁢⁤‏⁠‎⁡⁠⁣⁡‏⁠‎⁡⁠⁣⁢‏⁠‎⁡⁠⁣⁣‏⁠‎⁡⁠⁢⁣⁣‏⁠‎⁡⁠⁢⁣⁤‏⁠‎⁡⁠⁢⁤⁡‏⁠‎⁡⁠⁢⁤⁢‏‏​⁡⁠⁡‌­
    .:a                           # ‎⁡-, Push input
                    .Ra           # ‎⁢Concatenate with input reversed
         aR.             a:.      # ‎⁣Comment
💎

Reverse Palindromized

[\n\.Ra  a:.[\n][\n].:a  aR.[\n]­⁡​‎‎⁡⁠⁢⁡‏⁠‎⁡⁠⁢⁢‏⁠‎⁡⁠⁢⁣‏⁠‎⁡⁠⁢⁤‏⁠‎⁡⁠⁣⁡‏‏​⁡⁠⁡‌⁢​‎‎⁡⁠⁢⁢⁢‏⁠‎⁡⁠⁢⁢⁣‏‏​⁡⁠⁡‌⁣​‎‎⁡⁠⁣⁢‏⁠‎⁡⁠⁣⁣‏⁠‎⁡⁠⁣⁤‏⁠‎⁡⁠⁤⁡‏⁠‎⁡⁠⁢⁢⁤‏⁠‎⁡⁠⁢⁣⁡‏⁠‎⁡⁠⁢⁣⁢‏⁠‎⁡⁠⁢⁣⁣‏⁠‎⁡⁠⁢⁣⁤‏⁠‎⁡⁠⁢⁤⁡‏⁠‎⁡⁠⁢⁤⁢‏‏​⁡⁠⁡‌­
    .Ra                           # ‎⁡-, Push input reversed
                    .:a           # ‎⁢Concatenate with input, ":" groups it as .(Ra).a rather than .R(a.a)
         a:.             aR.      # ‎⁣Comment
💎
\$\endgroup\$
1
\$\begingroup\$

PHP -F, 31 bytes

echo$argn;#;)ngra$(verrts ohce

Try it online!


echo strrev($argn);#;ngra$ohce

Reversed version

echo$argn;#;)ngra$(verrts ohce


echo strrev($argn);#;ngra$ohce

Palindromed version


echo strrev($argn);#;ngra$ohce
echo$argn;#;)ngra$(verrts ohce

Reversed and Palindromed version

Actually, my answer to the palindrome question will work here too, because it uses 2 separate echo for the palindrome parts ;) Notice the new line at the end which is necessary because of the comment trick. Actually saved one byte here using # as comment

\$\endgroup\$

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.