Cụm từ bánh cuộn


17

Bánh cuộn là loại kẹo ngon được làm bằng một lớp bánh xốp, một lớp kem (hoặc kem), cuộn như hình xoắn ốc, xem Wikipedia .

Nhiệm vụ của bạn là lấy một chuỗi cụm từ không trống từ đầu vào tiêu chuẩn, tách các khoảng trắng ra và cuộn nó như một chiếc bánh cuộn bắt đầu từ trung tâm từ phải sang trái theo hướng ngược chiều kim đồng hồ, trong khi các khoảng trắng bắt đầu ở dưới cùng của đầu tiên cụm từ ký tự, từ trung tâm trái sang phải, như trong ví dụ sau.

Xin lưu ý rằng cụm từ sẽ là lớp bánh xốp và lớp kem sẽ được thể hiện dưới dạng khoảng trống.

Input: Lorem ipsum dolor sit amet, consectetu adipiscing elit, sed do eiusmod

Output with spaces ' ' represented as _

ilegnicsip
t________i
,_srolod_d
s_i____m_a
e_t_oL_u_u
d_a_r__s_t
d_m_emip_e
o_e______t
e_t,consec
i____
usmod

Các trường hợp thử nghiệm

Input: Lorem ipsum dolor sit amet, consectetu adipiscing elit, sed do eiusmod

Output:

ilegnicsip
t        i
, srolod d
s i    m a
e t oL u u
d a r  s t
d m emip e
o e      t
e t,consec
i    
usmod

Input: Sandbox for Proposed Challenges

Output:

soporp
e    r
d aS o
c n  f
h dbox
a      
llenges

Input: Cake

Output:

aC 
k  
e

Input: it

Output:

ti

Quy tắc:



Câu trả lời:


2

Python 2, 202 200 byte

-2 byte nhờ LevitatingLion

c=[];s=n=0
a=input().replace(' ','')
#Calculate the ammount of rotations
l=len(a)
while s<l:s=n*(1+n)/2;n+=1
#Add trailing spaces to not break the rolling
a+=' '*(s-l)
#Add a cream layer on top and a cake layer on bottom of the cake and roll 90 degrees
for i in range(1,n):c=[' '*i]+zip(*c[::-1])+[a[:i]];a=a[i:]
#Finish the rolling to match the pattern
exec~i%4*'c=zip(*c[::-1]);'
#Serve the cake
for i in c:print''.join(i)

Bạn có thể thả dấu ngoặc quanh exec: exec~i%4*'c=zip(*c[::-1]);'. Điều này tiết kiệm hai byte
LevitatingLion

2

Befunge, 260 250 byte

"'"::00p09p19p35*::10p29pv
 v`*84:~<0p041p031p020p93<
v_0`!#v_^*!p04+g04:p03%4+g03:p02%4+g02:!%g04:+1pp01+%2-1g03p\3+<
>00g:: :09g\`8+0\p::19g`8+1\p20g1-2%+00p10g:::29g\`8+2\p::39g`8^
9g\-\v>29g:39g\->\:50p09g:1
$$1+\:!#@_1-55+,^>
,gg05<:+1\-1_:$#\^#

Hãy thử trực tuyến!

Ý tưởng cơ bản là chúng tôi xây dựng hình xoắn ốc ở nửa dưới của sân chơi, tiếp tục theo dõi các tọa độ tối thiểu và tối đa khi chúng tôi đi cùng. Khi tất cả các đầu vào đã được xử lý, chúng tôi lặp lại phạm vi tọa độ và viết ra nội dung đã được lưu.

Giải thích chi tiết

Lưu ý rằng các giá trị dxdy nằm trong phạm vi 0 đến 3, tương ứng với các độ lệch tương đối -1, 0, 1, 0. Do đó, việc cập nhật hướng được thực hiện bằng cách thêm 1, modulo 4. Chuyển đổi sang phần bù tương đối được thực hiện bằng cách trừ 1, modulo 2.

"'"::00p09p19p      Set x, minx, and maxx to 39.
35*::10p29p39p      Set y, miny, and maxy to 15.
020p130p            Set dx to 0 (relative offset -1) and dy to 1 (relative offset 0).
140p                Set the segment length to 1.
0                   Set the index to 0 (on the stack).

                    -- The main input loop start here --
~                   Read a character from stdin.
:48*`_              If it's greater than 32 (i.e. not whitespace or EOL)...
  00g:              Get the current value of x and make a duplicate.
  ::09g\`8+0\p      If it's less than minx, update minx.
  ::19g`8+1\p       If it's greater than maxx, update maxx.
  20g1-2%           Calculate (dx-1)%2, which converts dx into the range -1 to 1.
  +00p              Add that to x and save (leaving the original x on the stack).
  10g:              Get the currrent value of y and make a duplicate.  
  ::29g\`8+2\p      If it's less than miny, update miny.
  ::39g`8+3\p       If it's greater than maxy, update maxy.
  30g1-2%           Calculate (dy-1)%2, which converts dy into the range -1 to 1.
  +10p              Add that to y and save (leaving the original y on the stack).
  p                 Store the character we read at the original x,y coordinates.
  1+                Increment the index by 1.
  :40g%!            Mod that with the current segment len; if zero it's time to turn.
  :20g+4%20p        Add the turn condition to the dx value, thus turning if needed.
  :30g+4%30p        Similarly add the turn condition to the dy value.
  :40g+40p          Also add it to the segment length, so it gets longer every turn.
  !*                Multiply the index by !turn, so it's reset to zero on every turn.
  ^                 Repeat the loop, reading another character.
0`!#v_^             If the char <= 32, we test for EOF and repeat the loop if not.

>                   If it is EOF, we start the output routine.
29g                 Initialise the y coordinate with miny.
:39g\-              Calculate maxy - miny to use as the height countdown.

                    -- The outer output loop starts here --
\:50p               Swap y to the top and save in a temporary variable.
09g                 Initialise the x coordinate with minx.
:19g\-              Calculate maxx - miny to use as our width countdown.
\                   Swap x to the top of the stack.

                    -- The inner output loop starts here --
  :50g              Take a copy of x and the y save in the temporary variable.
  g,                Read the character at those coordinate and write to stdout.
  \:_               Swap the width countdown to the top of the stack and break if zero.
  1-                Otherwise decrement the width countdown.
  \1+               Swap the x value back to the top and increment it.
  <                 Repeat the inner loop.

$$                  Clear the width countdown and x value from the stack.
1+                  Increment the y value.
\                   Swap the height countdown to the top of the stack.
:¬#@_               If it's zero, exit.
1-                  Otherwise decrement it.
55+,                Output a line break.
^                   Repeat the outer loop.
Khi sử dụng trang web của chúng tôi, bạn xác nhận rằng bạn đã đọc và hiểu Chính sách cookieChính sách bảo mật của chúng tôi.
Licensed under cc by-sa 3.0 with attribution required.