सर्पिल प्रोग्रामिंग


14

एक नामित फ़ंक्शन या प्रोग्राम लिखें जो एकल पूर्णांक N और प्रिंट (STDOUT) या रिटर्न (एक स्ट्रिंग के रूप में) के नीचे सर्पिल की पहली एन सलाखों को स्वीकार करता है, केंद्र में ऊर्ध्वाधर बार और बाहर की ओर सर्पिलिंग के साथ शुरू होता है।

        _______________
       / _____________ \
      / / ___________ \ \
     / / / _________ \ \ \
    / / / / _______ \ \ \ \
   / / / / / _____ \ \ \ \ \
  / / / / / / ___ \ \ \ \ \ \
 / / / / / / / _ \ \ \ \ \ \ \
/ / / / / / / / \ \ \ \ \ \ \ \
| | | | | | | | | | | | | | | |
\ \ \ \ \ \ \___/ / / / / / / /
 \ \ \ \ \ \_____/ / / / / / /
  \ \ \ \ \_______/ / / / / /
   \ \ \ \_________/ / / / /
    \ \ \___________/ / / /
     \ \_____________/ / /
      \_______________/ /

आप मान सकते हैं कि 0 <N <= 278. आपके आउटपुट में सर्पिल के सबसे बाएं वर्ण के सामने कोई व्हाट्सएप नहीं हो सकता है। आप वैकल्पिक रूप से एक एकल अनुगामी न्यूलाइन प्रिंट कर सकते हैं।

10 के इनपुट के लिए, सही आउटपुट है

   _
  / \ 
  | | 
\___/

2 के इनपुट के लिए, सही आउटपुट है

/
|

20 के इनपुट के लिए, सही आउटपुट है

  ___
 / _ \
/ / \ \ 
| | | |
\___/ /

एक आउटपुट जो गलत होगा क्योंकि लेफ्टेस्ट कैरेक्टर व्हाट्सएप से पहले का है

       ___
      / _ \
     / / \ \ 
     | | | |
     \___/ /

विजेता बाइट्स में सबसे छोटा सबमिशन है।


यह एक अजीब सर्पिल है, प्रति क्रांति में 6/8 पक्ष बढ़ते हैं और 2 का आकार 1 रहता है
डेवोन पार्सन्स

1
@DevonParsons सोचो की यह एक हेक्सागोनल सर्पिल, जहां खड़ी सलाखों (है |) केवल निकटतम चरित्र है कि एक की बैठक का प्रतिनिधित्व कर सकते हैं /और \
mbomb007

जवाबों:


5

CJam - 156/147

L{[W1]{:I0'|{IIW*:J'/}X*[0J'_]X2*I+*[J0_]I1={\}*{J_'\}X*0I0}%L*3/{~_{[UV@]a3$+}{;@}?V@+:V;U@+:U;}/}A,1>fX]ri=_z::e<2<f{[\\]z::-}$_W=0=)S50*a*\{~3$3$=\tt}/N*

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

यह 1 से 378 समावेशी इनपुट्स के साथ काम करता है (आवश्यकता से 100 अधिक)

नवीनतम प्रतिबद्ध (hg में सार्वजनिक रूप से उपलब्ध) का उपयोग करते हुए लेकिन इस चुनौती को पोस्ट किए जाने के समय असंबंधित CJam कोड, समाधान को 147 वर्णों तक छोटा किया जा सकता है:

L{[W1]{:I0'|{IIW*:J'/}X*[0J'_]X2*I+*[J0_]I1={\}*{J_'\}X*0I0}%L*3/{~_{[UV@]a3$+}{;@}?V@+:V;U@+:U;}/}A,1>fX]ri=_:.e<2<f.-$_W=0=)S50*a*\{~3$3$=\tt}/N*

स्पष्टीकरण:

कार्यक्रम पुनरावृत्ति से सभी सर्पिलों का निर्माण करता है [xy वर्ण] के सरणियों के साथ शुरू होता है [0 0 '|], अनुरोधित सर्पिल प्राप्त करता है, निर्देशांक को समायोजित करता है ताकि न्यूनतम x और y 0 हों, रिक्त स्थान की एक मैट्रिक्स बनाता है (पंक्तियों की सही संख्या और 50 स्तंभों के साथ) ) फिर सर्पिल से पात्रों को सेट करता है और पंक्तियों को नए सिरे से जोड़ता है।

L                   start with an empty array (spiral no. 0)
{…}A,1>fX           for X in 1..9 (A=10)
                    each X represents a full 360° tour with groups of X /'es and \'es
    [W1]{…}%        transform the array [-1 1] (W=-1) applying the block to each item
                    the block generates a series of triplets dx, dy, character
                    note: dx is down, dy is right; -1 handles ↑↗→↘, 1 handles ↓↙←↖
        :I          store the current item in I
        0'|         add 0 and |, which will form a triplet with the previous I
        {…}X*       repeat X times
            IIW*    add I and -I
            :J'/    also store -I in J, and add /
        [0J'_]      make an array [0 J _]
        X2*I+*      repeat the array X*2+I times
        [J0_]       make an array [J 0 0]
                    (a 0 instead of a character means only changing the position)
        I1={\}*     if I=1, swap the two arrays (the position adjustment is different
                    for the upper and lower horizontal sections)
        {…}X*       repeat X times
            J_'\    add J, J and \
        0I0         add 0, I and 0 (another position adjustment)
    L*              flatten the array (since we added a few inner arrays)
    3/              split into [dx dy char] triplets
    {…}/            for each triplet
        ~_          dump the 3 items on the stack and duplicate the character
        {…}         if the character is not 0
            [UV@]   make an array [U V char] (U and V are initially 0)
                    U represents "x" and V represents "y"
            a3$+    add it as an element to a copy of the previous spiral
        {…}         else
            ;@      pop the character and bring the previous spiral to the top
        ?           end if
        V@+:V;      V+=dy
        U@+:U;      U+=dx
]                   put all the spirals in an array
ri=                 read token, convert to integer and get that spiral
_z::e<              copy the spiral and get a triplet with the minimum values
2<                  keep only the first 2 items (xmin and ymin)
f{…}                for each triplet and the array [xmin ymin]
    [\\]z::-        subtract xmin and ymin from x and y in the triplet
                    (in the latest CJam code this is simply ".-")
$                   sort the spiral (putting the triplets in order by x then y)
_W=0=)              get the maximum (updated) x and increment it
S50*                make a string of 50 spaces
a*                  put it in an array and repeat it xmax+1 times
                    this is the initial matrix of spaces
\                   swap with the spiral
{…}/                for each triplet in the spiral
    ~               dump the 3 items (x y char) on the stack
    3$3$=           copy the matrix and x, and get the x'th row
    \t              swap with the character and put that character in the y'th position
    t               put the modified row in the x'th position in the matrix
N*                  join the matrix rows with newlines

8

पायथन 2, 290 289

यह शायद बहुत बुरा है, लेकिन मैंने कोशिश की: डी

आउटपुट में अनुगामी स्थान होते हैं, लेकिन यह कल्पना में निषिद्ध नहीं है।

अद्यतन: 1 बाइट को बदलने के साथ सहेजा \nगया ;

m=x=y=c=0
l,f=1,[31*[' ']for t in[0]*31]
for i in[0]*input():
 k=m%4;f[14+y+(2<m<6)][14+x-(m>3)],x,y,c='|/_\\'[k],x+(k>0)*(2*(4>m)-1),y+(k!=2)*(2*(2<m<6)-1),c+1
 if(c==l)*(m%2)+(k==0)+(k==2)*(c==2*l-1+m//3):m,c,l=(m+1)%8,0,l+m//7
print'\n'.join(''.join(e[16-l*2:])for e in f if[' ']*31!=e)

ट्रेलिंग स्पेस ठीक हैं। मैंने यह परीक्षण किया। अच्छी नौकरी!
रेनबोल्ट

4

जावास्क्रिप्ट (ईएस 6) 257 288 321

मर्ज किए गए चरणों को संपादित करें । कुछ और चार्ट को काटने के लिए गोल्फ कोड को
संपादित करें

वर्तमान एक्स और वाई स्थिति और वर्तमान दिशा का ट्रैक रखते हुए आर सरणी में पुनरावृत्त रूप से आउटपुट का निर्माण करें। जब x या y स्थिति <0 होती है, तो संपूर्ण r सरणी को पुन: अन्याय किया जाता है।

मुख्य चर:

  • आर परिणाम सरणी या पंक्तियाँ
  • x, y वर्तमान स्थिति।
  • वर्तमान दिशा (0..7) (या वर्तमान स्थिति)
  • d वर्तमान चिन्ह (0..3) -> '| \ _ /'
  • वर्तमान क्रम पर l रननिग स्थिति (नीचे ०)
  • डब्ल्यू वर्तमान सर्पिल त्रिज्या (कम या ज्यादा)
F=n=>
  (w=>{
    for(r=b=[],s=y=x=d=0;n--;
      d&&--l||((s=s+1&7,d=s&3)?l=d-2?w:s/2-2+w+w:w+=!s))
      s>0&s<4?++x:s>4?x?--x:r=r.map(v=>' '+v):b+='  ',
      q=r[s>2&s<6?++y:y]||b,
      r[y]=(q+b).slice(0,x)+'|/_\\'[d]+q.slice(x+1),
      s<2|s>6?y?--y:r=[,...r]:x+=!d*2,x-=!d
  })(1)||r.join('\n')

Ungolfed

F=n=>{
  var r=[], s,x,y,d,w,l, q
  for(l=w=1, s=x=y=d=0; n--;)
  {
    if (s>2 && s<6) ++y; // right side, inc y before drawing

    if (x < 0) // too left, adjust
    {
      r = r.map(v=>' '+v) // shift all to right
      ++x; // move current position to right
    }
    if (y < 0) // too up
    {
      r = [q='',...r] // shift all to bottom
      ++y; // move current position to bottom
    }
    q = r[y] || ''; // current row, if undefined convert to empty string
    r[y] = (q+' '.repeat(x)).slice(0,x) + '|/_\\'[d] + q.slice(x+1); // add current symbol in the x column

    if (s<2 || s>6) --y; // left side, dec y after drawing

    if (s>0 && s<4) // always change x after drawing
      ++x;
    else if (s > 4)
      --x;

    --l; // decrement current run
    if (l == 0) // if 0, need to change direction
    {
      s = (s+1) % 8; // change direction
      d = s % 4; // change symbol
      if (d == 0)
      { 
        // vertical direction, adjust x and if at 0 increase radius
        l = 1 // always 1 vertical step
        if (s == 0)
          ++x, ++w
        else
          --x
      }
      else
      {
        if (d != 2)
        {
          l = w; // diaagonal length is always w
        }
        else if (s == 2)
        {
          l = w+w-1 // top is radius * 2 -1
        }
        else
        {
          l = w+w+1 // bottom is radius * 2 +1
        }
      }
    }
  }    
  return r.join('\n')
}  

टेस्ट में फ़ायरफ़ॉक्स / फ़ायरबग कंसोल (या JSFiddle THX @Rainbolt)

;[1, 2, 10, 20, 155, 278].forEach(x=>console.log(F(x)))

उत्पादन

|

/
|

   _
  / \
  | |
\___/

  ___
 / _ \
/ / \ \
| | | |
\___/ /

      ___________
     / _________ \
    / / _______ \ \
   / / / _____ \ \ \
  / / / / ___ \ \ \ \
 / / / / / _ \ \ \ \ \
/ / / / / / \ \ \ \ \ \
| | | | | | | | | | | |
\ \ \ \ \___/ / / / /
 \ \ \ \_____/ / / /
  \ \ \_______/ / /
   \ \_________/ /
    \___________/

        _______________
       / _____________ \
      / / ___________ \ \
     / / / _________ \ \ \
    / / / / _______ \ \ \ \
   / / / / / _____ \ \ \ \ \
  / / / / / / ___ \ \ \ \ \ \
 / / / / / / / _ \ \ \ \ \ \ \
/ / / / / / / / \ \ \ \ \ \ \ \
| | | | | | | | | | | | | | | |
\ \ \ \ \ \ \___/ / / / / / / /
 \ \ \ \ \ \_____/ / / / / / /
  \ \ \ \ \_______/ / / / / /
   \ \ \ \_________/ / / / /
    \ \ \___________/ / / /
     \ \_____________/ / /
      \_______________/ /

मैंने इसका परीक्षण किया और यह काम करता है। यहाँ एक jsfiddle है जो आपके उत्तर को काम करता हुआ दिखाता है। इसे अपने उत्तर में शामिल करने के लिए स्वतंत्र महसूस करें।
रेनबोल्ट

2

Pyth, 166 165

मैंने अपने पायथन जवाब का अनुवाद पायथ के साथ किया, अपने नॉट-ग्रेट पायथ कौशल के साथ। परिणामस्वरूप उल्टी नीचे है।

Jm*31]d*31dK0=G0=H0=Y1VQ X@J++14H&<2K<K6+14-G<3K@"|/_\\"%K4~G*<0%K4-*2>4K1~H*n2%K4-*2&<2K>6K1~Z1I||&qZY%K2!%K4&q2%K4qZ+-*2Y1/K3~Y/K7=K%+1K8=Z0;jbmj>d-16*2Ykfn*31]dTJ
हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.