29
\$\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\$
  • \$\begingroup\$ Can it be a function? \$\endgroup\$ – totallyhuman Aug 13 '17 at 19:34
  • \$\begingroup\$ @manatwork (S)he edited it just now. \$\endgroup\$ – NoOneIsHere Aug 13 '17 at 19:42
  • \$\begingroup\$ Must input be a string? Or can it be an integer? \$\endgroup\$ – Shaggy Aug 13 '17 at 19:48
  • \$\begingroup\$ Must be able to handle both integer and string inputs. \$\endgroup\$ – Quartzic Aug 13 '17 at 20:00
  • 2
    \$\begingroup\$ Can we output a function as per the defauts on meta? \$\endgroup\$ – JAD Aug 14 '17 at 18:26

72 Answers 72

0
\$\begingroup\$

AWK, 25 bytes

$0="BEGIN{print\""$0"\"}"

Try it online!

Should work for most input other than ".

If some (possibly empty) input is allowed for the secondary program this could be shortened to:

$0="$0=\""$0"\""
\$\endgroup\$
0
\$\begingroup\$

dc, 10 bytes

91Pn93PACP

Try it online!

Straightforward: print an open bracket, then the input, then the close bracket and the letter p for the print command. 91Pn23920P also works.

\$\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\$
  • \$\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 '18 at 2:50
  • \$\begingroup\$ Or zero bytes using REPL? \$\endgroup\$ – lebatsnok Jun 19 '18 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 '18 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\$
  • \$\begingroup\$ wat O_o what even is .. ... .. \$\endgroup\$ – ASCII-only Jun 23 '18 at 4:20
  • \$\begingroup\$ .. = [Concatenate] ... = Argument .. = [Concatenate again] \$\endgroup\$ – Alex Allen Jun 24 '18 at 12:09
0
\$\begingroup\$

Scala, 79 bytes

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

Try it online!

\$\endgroup\$
  • \$\begingroup\$ you shouldn't use that as argument since it doesn't work \$\endgroup\$ – ASCII-only Jun 23 '18 at 4:17
  • \$\begingroup\$ @ASCII-only I didn't get it, can you help me explain a more bit? \$\endgroup\$ – Shankar Shastri Jun 23 '18 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 '18 at 4:20
  • \$\begingroup\$ @ASCII-only Thanks :-) \$\endgroup\$ – Shankar Shastri Jun 23 '18 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 '18 at 4:24
-1
\$\begingroup\$

JavaScript Firefox, 6 bytes

uneval
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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