-14
\$\begingroup\$

For today's challenge you have to take an input, and if your source code is reversed, then the input should be printed doubled.

Let's say your source code is ABC and its input is xyz. If I write CBA instead and run it, the output must be any of be xyzxyz or xyz xyz or

xyz 
xyz

For any numerical input you should not multiply it with 2, instead use the upper rule, for input 1 -> 11 or 1 1 or

1
1

(Trailing newlines allowed in output, but not the leading ones)

For simplicity, you can assume that the input is always a single line string containing only ASCII letters (a-z), digits (0-9) and spaces. (You will not get empty input)

Standard loopholes apply, shortest code wins

original source code's output can be anything

\$\endgroup\$
10
  • 1
    \$\begingroup\$ What is the output for original source code? \$\endgroup\$ Commented Mar 8, 2021 at 6:55
  • 3
    \$\begingroup\$ @Daemon So if original source is a palindrome that directly prints the input doubled, then that is allowed too? \$\endgroup\$ Commented Mar 8, 2021 at 7:09
  • 8
    \$\begingroup\$ The challenge wouldn't be very interesting if the original source has no restriction (ex. a solution with a trailing comment would suffice) \$\endgroup\$ Commented Mar 8, 2021 at 7:25
  • 4
    \$\begingroup\$ @Daemon As you can see, your challenge spec isn't the most popular. Consider using the Sandbox next time, to avoid such situations. \$\endgroup\$ Commented Mar 8, 2021 at 8:32
  • 11
    \$\begingroup\$ @Daemon You have posted 6 challenges of which 4 have negative total score. You have never answered any challenges yourself. I highly recommend answering some existing challenges before venturing into writing your own, as that will give you some insight into what makes for a good, well-specified challenge. \$\endgroup\$ Commented Mar 8, 2021 at 8:36

15 Answers 15

6
\$\begingroup\$

Trivial comment trick

JavaScript (ES6), 12 bytes

s=>s//s+s>=s

Try it online!

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

Japt, 1 byte

p

Try it

  • Returns S repeated n times with n defaulted to 2.
\$\endgroup\$
6
  • \$\begingroup\$ How can 1 byte of code have different behaviour when reversed? \$\endgroup\$ Commented Mar 8, 2021 at 8:24
  • 2
    \$\begingroup\$ @Adám Abuses the rule "original source code can be anything " by doubling the input too, loopholes allowed \$\endgroup\$ Commented Mar 8, 2021 at 8:28
  • 4
    \$\begingroup\$ Oh, I see. OP gets my downvote too, then. \$\endgroup\$ Commented Mar 8, 2021 at 8:29
  • \$\begingroup\$ @Adám lol, I feel so sad for OP.. People should take more care to advices! \$\endgroup\$ Commented Mar 8, 2021 at 8:31
  • \$\begingroup\$ works also for sed \$\endgroup\$ Commented Mar 8, 2021 at 9:34
3
\$\begingroup\$

Perl 5 (-0777p -M5.01), 3 bytes

yas

reversed

say
  • -0777 to undef input record separator (slurp input).
  • -p option print default argument (input) to output.
  • -M5.01 to use say shorter than print
  • yas : bareword (same as "yas" with the quotes), no-op.

Try it online!

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

PowerShell, 8 bytes

#2*sgra$

Try it online!

!enilno ti yrT

-6 bytes thanks to @NauhelFouilleul

\$\endgroup\$
2
  • 1
    \$\begingroup\$ or maybe just #2*sgra$ because the output can be empty for original source \$\endgroup\$ Commented Mar 8, 2021 at 7:21
  • \$\begingroup\$ @NahuelFouilleul thanks! \$\endgroup\$ Commented Mar 8, 2021 at 7:23
2
\$\begingroup\$

APL (Dyalog Unicode), 3 bytes

Anonymous tacit prefix function.

,,,

Try it online!

The outer commas ravel (flatten) the argument to a simple string (i.e. a no-op).

The middle comma is concatenation.

The whole function concatenates the string to itself.

Since all characters are identical, this works when reversed too.

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

Keg, -no, 2 bytes

#:

Don't try it online!

Ah yes, bad language design at it's finest. You can't try it online because it desperately needs a pull - and Dennis hasn't been around for a while.

The -no flag makes everything output as-is - without it, numbers would attempt to output as characters (not very helpful)

Explained

#:

Do nothing: standard cat program

:#

Duplicate the top of the stack and output the entire stack

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

Vyxal, 2 bytes

#d

Try it Online!

Very good trivial answer. Make sure to wrap input in double quotation marks.

Explained

#d

Nothing, a standard cat program

d#

Double the input string and output.

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

Pyth, 2 bytes

zz

Try it online!

z -> Prints the input.

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

Python 3, 17 bytes

#)2*)(tupni(tnirp

Try it online!

!enilno ti yrT

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

05AB1E (legacy), 2 bytes

x#

Try it online!

!enilno ti yrT

x doubles the input

\$\endgroup\$
2
  • \$\begingroup\$ Please ignore my previous comment. A note to people testing this out: you need to surround numbers with quotation marks for it to work with numbers \$\endgroup\$ Commented Mar 8, 2021 at 7:34
  • \$\begingroup\$ JD also works and doesn't require numbers to be quoted. \$\endgroup\$ Commented Mar 8, 2021 at 8:40
1
\$\begingroup\$

PHP -F, 15 bytes

;ngra$.ngra$=?<

Try it online!

Try it reversed!


PHP, 13 bytes

i$.i$>=)i$(nf

Outputs error when not reversed.

Try it online!

Try it reversed!

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

Charcoal, 3 bytes

²×⁰

Try it online! Outputs --.

⁰ײ

!enilno ti yrT Explanation: Outputs zero -s, then doubles the implicit input.

Charcoal normally takes string input, but you can force it to take numeric input. There are various ways of constructing a 5-byte program that forces string output, although in that case the unreversed output will no longer be --.

The assumes that both programs need to be syntactically valid, as Charcoal will normally ignore invalid programs rather than exiting with error. Abusing this would allow the programs to be reduced to 2 and 3 bytes respectively.

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

Batch, 16 bytes

@ rem&1%1% ohce@
@echo %1%1&rem @
\$\endgroup\$
0
\$\begingroup\$

YaBASIC, 8 bytes

//$i,$i?

Try it online!

?i$,i$//

!enilno ti yrT

A basic BASIC answer - hide the reversed source behind a comment! Original does nothing, reversed prints input i$ twice. This dialect recognizes // as a comment. I'd shave a byte if it would accept ' like Microsoft BASIC variations do!

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

05AB1E, 1 bytes

Ties AZTECCO's Japt answer for #1.

«

Try it online!

!enilno ti yrT

«  # full program
   # implicit input...
«  # concatenated with...
   # implicit input
   # implicit output
\$\endgroup\$

Your Answer

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