Ya adalah bahasa berbasis tumpukan yang memiliki beberapa petunjuk yang dipisahkan oleh ruang:
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
Elemen terakhir stack selalu dihasilkan pada akhir program. Semua karakter non-alfanumerik dan non-spasi diabaikan. Semua kode adalah huruf kecil. Contoh program:
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
cetakan Hello, World!
.
what
mencetak input ( cat
program.)
no nah
cetakan -1
.
no really
mencetak NUL
karakter ( 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
mencetak input dan garis bawah.
yes no nope
cetakan 0
yes no yep
cetakan 1
.
Anda harus menulis penerjemah sesedikit mungkin. Berikut ini adalah implementasi JS (tidak golf dengan baik!):
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>
Lihat juga jawaban JS saya di bawah ini.
Anda dapat mengasumsikan bahwa semua bilangan bulat yang terlibat akan kurang dari atau sama dengan 126, yang really
tidak akan pernah dijalankan dengan string multi-char di atas tumpukan, dan bahwa tumpukan tidak akan pernah lebih dari 100 elemen.
Kasus tepi
yes yes oh
cetakan11
.- Intput dapat berupa string atau angka.
- Kode dapat berisi karakter apa saja. Yang tidak cocok
[a-z ]
harus diabaikan.
sumber
really
dilakukan jika ada string multi-karakter di atas tumpukan?yes yes oh
mencetak11
?[a-zA-Z ]
?Jawaban:
05AB1E ,
77676361 byteAsumsikan program berada di atas tumpukan.
Cobalah online!
sumber
yes no sey yep
JavaScript (ES6),
218215204203 byteMengambil string program
s
dan inputi
dalam sintaks currying(s)(i)
.Bagaimana?
Kami menggunakan fungsi hash sempurna
parseInt(S, 35) % 156 % 9
untuk mengubah instruksi S menjadi indeks dalam 0… 8 dan menggunakan indeks ini untuk memilih kode JS yang akan dieksekusi:The P Fungsi muncul item terakhir dari tumpukan s dan beban ke k .
Kami mencegah hasil beberapa instruksi agar tidak didorong kembali ke stack dengan menguji apakah metode .map () didefinisikan, yaitu, jika hasilnya adalah sebuah array. Kode untuk oh mengembalikan array dengan desain dan kami memaksa nggak dan ya untuk mengembalikan array juga. Karena itu sintaksnya:
Uji kasus
Tampilkan cuplikan kode
sumber
Röda , 256 byte
Cobalah online!
Penjelasan
#variable
mengembalikan panjangvariable
(jika itu adalah string atau array).sumber
Pyth , 69 byte
Cobalah online!
sumber
013456789yes 012345678no oh
mencetak 0, output yang diharapkan adalah 10.yes yes oh
(ditentukan dalam pertanyaan), output yang diharapkan adalah11
.JS (ES6),
361340 byteMengambil kode dan input melalui currying.
sumber
JavaScript (ES6),
220216 byteFungsi yang mengambil input dengan sintaks currying, seperti
f(code)(input)
.sumber
Python 2 , 258 byte
Cobalah online!
-3 byte berkat @Wondercricket
sumber
-1
sebagai variabelPerl 6 ,
233225 byteCobalah
Hello World
cat
-1
nul
cat_
0
1
Bekerja sama kecuali hanya mengambil dua karakter terakhir dari setiap perintah, dan menggunakan
$_
bukan$/
untukreally
.Cobalah (Hello World)
Diperluas:
sumber
PHP,
315305 bytedraft kedua, belum diuji
Jalankan dengan
php -nr '<php-code>' <input> <yes-code>
.kerusakan
sumber