把绳子变成风车


14

该站点上的代码正在迅速耗尽。我们需要投资可再生能源。因此,您必须编写一个使用字符串并将其转换为风车的程序。

挑战

让我们以一个简单的风车弦为例。拿弦abc。该枢纽为中心的性格,在这种情况下b。由于字符串的长度为3个字符,因此每个输出将恰好是三行高和三个字符宽。这是步骤1的输出。(请注意空格)

abc

要进行下一步,请顺时针旋转每个字符。这是步骤2:

一种
 b
  C

这是步骤3-8:

 一种
 b
 C
  一种
 b
C
cba

C
 b
  一种
 C
 b
 一种
  C
 b
一种

在第九步中,它绕到原始字符串整圈:

abc

注意b整个时间都停留在同一地点。这是因为b是关键人物。您必须编写一个将字符串作为输入的程序或函数,并重复打印此序列,直到关闭该程序。

澄清说明

  • 所有输入字符串将具有奇数个字符。(以便每个风车都有一个枢轴)

  • 为了简化挑战,所有字符串将仅包含大写和小写字母字符。

  • 输出必须是len(input_string)宽而高的字符。

  • 只要您继续旋转并永远循环,就从序列的哪一步开始无关紧要。

更多测试IO:

由于帖子已经很长了,因此以下是“ windmill”输出的链接

边注:

由于这应该是风车,所以如果您包含一些样板代码以使其具有较小的时间延迟或在每个步骤之间由用户输入动画效果,那就太好了。但是,由于某些语言没有内置时间,因此这不是强制性的。您提交内容的竞争部分可以尽快打印序列。


Answers:


7

MATL35 33 21字节

jtn2/kYaG1$Xd`wtD3X!T

以下将为风车设置动画(26个字节

jtn2/kYaG1$Xd`wtXxDlY.3X!T

在线演示

在此版本中,Xx指定清除显示内容,并且1Y.暂停1秒。

说明

基本思想是我们要创建输入的两个版本。“正交”版本

+-----+
|     |       
|     |
|abcde|
|     |
|     |
+-----+

和“对角线”版本

+-----+
|a    |
| b   |
|  c  |
|   d |
|    e|
+-----+

我们将这两个版本推入堆栈。每次循环时,我们都会切换堆栈顺序,并顺时针旋转顶部的堆栈。

j       % Grab the input as a string
t       % Duplicate the input

%--- Create the "orthogonal" version ---%

n2/     % Determine numel(input) / 2
k       % Round down to nearest integer
Ya      % Pad the input string with floor(numel(input)/2) rows above and below 

%--- Create the "diagonal" version ---%

G       % Grab the input again
1$Xd    % Place the input along the diagonal of a matrix    

`       % do...while loop   
  w     % Flip the order of the first two elements on the stack
  t     % Duplicate the top of the stack

  %--- OCTAVE ONLY (converts NULL to space chars) ---%

  O       % Create a scalar zero
  32      % Number literal (ASCII code for ' ')
  XE      % Replaces 0 elements in our 3D array with 32 (' ')

  %--- END OCTAVE ONLY ---%

  D     % Display the element     
  3X!   % Rotate this element 90 degrees counter-clockwise 3 times (clockwise)
  T     % Explicit TRUE to create an infinite loop
        % Implicit end of while loop

8

JavaScript(ES6),291字节

r=a=>{w.textContent=a.map(a=>a.join``).join`
`;for(i=j=h=a.length>>1;j++,i--;){t=a[i][i];a[i][i]=a[h][i];a[h][i]=a[j][i];a[j][i]=a[j][h];a[j][h]=a[j][j];a[j][j]=a[h][j];a[h][j]=a[i][j];a[i][j]=a[i][h];a[i][h]=t}}
s=w=>{a=[...w=[...w]].map(_=>w.map(_=>' '));a[w.length>>1]=w;setInterval(r,1000,a)}
s("windmills")
<pre id=w>


您不能通过减少举重时间来打几个字节吗?
市长蒙蒂2016年


5

红宝石,122个 119字节

->n{c=0
loop{i=[l=n.size,m=l+1,m+1,1][3-c=c+1&7]*(3.5<=>c)
s=(' '*l+$/)*l
l.times{|j|s[m*l/2-1+(j-l/2)*i]=n[j]}
$><<s}}

测试程序中带睡眠的非高尔夫版本

在整个控制台高度旋转并不是很令人信服。但是,如果将高度降低到输入字符串的长度,则旋转更具说服力。

f=->n{
  c=0                                     #loop counter
  m=1+l=n.size                            #l=string length. m=l+1
  loop{                                   #start infinite loop
    s=(' '*l+$/)*l                        #make a string of l newline-terminated lines of l spaces. Total m characters per line.              
    i=[m-1,m,m+1,1][3-c=c+1&7]*(3.5<=>c)  #array contains positive distance between characters in string 1=horizontal, m=vertical, etc.
                                          #c=c+1&7 cycles through 0..7. Array index 3..-4 (negative indices count from end of array, so 3=-1, 0=-4 etc)
                                          #(3.5<=>c) = 1 or -1. We use to flip the sign. This is shorter than simply using 8 element array [m-1,m,m+1,1,1-m,-m,-m+1,-1]  
    l.times{|j|s[m*l/2-1+i*(j-l/2)]=n[j]} #for each character in n, write the appropriate space character in s to the character in n
    puts s                                #print s
    sleep 1                               #sleep for 1 second
  }
}

f[gets.chomp]                             #get input, remove newline, call function


3

Python 3,193字节

def c(a):e =''; s = len(a); l = int(s / 2); b = range(s); m ='\ n'* l; print(m,a,m );对于b:print(e * x,a [x])中的x;对于b:print(e * l,a [x])中的x;对于b:print(e *(s-1)中的x x),a [x]); a = input();而True:c(a); c(a [::-1]);

不打高尔夫球

定义c(a):
    e =''; s = len(a); l = int(s / 2); b = range(s); m ='\ n'* l;
    打印(m,a,m);
    对于b:print(e * x,a [x])中的x;
    对于b:print(e * l,a [x])中的x;
    对于b:print(e *(s-1-x),a [x])中的x; 
a = input();
而True:
    c(a);
    c(a [::-1]);

递归,177字节

(几秒钟后崩溃)

def c(a):e =''; s = len(a); l = int(s / 2); b = range(s); m ='\ n'* l; print(m,a,m );对于b:print(e * x,a [x])中的x;对于b:print(e * l,a [x])中的x;对于b:print(e *(s-1)中的x x),a [x]); c(a [::-1]); c(input());

不打高尔夫球

定义c(a):
    e =''; s = len(a); l = int(s / 2); b = range(s); m ='\ n'* l;
    打印(m,a,m);
    对于b:print(e * x,a [x])中的x;
    对于b:print(e * l,a [x])中的x;
    对于b:print(e *(s-1-x),a [x])中的x;
    c(a [::-1])
c(输入());

另一种解决方案,268字节

导入itertools为i; def w(a):e =''; s = len(a); l = int(s / 2); t ='\ n'; m =(l-1)* t; h = list(i.chain.from_iterable((e * x + a [x],e * l + a [x],e *(s-1-x)+ a [x])对于范围内的x )); print(m,a,m,t.join(h [:: 3]),t.join(h [1 :: 3]),t.join(h [2 :: 3]),sep = t,end =''); a = input();而True:w(a); w(a [::-1]);

不打高尔夫球

导入itertools作为i;
def w(a):
    e =''; s = len(a); l = int(s / 2); t ='\ n'; m =(l-1)* t;
    h = list(i.chain.from_iterable((e * x + a [x],e * l + a [x],e *(s-1-x)+ a [x])对于范围内的x )))
    print(m,a,m,t.join(h [:: 3]),t.join(h [1 :: 3]),t.join(h [2 :: 3]),sep = t, end ='');
a = input();
而True:
    w(a);
    w(a [::-1]);

我可以借这个吗?
Leaky Nun

顺便说一句,欢迎来到PPCG
Leaky Nun

另外,您忘记了在末尾反转字符串(第一级缩进)。
Leaky Nun

此输出无效。步骤1和5缺少前导空白。
詹姆斯

变了!@MyHamDJ
p1714825 '16

2

Pyth,48个字节

JlzK/J2#*btKz*btKM+*dG@zHVJgNN)VJgKN)VJg-JNN)=_z

在线尝试!(注意:这是一个不会永远循环的版本,因为它会使解释器崩溃。)

从Python 3解决方案的无耻翻译中 @ByHH

怎么运行的:

JlzK/J2#*btKz*btKM+*dG@zHVJgNN)VJgKN)VJg-JNN)=_z
                                                 assign('z',input())
Jlz                                              assign("J",Plen(z))
   K/J2                                          assign("K",div(J,2))
       #                                         loop-until-error:
        *btK                                      imp_print(times(b,tail(K)))
            z                                     imp_print(z)
             *btK                                 imp_print(times(b,tail(K)))
                                                  @memoized
                 M                                def gte(G,H):
                  +*dG@zH                          return plus(times(d,G),lookup(z,H))
                         VJ   )                   for N in num_to_range(J):
                           gNN                     imp_print(gte(N,N))
                               VJ   )             for N in num_to_range(J):
                                 gKN               imp_print(gte(K,N))
                                     VJ     )     for N in num_to_range(J):
                                       g-JNN       imp_print(gte(minus(J,N),N))
                                             =_z  assign('z',neg(z))

此输出无效。步骤1和5缺少前导空白。
詹姆斯

现在可以吗???
Leaky Nun

是的,看起来很棒。;)
詹姆斯
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.