Sí es un lenguaje basado en pila que tiene algunas instrucciones separadas por espacios:
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
El último elemento de la pila siempre se emite al final del programa. Se ignoran todos los caracteres no alfanuméricos y no espaciales. Todo el código es minúscula. Programas de ejemplo:
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
impresiones Hello, World!
.
what
imprime la entrada ( cat
programa)
no nah
impresiones -1
.
no really
imprime el NUL
caracter ( 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
imprime la entrada y un guión bajo.
yes no nope
huellas dactilares 0
yes no yep
impresiones 1
.
Debe escribir un intérprete en la menor cantidad de bytes posible. Aquí hay una implementación de JS (¡no muy bien!):
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>
Ver también mi respuesta JS a continuación.
Puede suponer que todos los enteros involucrados serán menores o iguales a 126, que really
nunca se ejecutarán con una cadena de caracteres múltiples en la parte superior de la pila, y que la pila nunca tendrá más de 100 elementos.
Casos de borde
yes yes oh
impresiones11
.- Intput puede ser una cadena o un número.
- El código puede contener caracteres. Los que no coinciden
[a-z ]
deben ser ignorados.
fuente
really
si hay una cadena de caracteres múltiples en la parte superior de la pila?yes yes oh
imprimir11
?[a-zA-Z ]
?Respuestas:
05AB1E ,
77676361 bytesAsume que el programa está en la parte superior de la pila.
Pruébalo en línea!
fuente
yes no sey yep
JavaScript (ES6),
218215204203 bytesToma la cadena del programa
s
y la entradai
en la sintaxis de curry(s)(i)
.¿Cómo?
Usamos la función hash perfecta
parseInt(S, 35) % 156 % 9
para convertir la instrucción S en un índice en 0 ... 8 y usamos este índice para seleccionar el código JS que se ejecutará:La función P saca el último elemento de la pila sy lo carga en k .
Prevenimos que el resultado de algunas instrucciones se devuelva a la pila al probar si el método .map () está definido, es decir, si el resultado es una matriz. El código para oh está devolviendo una matriz por diseño y forzamos a nope y sip a devolver matrices también. De ahí la sintaxis:
Casos de prueba
Mostrar fragmento de código
fuente
Röda , 256 bytes
Pruébalo en línea!
Explicación
#variable
devuelve la longitud devariable
(si es una cadena o una matriz).fuente
Pyth , 69 bytes
Pruébalo en línea!
fuente
013456789yes 012345678no oh
imprime 0, la salida esperada es 10.yes yes oh
(especificado en la pregunta), el resultado esperado es11
.JS (ES6),
361340 bytesToma código y entrada a través de curry.
fuente
JavaScript (ES6),
220216 bytesUna función que toma entrada con sintaxis de curry, como
f(code)(input)
.fuente
Python 2 , 258 bytes
Pruébalo en línea!
-3 bytes gracias a @Wondercricket
fuente
-1
como una variablePerl 6 ,
233225 bytesPruébalo
Hello World
cat
-1
nul
cat_
0
1
Funciona igual, excepto que solo toma los dos últimos caracteres de cada comando y los usa en
$_
lugar de$/
forreally
.Pruébalo (Hola mundo)
Expandido:
fuente
PHP,
315305bytessegundo borrador, aún no probado
Corre con
php -nr '<php-code>' <input> <yes-code>
.Descompostura
fuente