हाँ के लिए एक दुभाषिया बनाओ!


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

आपको संभव के रूप में कुछ बाइट्स में एक दुभाषिया लिखना होगा। यहाँ एक जेएस कार्यान्वयन (अच्छी तरह से गोल्फ नहीं है!):

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>

नीचे मेरा जेएस उत्तर भी देखें ।

आप मान सकते हैं कि इसमें शामिल सभी पूर्णांक 126 से कम या बराबर reallyहोंगे , जो कभी भी स्टैक के शीर्ष पर बहु-चार स्ट्रिंग के साथ नहीं चलाया जाएगा, और यह कि स्टैक 100 तत्वों से अधिक लंबा नहीं होगा।

किनारे के मामले

  • yes yes ohप्रिंट करता है 11
  • इंटूप एक स्ट्रिंग या संख्या हो सकती है।
  • कोड में कोई भी वर्ण हो सकता है। मेल खाते हुए लोगों की [a-z ]अनदेखी नहीं की जानी चाहिए।

1
कृपया सुनिश्चित करें कि आपके टेस्टकेस में सभी कमांड शामिल हैं।
लीकी नून

2
किस प्रकार के पूर्णांक का उपयोग किया जाना चाहिए? क्या अतिप्रवाह है? reallyयदि स्टैक के ऊपर मल्टी-कैरेक्टर स्ट्रिंग है तो क्या करें?
मार्टिन एंडर

1
@ प्रोग्रामर 5000 yes yes ohप्रिंट नहीं होना चाहिए 11?
user41805

1
क्या यह ठीक है कि अगर हम मानते हैं कि इनपुट में केवल चरित्र हो सकता है [a-zA-Z ]?
user41805

1
इनपुट को पुश करें क्या इनपुट नंबर हो सकता है? सरणी? वर्ण? तार? यदि हां, तो क्या इनपुट तार मल्टी-लाइन हो सकते हैं? स्टैक में अंतिम आइटम बढ़ाएँ जब अंतिम आइटम नंबर नहीं है तो यह क्या करता है?
लुइस मेंडो

जवाबों:


4

05AB1E , 77 67 63 61 बाइट्स

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

मान लें कि कार्यक्रम स्टैक के शीर्ष पर है।

इसे ऑनलाइन आज़माएं!


क्या आप वास्तव में मान सकते हैं कि सभी इनपुट वैध हैं? उदाहरण के लिए कि आप इस कार्यक्रम को प्राप्त नहीं कर सकते हैंyes no sey yep
Emigna

@Emigna इस बारे में कोई परीक्षण के मामले नहीं हैं, कोई विनिर्देशों नहीं है, और किसी ने एक टिप्पणी छोड़ने के बाद भी ओपी ने यह स्पष्ट नहीं किया है।
ओकेक्स

मैंने परीक्षण-मामलों की कमी पर ध्यान दिया। इस बारे में टिप्पणियों में कुछ भी नहीं देखा, इसलिए मुझे आश्चर्य हुआ कि उनके संदर्भ कार्यक्रम में अमान्य आदेशों की अनदेखी की गई है, जबकि यदि आप वर्ण कोड का योग एक मान्य आदेश से मेल खाते हैं, तो आप उन्हें मान्य आदेशों के रूप में निष्पादित कर सकते हैं।
एमिग्ना

9

जावास्क्रिप्ट (ईएस 6), 218 215 204 203 बाइट्स

प्रोग्राम स्ट्रिंग sऔर इनपुट iको सिंटेक्स में करीने से लेता है (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निर्देश एस को 0 में सूचकांक में बदलने के लिए सही हैश फ़ंक्शन का उपयोग करते हैं ... 8 और जेएस कोड को निष्पादित करने के लिए इस सूचकांक का उपयोग करें:

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()]

पी समारोह बंद ढेर अंतिम आइटम पॉप रों में और यह भार कश्मीर

हम कुछ निर्देशों के परिणाम को परीक्षण द्वारा स्टैक पर वापस धकेलने से रोकते हैं यदि .map () विधि को परिभाषित किया जाता है, अर्थात यदि परिणाम एक सरणी है। ओह के लिए कोड डिजाइन द्वारा एक सरणी लौटा रहा है और हम nope और yep को सरणियाँ वापस करने के लिए मजबूर करते हैं। इसलिए वाक्य रचना:

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

परीक्षण के मामलों


5

Röda , 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

पायथ , 69 बाइट्स

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

इसे ऑनलाइन आज़माएं!


"हैलो, वर्ल्ड" प्रोग्राम पर काम नहीं करता।
ओकेक्स

@ ऑक्स यह काम करता है; मैंने अपने लिंक में कार्यक्रम को बदल दिया; मूल कार्यक्रम बहुत लंबा है।
लीक नून

या उस कार्यक्रम के लिए अन्य कार्यक्रमों का एक समूह। 013456789yes 012345678no ohप्रिंट 0, अपेक्षित आउटपुट 10. है
ओकेक्स

साथ ही एज केस yes yes oh(प्रश्न में निर्दिष्ट) के लिए काम नहीं करता है , अपेक्षित आउटपुट है 11
ओकेक्स

@Okx टिप्पणियों को देखें। ओपी ने इसे विशेष रूप से अपरिभाषित व्यवहार के रूप में निर्दिष्ट किया है।
लीक नून

1

जेएस (ईएस 6), 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

जावास्क्रिप्ट (ईएस 6), 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

पायथन 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को एक चर के रूप में सहेज कर बचा सकते हैं
वंडरक्रिकेट

1

पर्ल 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}

इसे आज़माएं
हैलो वर्ल्ड
कैट
-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

इसे आज़माएं (हैलो वर्ल्ड)

विस्तारित:

{
  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)
हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.