30
\$\begingroup\$

Write a piece of code that takes a string as input, and outputs a piece of code in the same language that, when run, will output the initial input string.

It must be able to handle any combination of characters A-Z, a-z, and 0-9.

Example in Python:

import sys
print "print('" + sys.argv[1]  + "')"

Given the input of testing123, it will return print('testing123').

Since this is , the shortest answer in bytes will win.

\$\endgroup\$
12
  • \$\begingroup\$ Can it be a function? \$\endgroup\$ Aug 13, 2017 at 19:34
  • \$\begingroup\$ @manatwork (S)he edited it just now. \$\endgroup\$ Aug 13, 2017 at 19:42
  • \$\begingroup\$ Must input be a string? Or can it be an integer? \$\endgroup\$
    – Shaggy
    Aug 13, 2017 at 19:48
  • \$\begingroup\$ Must be able to handle both integer and string inputs. \$\endgroup\$
    – quartzic
    Aug 13, 2017 at 20:00
  • 2
    \$\begingroup\$ Can we output a function as per the defauts on meta? \$\endgroup\$
    – JAD
    Aug 14, 2017 at 18:26

79 Answers 79

1 2
3
0
\$\begingroup\$

Befunge-98 (PyFunge), 30 bytes

'":v
~\#[
<v">:#,_""#
<>4k,'@,

Try it online!

This prints a program in the form "...ihgfedcba">:#,_@, which prints abcdefghi...

Explanation

The code boils down to:

'":v    Push 2 quotation marks

~\#[    Get input and swap the top 2 items until EOF.
            Because every time we put a new value from input on the stack we also swap,
            the second quotation mark will float to the top, surrounding the input.

   >:#,_       A standard "print until the top is 0" loop
        ""#    Pushes a space, which is fine, since we control how much we print later
<v">:#,_"      When the IP gets reversed, we actually push the loop backwards onto
               the stack. This lets us print it later, instead of having to hardcode it.

 >4k,'@,       Prints 5 characters, being the printing loop from before, along with a @
<     @,       Prints an extra space, but it doesn't matter, and ends the program
\$\endgroup\$
0
\$\begingroup\$

J, 12 bytes

'echo',quote

Try it online!

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

Ruby -n, 12 bytes

$><<"p'#$_'"

Try it online!

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

Aceto, 14 13 11 bytes

'"drsJJp'pp

Try it online!

'"       push a quote character
  d       duplicate
   r       get input
    s       swap top stack items, so the input is between the two quotes
     JJ      Join top two items, 2x
       p  p    print
        'p      push literal:p 
\$\endgroup\$
0
0
\$\begingroup\$

Javascript, 20 bytes

Uses eval to make a new function from the input string.

a=>eval(`a=>'${a}'`)
\$\endgroup\$
0
\$\begingroup\$

Julia 0.6, 18 bytes

x->"print(\"$x\")"

Try it online!

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

JavaScript Firefox, 6 bytes

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

WinDbg, 14 bytes

.echo.echo $u0

Input is taken by setting the fixed name alias $u0 (so that input can be either a string or number).

Running this will print:

.echo {value_of_$u0}

Which when executed prints the input.

Example:

$$ set input
r$.u0=testing123

$$ run code
.echo.echo $u0
$$output: .echo testing123

$$ run output
.echo testing123
$$output: testing123

How it works:

.echo            $$ print the rest of the statement
     .echo       $$ part of the string that gets printed
           $u0   $$ automatically expands to the value of the fixed name alias,
                 $$ the expanded alias is printed
                 $$ because only a-zA-Z0-9 needs to be handled, no special logic around the
                 $$ alias expansion with regard to spaces, ", or ; interfering with the
                 $$ next .echo needs to be done

How the output works:

.echo            $$ print the rest of the statement
      {string}   $$ the string that gets printed, guaranteed to be one statement because
                 $$ it can't contain ; or " and can't start with a space
\$\endgroup\$
0
\$\begingroup\$

T-SQL, 28 bytes

SELECT'PRINT'''+s+''''FROM t

An input table is allowed per our IO rules, so I simply create a PRINT statement with the appropriate quotation marks.

An input of FooBarBaz109481 in the input table t will result in the following output:

PRINT'FooBarBaz109481'

Which will output the original string.

The original question says the string will only contain alphanumerics, so we won't have to handle any quote marks in the input itself.

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

JavaScript (Node.js), 49 bytes

console.log('console.log("'+process.argv[2]+'")')

Try it online!

Takes input through argv[2], the first argument in TIO (May be different depending on where it is tested), and prints in the console.log("YOUR_MESSAGE") format. Requires a string without quotes (unless escaped with \) with available characters A-Z, a-z, and 0-9.

Explanation

console.log(           // Log the output (open the outer log statement)
  'console.log("'      // Take console.log with opening parenthesis and quote
    + process.argv[2]  // Add the command-line argument after the quote (the input)
    + '")'             // Add the closing quote and parenthesis
)                      // Close the outer log statement
\$\endgroup\$
0
\$\begingroup\$

R, 34 bytes

cat("cat(",scan(,""),")",sep="\"")

Try it online!

Non-functional answer - longer than the functional one

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

R, 5 4 1 0 bytes

Thanks to JayCe for contributing the one-byte answer!

There are a number of short R programs that would ...

take a string as input, and output a piece of code in the same language that, when run, will output the initial input string

. Some of these are listed below, one on each row, in descending order of length:

identity     # 8 bytes
force        # 5 bytes
print
eval         # 4 bytes
c            # 1 byte
             # 0 bytes

This is inspired by NoOneIsHere's 1-byte answer in Pyth which is said to "just evaluate the input". This is exactly what force and identity do. eval and c achieve the same result in different ways. But why stop here? When you feed a string into R, it will be evaluated (to itself) and printed in quotes. A quoted string is a valid R program. That's the idea of the 0-byte answer.

Uniquely, the same program works identically in R and Python:

R way

> "yo ho ho and a bottle of rum"
[1] "yo ho ho and a bottle of rum"

Python way

>>> 'yo ho ho and a bottle of rum'
'yo ho ho and a bottle of rum'
\$\endgroup\$
3
  • \$\begingroup\$ Funny I had not seen your answers before posting mine. If way is valid there is a function way shorter than eval that does the job... it’s actually just one byte... c \$\endgroup\$
    – JayCe
    Jun 19, 2018 at 2:50
  • \$\begingroup\$ Or zero bytes using REPL? \$\endgroup\$
    – lebatsnok
    Jun 19, 2018 at 7:24
  • 2
    \$\begingroup\$ The question is really what “output” means... If it means “printing” then this answer is not valid. \$\endgroup\$
    – JayCe
    Jun 19, 2018 at 12:49
0
\$\begingroup\$

Excel VBA, 10 bytes

An anonymous function that outputs an anonymous function that prints the value of cell A1.

?"?"""[A1]

Example I/O

Given that cell [A1] that is populated with One

?"?"""[A1]
?"One
One
\$\endgroup\$
0
\$\begingroup\$

Yabasic, 22 bytes

An anonymous function that takes input, S$ as a string and outputs a function that outputs S$ to the console.

This relies on the fact that the Input Call will print a ? character to the console as a prompt when no explicit prompt is provided

Input S$
?"\""+S$+"\""

Try it online!

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

Lua, 29 bytes

print('print("'.. ... ..'")')

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ wat O_o what even is .. ... .. \$\endgroup\$
    – ASCII-only
    Jun 23, 2018 at 4:20
  • \$\begingroup\$ .. = [Concatenate] ... = Argument .. = [Concatenate again] \$\endgroup\$
    – Alex Allen
    Jun 24, 2018 at 12:09
0
\$\begingroup\$

Scala, 79 bytes

println(s"""object x{def main(a:Array[String])=println("${args.mkString}")}""")

Try it online!

\$\endgroup\$
7
  • \$\begingroup\$ you shouldn't use that as argument since it doesn't work \$\endgroup\$
    – ASCII-only
    Jun 23, 2018 at 4:17
  • \$\begingroup\$ @ASCII-only I didn't get it, can you help me explain a more bit? \$\endgroup\$ Jun 23, 2018 at 4:18
  • \$\begingroup\$ 1. "asdf",10 doesn't work, 2. you don't have to support that. so just change it to letters and numbers only \$\endgroup\$
    – ASCII-only
    Jun 23, 2018 at 4:20
  • \$\begingroup\$ @ASCII-only Thanks :-) \$\endgroup\$ Jun 23, 2018 at 4:21
  • \$\begingroup\$ oh. also you have to return a valid submission, i.e. a function or program (also your submission also has to be valid. so you'd need your header and footer inside your post body as well here \$\endgroup\$
    – ASCII-only
    Jun 23, 2018 at 4:24
0
\$\begingroup\$

Perl 5 -p, 12 bytes

$_="say'$_'"

Try it online!

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

Rust, 26 bytes

Input needs to be enclosed in double quotes: ""

|a|print!("||print!({a})")

Playground

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

Thunno 2, 1 byte

Attempt This Online!

Built-in for "repr" (Thunno 2 has implicit input). Essentially this surrounds the string with double quotes. So, when you run the program a string gets pushed to the stack and gets implicitly output.

\$\endgroup\$
1 2
3

Your Answer

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

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