সর্পিল প্রোগ্রামিং


14

একটি নামকৃত ফাংশন বা প্রোগ্রাম লিখুন যা একটি একক পূর্ণসংখ্যা N এবং প্রিন্ট করে (STDOUT তে) বা নীচে সর্পিলের প্রথম এন বারগুলি প্রেরণ করে (স্ট্রিং হিসাবে) কেন্দ্রের উল্লম্ব বার দিয়ে শুরু করে এবং ঘড়ির কাঁটার দিকে ঘোরান।

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

আপনি ধরে নিতে পারেন 0 <এন <= 278। আপনি allyচ্ছিকভাবে একটি একক পেছনের নতুন লাইন মুদ্রণ করতে পারেন।

10 ইনপুট জন্য, সঠিক আউটপুট হয়

   _
  / \ 
  | | 
\___/

2 ইনপুট জন্য, সঠিক আউটপুট হয়

/
|

20 এর ইনপুটের জন্য, সঠিক আউটপুট

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

একটি আউটপুট যা ভুল হতে পারে কারণ বামতম অক্ষরটি সাদা স্থানের আগে হয়

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

বিজয়ী হ'ল সংক্ষিপ্ততম জমা, বাইটে।


এটি একটি অদ্ভুত সর্পিল, বিপ্লব প্রতি 6/8 পক্ষগুলি আরও বড় হয় এবং 2 টি আকার
ডিভন পার্সসন

1
@ ডেভনপারসনস এর ভাবুন এর একটি ষড়ভুজ সর্পিল রয়েছে, যেখানে উল্লম্ব বারগুলি ( |) কেবলমাত্র নিকটতম চরিত্র যা একটি /এবং এর সভার প্রতিনিধিত্ব করতে পারে \
mbomb007

উত্তর:


5

সিজেম - 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 বেশি)

সর্বশেষ প্রতিশ্রুতিবদ্ধ (এইচজি তে সর্বজনীনভাবে উপলভ্য) ব্যবহার না করে তবে এই চ্যালেঞ্জটি পোস্ট হওয়ার সময় অপ্রকাশিত সিজেএম কোডটি সমাধান 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

এটি সম্ভবত সত্যই খারাপ, কিন্তু আমি চেষ্টা করেছি: ডি

আউটপুটটিতে পেছনের স্থান রয়েছে তবে এটি অনুমান করা যায় না।

আপডেট: পরিবর্তন \nকরে 1 বাইট সংরক্ষণ করা হয়েছে ;

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

জাভাস্ক্রিপ্ট (ES6) 257 288 321

পদক্ষেপগুলি একত্রিত করা হয়েছে Edit আরও কিছু চর কাটাতে গল্ফযুক্ত কোড ফিডলিং
সম্পাদনা করুন

বর্তমান এক্স এবং ওয়াই অবস্থান এবং বর্তমান দিকের ট্র্যাক রেখে আউটপুটটি পুনরায় পুনরায় অ্যারেতে তৈরি করুন। যখন x বা y অবস্থানটি <0 হয় তখন পুরো আর অ্যারেটি সামঞ্জস্য হয়।

প্রধান পরিবর্তনশীল:

  • r ফলাফল অ্যারে বা সারি
  • এক্স, ওয়াই বর্তমান অবস্থান।
  • এর বর্তমান দিকনির্দেশ (0..7) (বা বর্তমান অবস্থা)
  • (0..3) -> '| \ _ /' আঁকতে d বর্তমান প্রতীক
  • আমি বর্তমান ক্রম উপর রাননিগ অবস্থান (নীচে 0 থেকে)
  • ডাব্লু বর্তমান সর্পিল ব্যাসার্ধ (কম বা কম)
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 ধন্যবা @Rainbolt)

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

আউটপুট

|

/
|

   _
  / \
  | |
\___/

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

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

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

আমি এটি পরীক্ষা করেছি এবং এটি কাজ করে। এখানে একটি জাসফিডাল যা আপনার উত্তরকে কাজ করে দেখায়। আপনার উত্তরের সাথে এটি নিখরচায় অন্তর্ভুক্ত করুন।
রেইনবোল্ট

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
আমাদের সাইট ব্যবহার করে, আপনি স্বীকার করেছেন যে আপনি আমাদের কুকি নীতি এবং গোপনীয়তা নীতিটি পড়েছেন এবং বুঝতে পেরেছেন ।
Licensed under cc by-sa 3.0 with attribution required.