เกลียวหมุน


12

รับสี่เหลี่ยมของข้อความที่แสดงถึงเกลียวของตัวละครหมุนมัน!

เกลียวเริ่มต้นที่ศูนย์และย้ายทวนเข็มนาฬิกาไปที่ขอบด้านนอกเริ่มต้นที่ด้านซ้ายของศูนย์:

987
216
345

123456789นี้แปลเป็นสตริง การหมุนจะทำไปทางซ้าย234567891ดังนั้นหากคุณหมุนตำแหน่งหนึ่งก็จะเป็น นี่คือตัวแทน:

198
327
456

อินพุต

อินพุตประกอบด้วยเกลียวและระยะทางในการหมุน

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

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

สมมติว่าตัวละครทั้งหมดเป็นตัวอักษรและตัวเลข [A-Za-z0-9]

เอาท์พุต

เอาท์พุทเป็นเกลียวหมุน ควรเป็นรูปสี่เหลี่ยมจัตุรัสในหลายบรรทัด (ไม่ว่าจะพิมพ์หรือส่งคืน)

ตัวอย่าง

อินพุต

3
tne
atd
bin

เอาท์พุต

bat
nit
den

อินพุต

18
efilr
naepo
umshf
tootr
butte

เอาท์พุต

rettu
omseb
oofft
trliu
hpean

นี่คือโค้ดกอล์ฟที่มีคะแนนนับเป็นปกติ

คำตอบ:


6

CJam, 45 44 ไบต์

]]l~LqN/{(W%@+\zW%}h;m<{1$,/(W%a@W%z+\s}h;N*

ทดสอบที่นี่

คำอธิบาย

วิธีแก้ปัญหาขี้เกียจ: แกะเกลียวออกมาใช้การหมุนอาเรย์ในตัวของ CJam แล้วหมุนเกลียวขึ้นอีกครั้ง

]]       e# Push [""]. We'll later use this to roll up the spiral.
l~       e# Read the integer and evaluate it.
L        e# Push an empty string: we'll unroll the input into this.
qN/      e# Read the spiral and split it into lines.
{        e# While the top of the stack is truthy...
  (W%    e#   Pull the first line off the spiral and reverse it.
  @+     e#   Pull up the string we've built so far and prepend the reversed line.
  \zW%   e#   Swap with the remaining spiral, and rotate the spiral.
}h       e# This terminates when the centre character has been added to the string and
         e# the spiral becomes an empty array.
;        e# Discard the empty array.
         e# Note that we've unrolled the spiral from outside in, but we've also built up
         e# the string in reverse, which gives us the string from inside out.
m<       e# Rotate to the left by the given number of characters.
{        e# While the top of the stack is truthy...
  1$,    e#   Copy the spiral so far and get the number of lines.
  /      e#   Split the string into chunks of that size.
  (W%a   e#   Pull off the first chunk, reverse it and wrap it in an array.
  @zW%   e#   Pull up the spiral so far, rotate it.
  +      e#   Prepend the chunk to the spiral as a line.
  \s     e#   Swap with the other chunks and flatten them into a string again.
}h       e# This terminates when the string has been used up completely.
;        e# Discard the empty string.
N*       e# Join the lines with linefeed characters.
โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.