Ja ist eine stapelbasierte Sprache mit einigen durch Leerzeichen getrennten Anweisungen:
yes: Push 1 to the stack
no: Push 0 to the stack
what: Push the input to the stack (input is taken at the start of program execution and is the same for the whole execution)
sure: Increment the last item in the stack
nah: Decrement the last item in the stack
really: If the last stack item is a number, replace it with its Unicode character. If it is a letter, replace it with its Unicode char code.
oh: convert the stack to strings and concatenate it, and push that to the stack.
nope: remove the first stack item
yep: remove the last stack item
Das letzte Element des Stapels wird immer am Programmende ausgegeben. Alle nicht alphanumerischen und nicht Leerzeichen werden ignoriert. Der gesamte Code ist klein geschrieben. Beispielprogramme:
yes sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure really
yes sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure really
yes sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure really
yes sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure really
yes sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure really
yes sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure really
yes sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure really
yes sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure really
yes sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure really
yes sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure really
yes sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure really
yes sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure really
yes sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure really
oh
druckt Hello, World!
.
what
druckt die Eingabe ( cat
Programm.)
no nah
druckt -1
.
no really
druckt das NUL
Zeichen ( U+0000
)
what
yes sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure sure really
oh
druckt die Eingabe und einen Unterstrich.
yes no nope
druckt 0
yes no yep
druckt 1
.
Sie müssen einen Interpreter in möglichst wenigen Bytes schreiben. Hier ist eine JS-Implementierung (nicht gut gespielt!):
function yes(code, input){
var stack = [];
var functions = {
"yes": "stack.push(1)",
"no": "stack.push(0)",
"what": "stack.push(input)",
"sure": "stack[stack.length - 1] ++",
"nah": "stack[stack.length - 1] --",
"really": "stack[stack.length - 1] = (typeof lastItem === 'number' ? String.fromCharCode(lastItem) : lastItem.charCodeAt())",
"oh": "stack.push(stack.reduce((x, y)=>''+x+y))",
"nope": "stack.shift()",
"yep": "stack.pop()"
};
code.replace(/[^a-z ]/g, "").split(" ").map(x=>(lastItem = stack[stack.length - 1],eval(functions[x])));
return stack[stack.length - 1];
}
textarea{
display: block;
}
Code: <textarea id = "code"></textarea>
Input: <textarea id = "input"></textarea>
<button onclick = "output.value = yes(code.value, input.value)">Run</button>
<textarea id = "output"></textarea>
Siehe auch meine JS-Antwort unten.
Sie können davon ausgehen, dass alle beteiligten Ganzzahlen kleiner oder gleich 126 sind, really
niemals mit einer Zeichenfolge mit mehreren Zeichen über dem Stapel ausgeführt werden und dass der Stapel niemals länger als 100 Elemente sein wird.
Randfälle
yes yes oh
druckt11
.- Die Eingabe kann eine Zeichenfolge oder eine Zahl sein.
- Der Code kann beliebige Zeichen enthalten. Nicht übereinstimmende
[a-z ]
sollten ignoriert werden.
quelle
really
es, wenn sich oben auf dem Stapel eine mehrstellige Zeichenfolge befindet?yes yes oh
drucken11
?[a-zA-Z ]
?Antworten:
05AB1E ,
77676361 BytesAngenommen, das Programm befindet sich oben auf dem Stapel.
Probieren Sie es online aus!
quelle
yes no sey yep
JavaScript (ES6),
218215204203 ByteÜbernimmt die Programmzeichenfolge
s
und die Eingabei
in Curry-Syntax(s)(i)
.Wie?
Wir verwenden die perfekte Hash-Funktion
parseInt(S, 35) % 156 % 9
, um den Befehl S in einen Index in 0… 8 umzuwandeln, und verwenden diesen Index, um den auszuführenden JS-Code auszuwählen:Die P- Funktion nimmt das letzte Element vom Stapel s und lädt es in k .
Wir verhindern, dass das Ergebnis einiger Anweisungen auf den Stapel zurückgeschoben wird, indem wir testen, ob die .map () -Methode definiert ist, dh ob das Ergebnis ein Array ist. Der Code für oh gibt ein Array von Entwurf zurück und wir zwingen nope und yep , auch Arrays zurückzugeben. Daher die Syntax:
Testfälle
Code-Snippet anzeigen
quelle
Röda , 256 Bytes
Probieren Sie es online aus!
Erläuterung
#variable
Gibt die Länge von zurückvariable
(wenn es sich um eine Zeichenfolge oder ein Array handelt).quelle
Pyth , 69 Bytes
Probieren Sie es online aus!
quelle
013456789yes 012345678no oh
druckt 0, erwartete Ausgabe ist 10.yes yes oh
(in der Frage angegeben), erwartete Ausgabe ist11
.JS (ES6),
361340 BytesNimmt Code und Eingabe durch Curry.
quelle
JavaScript (ES6),
220216 ByteEine Funktion, die Eingaben mit Curry-Syntax übernimmt, wie z
f(code)(input)
.quelle
Python 2 , 258 Bytes
Probieren Sie es online aus!
-3 Bytes dank @Wondercricket
quelle
-1
als Variable speichernPerl 6 ,
233225 BytesProbieren Sie es aus
Hello World
cat
-1
nul
cat_
0
1
Funktioniert genauso, außer dass nur die letzten beiden Zeichen aus jedem Befehl erfasst und
$_
anstelle von$/
for verwendet werdenreally
.Probieren Sie es aus (Hallo Welt)
Erweitert:
quelle
PHP,
315305 Byteszweiter Entwurf, noch nicht getestet
Laufen Sie mit
php -nr '<php-code>' <input> <yes-code>
.Nervenzusammenbruch
quelle