Schreiben Sie einen Code, der eine Zeichenfolge als Eingabe verwendet, und geben Sie einen Code in derselben Sprache aus , in der beim Ausführen die ursprüngliche Eingabezeichenfolge ausgegeben wird.
Es muss in der Lage sein, eine beliebige Kombination der Zeichen AZ, az und 0-9 zu verarbeiten.
Beispiel in Python:
import sys
print "print('" + sys.argv[1] + "')"
Bei Eingabe von testing123
wird es zurückgegeben print('testing123')
.
Da es sich um Code-Golf , wird die kürzeste Antwort in Bytes gewinnen.
code-golf
code-generation
Quarz
quelle
quelle
Antworten:
pl, 0 bytes
Code:
Try it online!
Explanation:
An empty program in pl is simply a cat program, outputting the exact same as the input.
In pl sind alle ASCII-Zeichen im Wesentlichen Variablen. Wenn die Variable jedoch (noch) nicht vorhanden ist, geht pl davon aus, dass sie Teil einer Zeichenfolge sind.
quelle
cat
interpreter. ;)brainfuck,
565533 bytes1 byte saved thanks to Gravitron, and 22 bytes saved thanks to Jo King!
Try it online!
Output requires an interpreter that wraps on memory underflow and has EOI=0. TIO has such an interpreter.
Explanation
--[>+<++++++]
initializes the tape with a single+
.The input loop:
Other solutions
Each line is its own solution.
quelle
+++++++++++++++
with+++[>+++++<-]>
(From brainfuck constants)0
's, with a pointer to a value.+
increments the value,-
decrements the value,>
moves the pointer right,<
moves the pointer left,,
reads input to the pointer as an ASCII character,.
outputs the value of the pointer as an ASCII character,[
starts a loop, and]
jumps to the corresponding[
if the value of the pointer is not0
. I'm bad at explaining, but that's how easy Brainfuck is. And Nice golf Conor!Mornington Crescent, 27640 bytes
Try it online!
Explanation
First, a quick refresher on Mornington Crescent:
Since printing strings is hard (see Martin Ender's Mornington Crescent "Hello, World!" program), we will need a simple template that can be used to output generic strings. Since the length of the generated programs don't matter, I've settled on this:
We'll need to generate each character separately and concatenate them, since the substring approach used by Martin is much harder to implement in the general case. Paddington is the station that performs string concatenation. When you arrive at Paddington, the following happens:
This behavior is used to initialize Paddington to the empty string that's assumed to be the input.
Go to Seven Sisters, which sets the accumulator to 7, and store it in the Bank. The Bank is the only way to copy values - Hammersmith is a readonly copy of Bank's value.
Divide 7 by 7 to get 1, a divisor of all ASCII values we need to support.
For each character, start by copying the 1 to Upminster.
Add more ones as necessary.
Finish up by going to Upminster only once, to keep the new value in the accumulator instead of storing it again. Take it to Charing Cross to turn the ASCII value into a character and append it to the string. The last two lines are there because I've misread the specification, so I'll stop here and golf my program down first.
quelle
Take District Line to Upminster
lines calculate the fibonacci sequenceJelly, 1 byte
Woo builtins!
The builtin
Ṿ
unevals the input.Try it online!
A more interesting one:
Joins the string "“”" by the input.
Try it online!
quelle
”“;
for -1.Charcoal, 1 byte
By default, a string of ASCII characters
..
~
simply prints itself, so it just remains to print the input.quelle
C (gcc),
89875856 bytesSaved two bytes thanks to Jakob!
Try it online!
quelle
puts
in the printed program to save 2 bytes, right? Or is it a problem that it appends a newline?Japt, 2 bytes
Try it online!
i
is a function that inserts a specified string at a specified index into the input.Q
is a variable that defaults to a quotation mark"
, and since there's no index given, this defaults to0
. The end result is a quotation mark inserted at the beginning of the string, which Japt auto-finishes when running.This works on strings containing anything except
"
,{
, or\
, though of course the string is guaranteed to consist of alphanumeric chars so this doesn't matter.quelle
Python 2, 22 bytes
Try it online!
quelle
print'print'+`input()`
.input
in Python 2 is equivalent toeval(raw_input())
. So it has to be a pythonic expression for it to work.MATLAB / Octave, 15 bytes
Try it online!
I completely overlooked the fact that the program only needs to handle alphanumeric characters, or, importantly, does not need to handle space characters. This means we can use the 'command' syntax, which uses significantly less bytes.
MATLAB, 27 bytes
For completeness, here's a version that can handle arbitrary input:
Defines an anonymous function taking the string
t
, and converts it to the program. Curiously, this does not work in Octave, which seems to have different ideas of whatmat2str
should do.quelle
@(t)['disp ',mat2str(t)]
should work...?mat2str
adds quotation marks, which are not necessary in command syntax. I had the function syntax combined withmat2str
to handle spaces in the input, but on rereading the challenge, that's not actually necessary, cutting the bytecount nearly in half. Thanks!Befunge-98 (FBBI),
211612 bytes-5 bytes thanks to NieDzejkob
-4 bytes thanks to Jo King
Try it online!
Outputs
't,'e,'s,'t,@
for an input oftest
How?
Initially the pointer moves East:
If there is no more input left,
~
reverses the direction of the pointer:quelle
Emojicode, 88 bytes
Try it online!
Input:
Output:
Script Output:
quelle
QBIC, 7 bytes
Explanation
Sample run
The output is a string literal containing the input, preceded with a
PRINT
command.quelle
Batch 11 bytes
I'm sure there's away to get it smaller, but this is pretty small.
The breakdown:
In English:
The user is asked for input with a leading phrase
echo
. Then the user may type in whatever, and the program terminates (e.g;echo hello world!
). This leaves us with a program that when ran, will outputhello world!
.Yes, the whitespace is supposed to be there
quelle
echo
portion be text echoed to the console and not program output?echo
to standard out. In fact, (I assume) if input comes from a file or pipe, all you'd see is the prompt. Has that kind of output been allowed?V, 2 bytes
In Vim,
i
changes to insert mode and allows inserting text. In this case, it just inserts thei
in front of the input text (to create the second program). V is Vim compatible, but adds an implicit<esc>
to the end of the program, so I don't need to worry about it here.quelle
PowerShell, 9 Bytes
single or double quotes in powershell can be used to contain strings, but only double-quotes allow variable expansion.
with an input of
foo
this will output'foo'
which is a valid powershell program which outputsfoo
quelle
Cubically, 87 bytes
Try it online!
A port of my answer here. There already existed a challenge to take input and output Cubically code that produced that input, and I had an answer in Cubically so... profit?
quelle
%6
s and@6
s; they can now be called implicitly. (You can also do that on your answer on the cubifier thread.)D,
9289 bytesTry it online!
quelle
Python 2, 17 bytes
Try it online!
quelle
1
'1'
to the function it correctly outputsprint'1'
SOGL V0.12, 2 bytes
Try it Here! (expects input on stack so
,
is added for ease-of-use)$
pushes”
,+
joins that after the input on the stack.quelle
"
.Perl 6, 19 bytes
Try it
This should work for any Unicode input in NFC
quelle
8th,
3437 bytesCode
Example
quelle
PHP, 12 bytes
Try it online!
This is to be run from the command line with the input as the parameter argument. And yes, it simply outputs the input string—but that is valid PHP: anything outside the
<?php
?>
delimiters is output unprocessed.Here is the script produced by the 'testing123' input in action as a demonstration: Try it online!
quelle
<?php
, mind you ;)MATL, 2 bytes
Try it online!
Simply wraps the string in quotes using
&D
, which is MATL's equivalent to MATLAB'smat2str
, a function specifically made to make something that, wheneval
'd, will give the original matrix (or in this case, string).The resulting 'program' is just the input string wrapped in quotes, which is thus pushed on the stack, and implicitly displayed.
quelle
eacal, 38 bytes
Try it online!
Simply appends the string
put string
to the memory string, then appends the argument, which is printed.quelle
Batch, 14 bytes
Works for special characters too if they are quoted using
"
s rather than^
.quelle
@echo.@echo.%*
would be more correct? The input could be potentially empty..
because that's never empty, and I used a(
for the second case because that's more reliable than.
, but thanks for the suggestions anyway!@echo.
s lolLy, 14 bytes
Try it online!
Explanation
quelle
05AB1E, 3 bytes
Try it online!
Surround input with quotes.
-1 @Adnan.
quelle
Brain-Flak,
9268 bytes-22 bytes thanks to Wheat Wizard
Includes +2 for
-cr
Try it online!
quelle
Pyth,
21 byteTry it online!
Just outputs the input. Requires strings to be in quotes.
-1 byte thanks to KarlKastor
quelle
+\"
? (\x
char literal + implicitQ
)+N
Carrot, 1 byte
How it works
An alternative, also at 1 byte is:
This works the same as the above but only takes the first line of input. Seeing as input will only ever be
[A-Za-z0-9]
this is fine.quelle
01
?