Tak to język oparty na stosie, który zawiera kilka instrukcji rozdzielonych spacjami:
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
Ostatni element stosu jest zawsze wyprowadzany na końcu programu. Wszystkie znaki niealfanumeryczne i spacje są ignorowane. Cały kod jest pisany małymi literami. Przykładowe programy:
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
odciski Hello, World!
.
what
wypisuje wejście ( cat
program.)
no nah
odciski -1
.
no really
wypisuje NUL
znak ( 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
wypisuje dane wejściowe i podkreślenie.
yes no nope
odciski 0
yes no yep
odciski 1
.
Musisz napisać tłumacza w jak najmniejszej liczbie bajtów. Oto implementacja JS (niezbyt dobrze zagrana w golfa!):
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>
Zobacz także moją odpowiedź JS poniżej.
Możesz założyć, że wszystkie zaangażowane liczby całkowite będą mniejsze lub równe 126, że really
nigdy nie będą uruchamiane z ciągiem znaków o wielu znakach na szczycie stosu i że stos nigdy nie będzie dłuższy niż 100 elementów.
Skrzynie na brzeg
yes yes oh
odciski11
.- Dane wejściowe mogą być ciągiem lub liczbą.
- Kod może zawierać dowolne znaki. Te, które nie pasują,
[a-z ]
należy zignorować.
źródło
really
zrobić, jeśli na stosie znajduje się ciąg znaków składający się z wielu znaków?yes yes oh
drukować11
?[a-zA-Z ]
?Odpowiedzi:
05AB1E ,
77676361 bajtówZakłada, że program znajduje się na górze stosu.
Wypróbuj online!
źródło
yes no sey yep
JavaScript (ES6),
218215204203 bajtówPobiera ciąg programu
s
i dane wejściowei
w składni curry(s)(i)
.W jaki sposób?
Używamy idealnej funkcji skrótu
parseInt(S, 35) % 156 % 9
do konwersji instrukcji S na indeks w 0… 8 i używamy tego indeksu do wyboru kodu JS do wykonania:Funkcja P wysuwa ostatni element ze stosu s i ładuje go do k .
Zapobiegamy wypychaniu wyników niektórych instrukcji z powrotem na stos, testując, czy zdefiniowano metodę .map () , to znaczy, czy wynikiem jest tablica. Kod dla oh zwraca tablicę zgodnie z projektem, a my wymuszamy, aby nope i yep również zwracały tablice. Stąd składnia:
Przypadki testowe
Pokaż fragment kodu
źródło
Röda , 256 bajtów
Wypróbuj online!
Wyjaśnienie
#variable
zwraca długośćvariable
(jeśli jest to ciąg znaków lub tablica).źródło
Pyth , 69 bajtów
Wypróbuj online!
źródło
013456789yes 012345678no oh
wypisuje 0, oczekiwany wynik to 10.yes yes oh
(określonego w pytaniu), oczekiwany wynik to11
.JS (ES6),
361340 bajtówPobiera kod i dane wejściowe poprzez curry.
źródło
JavaScript (ES6),
220216 bajtówFunkcja, która pobiera dane wejściowe ze składnią curry, np
f(code)(input)
.źródło
Python 2 , 258 bajtów
Wypróbuj online!
-3 bajty dzięki @Wondercricket
źródło
-1
jako zmiennąPerl 6 ,
233225 bajtówWypróbuj
Hello World
cat
-1
nul
cat_
0
1
Działa tak samo, ale pobiera tylko dwie ostatnie postacie z każdego polecenia i używa
$_
zamiast$/
dlareally
.Wypróbuj (Hello World)
Rozszerzony:
źródło
PHP,
315305 bajtówdrugi szkic, jeszcze nie przetestowany
Uruchom z
php -nr '<php-code>' <input> <yes-code>
.awaria
źródło