ทำล่ามให้ใช่!


10

ใช่เป็นสแต็คที่ใช้ภาษาที่มีคำแนะนำการใช้พื้นที่ที่แยกออกจากกันไม่กี่:

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

องค์ประกอบสุดท้ายของสแต็กจะถูกเอาต์พุตเมื่อสิ้นสุดโปรแกรม อักขระที่ไม่ใช่ตัวอักษรและไม่ใช่ช่องว่างทั้งหมดจะถูกละเว้น รหัสทั้งหมดเป็นตัวพิมพ์เล็ก โปรแกรมตัวอย่าง:

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

Hello, World!พิมพ์

what

พิมพ์อินพุต ( catโปรแกรม)

no nah

-1พิมพ์

no really

พิมพ์NULตัวละคร ( 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

พิมพ์อินพุตและขีดล่าง

yes no nope

พิมพ์ 0

yes no yep

1พิมพ์

คุณต้องเขียนล่ามเป็นไบต์น้อยที่สุด นี่คือการใช้งาน JS (ไม่ค่อยเล่นกอล์ฟ!):

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>

ดูคำตอบ JS ของฉันด้านล่าง

คุณอาจสมมติว่าจำนวนเต็มทั้งหมดที่เกี่ยวข้องจะน้อยกว่าหรือเท่ากับ 126 ซึ่งreallyจะไม่ถูกเรียกใช้ด้วยสตริงแบบมัลติชาร์ด้านบนสุดของสแต็กและสแต็กจะไม่ยาวเกิน 100 องค์ประกอบ

กรณีขอบ

  • yes yes oh11พิมพ์
  • Intput อาจเป็นสตริงหรือตัวเลข
  • รหัสสามารถมีตัวอักษรใด ๆ คนที่ไม่ตรงกัน[a-z ]ควรถูกละเว้น

1
โปรดตรวจสอบให้แน่ใจว่าเอกสารทดสอบของคุณมีคำสั่งทั้งหมด
Leun Nun

2
ควรใช้จำนวนเต็มชนิดใด มีน้ำล้นหรือไม่ จะreallyทำอย่างไรถ้ามีสตริงหลายตัวที่ด้านบนของสแต็ค?
Martin Ender

1
@ programmer5000 ไม่ควรyes yes ohพิมพ์11ใช่ไหม
user41805

1
มันไม่เป็นไรถ้าเราคิดว่าการป้อนข้อมูลที่มีเพียงตัวอักษรสามารถ[a-zA-Z ]?
user41805

1
ดันอินพุตอินพุตสามารถเป็นตัวเลขได้หรือไม่? อาร์เรย์? ตัวอักษร? สตริง? ถ้าเป็นเช่นนั้นสตริงอินพุตสามารถเป็นหลายบรรทัดได้หรือไม่? เพิ่มรายการสุดท้ายในสแต็คสิ่งนี้จะทำอย่างไรเมื่อรายการสุดท้ายไม่ใช่ตัวเลข?
Luis Mendo

คำตอบ:


4

05AB1E , 77 67 63 61 ไบต์

Að«Ã#vyÇO§}ðý•9ǝ×н}ÀÀÙ™Íð•650в"X ¾ I > < DdiçëÇ} J r\r \"#:.V

ถือว่าโปรแกรมอยู่ด้านบนสุดของสแต็ก

ลองออนไลน์!


คุณสามารถสมมติได้ว่าอินพุตทั้งหมดนั้นถูกต้องหรือไม่? ตัวอย่างเช่นคุณไม่สามารถรับโปรแกรมได้yes no sey yep
Emigna

@Emigna ไม่มีกรณีทดสอบเกี่ยวกับเรื่องนี้ไม่มีข้อมูลจำเพาะและ OP ไม่ได้ชี้แจงเรื่องนี้แม้จะมีใครบางคนแสดงความคิดเห็น
Okx

ฉันสังเกตเห็นว่าไม่มีกรณีทดสอบ ไม่เห็นอะไรเลยในความคิดเห็นเกี่ยวกับเรื่องนี้ดังนั้นฉันสงสัยว่าโปรแกรมอ้างอิงของเขาจะละเว้นคำสั่งที่ไม่ถูกต้องในขณะที่คุณอาจดำเนินการเป็นคำสั่งที่ถูกต้องหากผลรวมของรหัสอักขระตรงกับคำสั่งที่ถูกต้อง
Emigna

9

JavaScript (ES6), 218 215 204 203 203 ไบต์

สตริงโปรแกรมsและใส่ในไวยากรณ์i currying(s)(i)

s=>i=>s.replace(/\w+/g,S=>(c=eval("[P()];P()+1;[s.shift()];1;0;s=[s.join``];P()-1;i;P()[0]?k.charCodeAt():String.fromCharCode(k)".split`;`[parseInt(S,35)%156%9])).map||s.push(c),s=[],P=_=>k=s.pop())&&P()

อย่างไร?

เราใช้ฟังก์ชันแฮชที่สมบูรณ์แบบparseInt(S, 35) % 156 % 9เพื่อแปลงคำสั่งSเป็นดัชนีใน0 … 8และใช้ดัชนีนี้เพื่อเลือกรหัส JS ที่จะดำเนินการ:

instruction | base 35 -> dec. | % 156 | % 9 | JS code
------------+-----------------+-------+-----+---------------------------------------------
"yes"       |           42168 |    48 |   3 | 1
"no"        |             829 |    49 |   4 | 0
"what"      |         1393204 |   124 |   7 | i
"sure"      |         1238209 |    37 |   1 | P()+1
"nah"       |           28542 |   150 |   6 | P()-1
"really"    |      1439554619 |    35 |   8 | P()[0]?k.charCodeAt():String.fromCharCode(k)
"oh"        |             857 |    77 |   5 | s=[s.join``]
"nope"      |         1016414 |    74 |   2 | [s.shift()]
"yep"       |           42165 |    45 |   0 | [P()]

Pฟังก์ชั่นปรากฏรายการสุดท้ายที่ออกจากสแต็คsและโหลดลงในk

เราป้องกันผลลัพธ์ของคำแนะนำบางอย่างจากการถูกส่งกลับไปยังสแต็กโดยการทดสอบว่ามีการกำหนดวิธีการ. map ()นั่นคือถ้าผลลัพธ์เป็นอาร์เรย์ รหัสของโอ้คืนอาร์เรย์ด้วยการออกแบบและเราบังคับให้nopeและyepคืนค่าอาร์เรย์เช่นกัน ดังนั้นไวยากรณ์:

(c = eval("[code0];code1;...".split`;`[index])).map || s.push(c)

กรณีทดสอบ


5

โรดา , 256 ไบต์

f c,n{s=[];(c/`\W|_`)()|{|m|s+=#m-2 if[m=~"yes|no"];s[-1]+=#m*2-7 if[m=~"sure|nah"];s+=n if[m="what"];s=s[#m%3:#s-#m%2]if[m=~"nope|yep"];{s+=""s()|s[-1].=_}if[m="oh"];{t=s[-1]y=t..""a=t+0;a=a..""{s[-1]=ord(s)}if[#a>#y]else{s[-1]=chr(t)}}if[#m=6]}_;[s[-1]]}

ลองออนไลน์!

คำอธิบาย

#variableส่งคืนความยาวของvariable(ถ้าเป็นสตริงหรืออาร์เรย์)

f c,n{                         /*declare a function f with arguments c and n*/
s=[];                          /*initialise the stack*/
(c/`\W|_`)                     /*split the code on anything not [a-zA-Z0-9]*/
          ()|                  /*and push each of its values to the stream*/
{|m|...}_                      /*for each element m in the stream, do:*/
s+=#m-2 if[m=~"yes|no"];       /* add 1 or 0 to the stack if m is "yes" or "no"*/
s[-1]+=#m*2-7if[m=~"sure|nah"];/* increment or decrement the top element if m is "sure" or "nah"*/
s+=n if[m="what"];             /* push input if m is "what"*/
s=s[#m%3:#s-#m%2]              /* remove the first or last element of the stack
  if[m=~"nope|yep"];           /* if m is "nope" or "yep" */
{                 }if[m="oh"]; /* if m is "oh" do:*/
 s+=""                         /*  add an element to the stack*/
      s()|s[-1].=_             /*  for each element in s, concatenate that amount to the last element of the stack*/
{                   }if[#m=6]  /* if m is "really" (it's length is 6) do:*/
 t=s[-1]y=t..""                /*  get the last element of the stack*/
 a=t+0;a=a..""                 /*  add 0 to it, if a is a number, this does nothing, otherwise this makes a longer by 1 character*/
 {s[-1]=ord(s)}if[#a>#y]       /*  if a is longer than t (the last element is a char/string) replace it with its code point*/
 else{s[-1]=chr(t)}            /*  otherwise, replace the last element with the char it represents*/
 [s[-1]]                       /*finally output the top of the stack*/

3

Pyth , 69 ไบต์

Vczd=Y.v@c"+YsY X_1Y1 +Yw  +Y0 X_1Y_1    PY   +Y1 tY +PYCeY"d%CN23;eY

ลองออนไลน์!


ดูเหมือนจะไม่ทำงานในโปรแกรม "Hello, World"
Okx

@Okx มันใช้งานได้ ฉันเปลี่ยนโปรแกรมในลิงค์ของฉัน โปรแกรมดั้งเดิมยาวเกินไป
Leun Nun

หรือโปรแกรมอื่น ๆ อีกมากมายสำหรับเรื่องนั้น 013456789yes 012345678no ohพิมพ์ 0 เอาต์พุตที่คาดหวังคือ 10
Okx

นอกจากนี้ยังไม่ได้ผลสำหรับกรณีขอบyes yes oh(ระบุไว้ในคำถาม) 11การส่งออกที่คาดไว้คือ
Okx

@Okx ดูความคิดเห็น OP ระบุสิ่งนี้เป็นการทำงานที่ไม่ได้กำหนดไว้โดยเฉพาะ
Leun Nun

1

JS (ES6), 361 340 ไบต์

c=>i=>(s=[],r="s[s.length-1]",c.replace(/[^a-z ]/g,"").split` `.map(x=>(eval({"yes":"s.push(1)","no":"s.push(0)","what":"s.push(i)","sure":"~++","nah":"~--","really":"~=((typeof ~)[0]<'o'?String.fromCharCode(~):lastItem.charCodeAt())","oh":"s.push(s.reduce((x,y)=>''+x+y))","nope":"s.shift()","yep":"s.pop()"}[x].replace(/~/g,r)))),eval(r))

ใช้รหัสและอินพุตผ่านการแกง

var yes = 
c=>i=>(s=[],r="s[s.length-1]",c.replace(/[^a-z ]/g,"").split` `.map(x=>(eval({"yes":"s.push(1)","no":"s.push(0)","what":"s.push(i)","sure":"~++","nah":"~--","really":"~=((typeof ~)[0]<'o'?String.fromCharCode(~):lastItem.charCodeAt())","oh":"s.push(s.reduce((x,y)=>''+x+y))","nope":"s.shift()","yep":"s.pop()"}[x].replace(/~/g,r)))),eval(r))
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>


1

JavaScript (ES6), 220 216 ไบต์

c=>i=>c.replace(/\w+/g,x=>a=a.concat(eval("Q,[]  (b=Q)[0]?b.charCodeAt():String.fromCharCode(b) 1    [a.join``,a=[]][0] Q+1 a.shift(),[]  i  Q-1".split`Q`.join`a.pop()`.split` `[parseInt(x,36)%19]||0)),a=[])&&a.pop()

f(code)(input)ฟังก์ชั่นที่จะเข้ากับความดีความชอบไวยากรณ์เช่น


1

Python 2 , 258 ไบต์

s=[]
m=-1
i,p=input()
for c in p.split(' '):
 k=c<"o"
 if c in"yesnowhat":s+=[i if"w"==c[0]else-k+1]
 if c in"surenah":s[m]+=[1,m][k]
 if"p"in c:s.pop(k-1)
 if"oh"==c:s+=[''.join(map(str,s))]
 if"y"==c[m]:s[m]="'"in`s[m]`and ord(s[m])or chr(s[m])
print(s[m])

ลองออนไลน์!


-3 ไบต์ขอบคุณ @Wondercricket


1
คุณสามารถบันทึก 4 ไบต์โดยจัดเก็บ-1เป็นตัวแปร
Wondercricket

1

Perl 6 ,  233  225 ไบต์

{my @s;{yes=>{@s.push(1)},no=>{@s.push(0)},what=>{@s.push: once slurp},sure=>{++@s.tail},nah=>{--@s.tail},really=>{($/:=@s.tail)~~Int??$/.=chr!!$/.=ord},oh=>{@s.=join},nope=>{@s.shift},yep=>{@s.pop},}{.words}.map:{.()};@s.tail.print}

ลองใช้
Hello World
cat
-1
nul
cat_
0
1

{my @s;{es=>{@s.push(1)},no=>{@s.push(0)},at=>{@s.push: once slurp},re=>{++@s.tail},ah=>{--@s.tail},ly=>->{($_:=@s.tail)~~Int??.=chr!!.=ord},oh=>{@s.=join},pe=>{@s.shift},ep=>{@s.pop},}{.comb(/..»/)}.map:{.()};@s.tail.print}

การทำงานเดียวกันยกเว้นว่ามันจะคว้าเพียงตัวละครสองตัวสุดท้ายจากแต่ละคำสั่งและใช้$_แทนสำหรับ$/really

ลอง (Hello World)

ขยาย:

{
  my @s; # stack

  {  # Associative array
    yes    => {@s.push(1)},
    no     => {@s.push(0)},
    what   => {@s.push: once slurp}, # read everything from $*IN
    sure   => {++@s.tail},
    nah    => {--@s.tail},
    really => {
          ( $/ := @s.tail ) # bind $/ to the last value in the stack
          ~~ Int            # if that is an Int
      ??  $/.=chr           # replace it with that character
      !!  $/.=ord           # otherwise replace it with its ordinal
    },
    oh     => {@s.=join},
    nope   => {@s.shift},
    yep    => {@s.pop},
  }\
  { .words }                # index by the words in the program
  .map: {.()};              # call each of the lambdas in order

  @s.tail.print             # print the last value on the stack
}

1

PHP, 315 305 ไบต์

ร่างที่สองยังไม่ได้ทดสอบ

foreach($argv as$k=>$v)if($k>1)eval((strstr($c=preg_replace('#[^a-z ]#','',$v),p)?'':'$s[]=').[yes=>1,no=>0,what=>'$argv[1]',sure=>'array_pop($s)+1',nah:'array_pop($s)-1',really=>'is_int($x=array_pop($s))?chr($x):ord($x)',oh=>'join($s)',nope=>'array_shift($s)',yep=>'array_pop($s)'][$c].';');echo end($s);

php -nr '<php-code>' <input> <yes-code>ทำงานด้วย

ทำให้พังถล่ม

foreach($argv as$k=>$v)if($k>1)         # loop through commands
    eval(                                   # 3. interprete
        (strstr(                            # 2. if no 'p' in command, prepend '$s[]='
                                            # 1. ignore all non-code characters
            $c=preg_replace('#[^a-z ]#','',$v),p)?'':'$s[]=').
        [yes=>1,                                # yes: append 1
        no=>0,                                  # no: append 0
        what=>'$argv[1]',                       # what: append input
        sure=>'array_pop($s)+1',                # sure: remove end, increment, append
        nah:'array_pop($s)-1',                  # nah: remove end, decrement, append
                                                # really: convert between ascii and ordinal
        really=>'is_int($x=array_pop($s))?chr($x):ord($x)',
        oh=>'join($s)',                         # oh: concatenate elements, append
        nope=>'array_shift($s)',                # nope: remove first element
        yep=>'array_pop($s)']                   # yep: remove last element
        [$c]
    .';');
echo end($s);                           # print last element (if exists)
โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.