旋转螺旋


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

这是代码高尔夫,分数照常以字节为单位。

Answers:


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.
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.