方形,钻石,方形,钻石


41

任务

给定一个非空的小写ASCII字母字符串a- z,请使用第一个字符,并:

  • 第二个字符的副本方块围住它,
  • 第三个角色的副本菱形包围它,
  • 第四个字符的副本方块围住…

…在正方形和菱形边框之间交替,直到字符串结尾。边框之间的空白应由ASCII空格()表示。

要添加正方形边框,请在整个当前“工作数组”周围精确绘制一个正方形:

                                               sssssssssss
                                t              s    t    s
                               t t             s   t t   s
                              t   t            s  t   t  s
                             t aaa t           s t aaa t s
                            t  aca  t    =>    st  aca  ts
                             t aaa t           s t aaa t s
                              t   t            s  t   t  s
                               t t             s   t t   s
                                t              s    t    s
                                               sssssssssss

要添加菱形边框,请绘制居中的菱形形状,该形状以对角线而不是正交的方式接触最外面的正方形:

                                                     s            
                                                    s s           
                                                   s   s          
                                                  s     s         
                                                 s       s        
                                                s         s       
                                               s           s      
                    wwwwwwwwwww               s wwwwwwwwwww s     
                    w    o    w              s  w    o    w  s    
                    w   o o   w             s   w   o o   w   s   
                    w  o   o  w            s    w  o   o  w    s  
                    w o eee o w           s     w o eee o w     s 
                    wo  eme  ow    =>    s      wo  eme  ow      s
                    w o eee o w           s     w o eee o w     s 
                    w  o   o  w            s    w  o   o  w    s  
                    w   o o   w             s   w   o o   w   s   
                    w    o    w              s  w    o    w  s    
                    wwwwwwwwwww               s wwwwwwwwwww s     
                                               s           s      
                                                s         s       
                                                 s       s        
                                                  s     s         
                                                   s   s          
                                                    s s           
                                                     s            

您的程序必须输出最终数组。

  • 每行可以包含任意数量的尾随空格。
  • 您可以输出代表行的字符串列表,也可以输出一个单独的以换行符分隔的字符串,并带有可选的尾随换行符。
  • 前导/尾随的空白行是不允许的。
  • 也不允许前导空格。

这是。以字节为单位的最短代码获胜。

测试用例

上述实施例是预期的输出,用于catcatsmeow,和meows,在读取顺序。其他一些值得处理的情况:

  • 对于输入a,您的程序应输出:

    a
    
  • 对于输入ab,您的程序应输出:

    bbb
    bab
    bbb
    
  • 对于输入codegolf,您的程序应输出:

    fffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
    f                            l                            f
    f                           l l                           f
    f                          l   l                          f
    f                         l     l                         f
    f                        l       l                        f
    f                       l         l                       f
    f                      l           l                      f
    f                     l             l                     f
    f                    l               l                    f
    f                   l                 l                   f
    f                  l                   l                  f
    f                 l                     l                 f
    f                l                       l                f
    f               l                         l               f
    f              l                           l              f
    f             l ooooooooooooooooooooooooooo l             f
    f            l  o            g            o  l            f
    f           l   o           g g           o   l           f
    f          l    o          g   g          o    l          f
    f         l     o         g     g         o     l         f
    f        l      o        g       g        o      l        f
    f       l       o       g         g       o       l       f
    f      l        o      g           g      o        l      f
    f     l         o     g eeeeeeeeeee g     o         l     f
    f    l          o    g  e    d    e  g    o          l    f
    f   l           o   g   e   d d   e   g   o           l   f
    f  l            o  g    e  d   d  e    g  o            l  f
    f l             o g     e d ooo d e     g o             l f
    fl              og      ed  oco  de      go              lf
    f l             o g     e d ooo d e     g o             l f
    f  l            o  g    e  d   d  e    g  o            l  f
    f   l           o   g   e   d d   e   g   o           l   f
    f    l          o    g  e    d    e  g    o          l    f
    f     l         o     g eeeeeeeeeee g     o         l     f
    f      l        o      g           g      o        l      f
    f       l       o       g         g       o       l       f
    f        l      o        g       g        o      l        f
    f         l     o         g     g         o     l         f
    f          l    o          g   g          o    l          f
    f           l   o           g g           o   l           f
    f            l  o            g            o  l            f
    f             l ooooooooooooooooooooooooooo l             f
    f              l                           l              f
    f               l                         l               f
    f                l                       l                f
    f                 l                     l                 f
    f                  l                   l                  f
    f                   l                 l                   f
    f                    l               l                    f
    f                     l             l                     f
    f                      l           l                      f
    f                       l         l                       f
    f                        l       l                        f
    f                         l     l                         f
    f                          l   l                          f
    f                           l l                           f
    f                            l                            f
    fffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
    

参考实施

我写了一些Julia代码,程序的输出应该匹配(至少在视觉上)。在线尝试!

Answers:


5

MATL,63 54字节

l&)"X@o?TT@I$Ya}t&n2/Xkw2+-|thYa9Mqt_w&:|&+tleXM=@*+]c

在线尝试!

说明

            % Implicitly grab the input as a string
l&)         % Separate the first letter from the rest and push them onto the stack
"           % For every letter in the string besides the first
  X@o?      % If this is an odd iteration through the loop
    TT      % Push the array [1, 1]
    @       % Get current character
    I$Ya    % Pad the matrix with 1 copy of this character on all sides
  }         % Else (this is a diamond)
    t       % Duplicate the matrix
    &n      % Determine the number of rows and columns (push separately to stack)
    2/Xk    % Compute the radius (half the # of columns rounded up)
    w2+     % Flip the stack to get the # of rows and add 2. This is the new radius.
    -|      % Subtract the new and old radii and take the absolute value
    thYa    % Apply the necessary number of padding zeros to the matrix
    9Mqt_w&:% Generate the array [-radius ... 0 ... radius]
    |&+     % Take the absolute value and compute the element-wise sum with itself
            % and automatically broadcast to a matrix. For a radius of 5 this yields
            % the following:
            %
            %   8   7   6   5   4   5   6   7   8
            %   7   6   5   4   3   4   5   6   7
            %   6   5   4   3   2   3   4   5   6
            %   5   4   3   2   1   2   3   4   5
            %   4   3   2   1   0   1   2   3   4
            %   5   4   3   2   1   2   3   4   5
            %   6   5   4   3   2   3   4   5   6
            %   7   6   5   4   3   4   5   6   7
            %   8   7   6   5   4   5   6   7   8
            % 
    tleXM   % Duplicate and compute the mode of this matrix (4 in the example above)
    =@*     % Find the locations where the matrix is equal to the mode and
            % replace with the current character. Set all else to 0.
    +       % Add this to the padded version of the previous iteration. 
 ]          % End of if...else
 c          % Cast the current matrix as a character array (zeros are spaces)
            % Implicit end of for loop and implicit display 

17

的JavaScript(ES6),294 288 287 284 256 246 240 238 220字节

我在这个网站上的第一个高尔夫。将输入字符串传递给函数f,例如f('codegolf')

f=s=>{N=n=>(2<<n/2+.5)-3-n%2
D=N(l=s.length)
for(a=[],i=S=D*2+1;i--;)a[i]=a[S+~i]=Array(S).fill` `
for(;l--;)for(o=n=N(l+1),I=0;I<=n;I++,o-=~l&1)a[y=D+I][x=D+o]=a[x][y]=a[y][D-o]=a[x][D-I]=s[l]
return a.map(r=>r.join``)}

-6个字节,感谢@Sunny Pun:传递模板字符串时,请删除括号。

@ETHproductions -2个字节:更改S-i-1S+~i,更改l%2||o--o-=~l&1

@Sunny Pun,@ edc65的每个建议为-18个字节:删除prompt()console.log()支持函数格式,请勿.join()最终确定字符串数组


2
我数了256个字节。您是否有可能将换行符计数为每个(\r\n)两个字节,而不是一个(\n)?
Mego

2
使其成为函数应通过删除prompt和来节省字节console.log。您也可以使用模板字符串,这样可以在每个函数调用上为字符串节省2个字节(如fill(' ')变为fill` `
Sunny Pun

4
欢迎来到PPCG!用什么其他人说一起去,可以缩短S-i-1S+~i,并i%2||o--o-=~i&1
ETHproductions 2016年

@Mego重新检查计数,正确为256。谢谢!
darrylyeo

我认为a=Array(D*2+1).fill .map((_,$,a)=>[...a])可以为您节省10个字节。
尼尔

11

Haskell中,138个 137 136字节

q 1=0
q x=(mod x 2+1)*(q(x-1)+1)
f x|m<-q$length x,n<-abs<$>[-m..m]=[do j<-n;max" "[c|(k,c)<-zip[1..]x,cycle[max i j,i+j]!!k==q k]|i<-n]

定义一个f将结果作为行列表返回的函数

-1字节感谢@Lynn


8

C ++- 373366362359字节

@TuukkaX -1,@ Kevin Cruijssen -6,@-4进口,-3 @ Baum mit Augen进口

#import<string>
#import<vector>
#define A(x) abs(x-r)
using S=std::string;using V=std::vector<S>;int R(int n){return n?(n%2)?R(n-1)+1:2*R(n-1)+2:0;}V f(S w){int l=w.size(),r=R(l-1),s=r*2+1,i,j,k;V x(s);for(i=0;i<s;++i){x[i]=S(s,46);for(j=0;j<s;++j)for(k=0;k<l;++k){if(A(i)<=R(k)&A(j)<=R(k)&k%2?A(i)==R(k)|A(j)==R(k):(A(i)+A(j)==R(k)))x[i][j]=w[k];}}return x;}

http://ideone.com/5y54mx

注意:此解决方案将点('。')用作“空格”字符,以使空白更加明显。要使用空格,必须将更S(s,46)改为S(s,32)。分数保持不变。

取消高尔夫:

#import<string>
#import<vector>

#define A(x) abs(x-r) //r will be the center coordinate

using S=std::string;
using V=std::vector<S>;

int R(int n){ //recursive function returning the radius of a square/diamond
    return n?  //if(n!=0)
        (n%2)? //if(n%2 != 0)
            R(n-1)+1 //square -> just increase radius by 1
        :
            2*R(n-1)+2 //diamond -> radius doubles + 2
    :
        0 //n==0 -> end of recursion
    ;
}

V f(S w){ //function taking a string and returning a vector<string>
    int l=w.size(),r=R(l-1),s=r*2+1,i,j,k;  //r is radius (==center coord), s is size
    V x(s);  //vector<string>
    for(i=0;i<s;++i){  //loop over vector
        x[i]=S(s,46); //initialize string with dots. use 32 for spaces
        for(j=0;j<s;++j)
            for(k=0;k<l;++k){ //loop over word
                if(
                    A(i)<=R(k) //check bounds
                    &
                    A(j)<=R(k)
                    &
                    k%2?A(i)==R(k)|A(j)==R(k):(A(i)+A(j)==R(k))
                    //for square: check if distance to center of one coordinate = R
                    //for diamond: check if sum of distances = R
                )
                    x[i][j]=w[k]; //if any is true, replace default char
            }
    }

    return x;
}

在线尝试取消高尔夫

如何使用:

include<iostream>

void printSquare(std::string word){
    auto result = f(word);
    for(auto& s:result)
        std::cout << s << std::endl;
    std::cout << std::endl << std::endl;
}

int main() {
    printSquare("a");
    printSquare("ab");
    printSquare("cat");
    printSquare("cats");
    printSquare("meow");
    printSquare("meows");
    printSquare("codegolf");
    return 0;
}

输出:

a


bbb
bab
bbb


....t....
...t.t...
..t...t..
.t.aaa.t.
t..aca..t
.t.aaa.t.
..t...t..
...t.t...
....t....


sssssssssss
s....t....s
s...t.t...s
s..t...t..s
s.t.aaa.t.s
st..aca..ts
s.t.aaa.t.s
s..t...t..s
s...t.t...s
s....t....s
sssssssssss


wwwwwwwwwww
w....o....w
w...o.o...w
w..o...o..w
w.o.eee.o.w
wo..eme..ow
w.o.eee.o.w
w..o...o..w
w...o.o...w
w....o....w
wwwwwwwwwww


............s............
...........s.s...........
..........s...s..........
.........s.....s.........
........s.......s........
.......s.........s.......
......s...........s......
.....s.wwwwwwwwwww.s.....
....s..w....o....w..s....
...s...w...o.o...w...s...
..s....w..o...o..w....s..
.s.....w.o.eee.o.w.....s.
s......wo..eme..ow......s
.s.....w.o.eee.o.w.....s.
..s....w..o...o..w....s..
...s...w...o.o...w...s...
....s..w....o....w..s....
.....s.wwwwwwwwwww.s.....
......s...........s......
.......s.........s.......
........s.......s........
.........s.....s.........
..........s...s..........
...........s.s...........
............s............


fffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
f............................l............................f
f...........................l.l...........................f
f..........................l...l..........................f
f.........................l.....l.........................f
f........................l.......l........................f
f.......................l.........l.......................f
f......................l...........l......................f
f.....................l.............l.....................f
f....................l...............l....................f
f...................l.................l...................f
f..................l...................l..................f
f.................l.....................l.................f
f................l.......................l................f
f...............l.........................l...............f
f..............l...........................l..............f
f.............l.ooooooooooooooooooooooooooo.l.............f
f............l..o............g............o..l............f
f...........l...o...........g.g...........o...l...........f
f..........l....o..........g...g..........o....l..........f
f.........l.....o.........g.....g.........o.....l.........f
f........l......o........g.......g........o......l........f
f.......l.......o.......g.........g.......o.......l.......f
f......l........o......g...........g......o........l......f
f.....l.........o.....g.eeeeeeeeeee.g.....o.........l.....f
f....l..........o....g..e....d....e..g....o..........l....f
f...l...........o...g...e...d.d...e...g...o...........l...f
f..l............o..g....e..d...d..e....g..o............l..f
f.l.............o.g.....e.d.ooo.d.e.....g.o.............l.f
fl..............og......ed..oco..de......go..............lf
f.l.............o.g.....e.d.ooo.d.e.....g.o.............l.f
f..l............o..g....e..d...d..e....g..o............l..f
f...l...........o...g...e...d.d...e...g...o...........l...f
f....l..........o....g..e....d....e..g....o..........l....f
f.....l.........o.....g.eeeeeeeeeee.g.....o.........l.....f
f......l........o......g...........g......o........l......f
f.......l.......o.......g.........g.......o.......l.......f
f........l......o........g.......g........o......l........f
f.........l.....o.........g.....g.........o.....l.........f
f..........l....o..........g...g..........o....l..........f
f...........l...o...........g.g...........o...l...........f
f............l..o............g............o..l............f
f.............l.ooooooooooooooooooooooooooo.l.............f
f..............l...........................l..............f
f...............l.........................l...............f
f................l.......................l................f
f.................l.....................l.................f
f..................l...................l..................f
f...................l.................l...................f
f....................l...............l....................f
f.....................l.............l.....................f
f......................l...........l......................f
f.......................l.........l.......................f
f........................l.......l........................f
f.........................l.....l.........................f
f..........................l...l..........................f
f...........................l.l...........................f
f............................l............................f
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffff


+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+............................................................+............................................................+
+...........................................................+.+...........................................................+
+..........................................................+...+..........................................................+
+.........................................................+.....+.........................................................+
+........................................................+.......+........................................................+
+.......................................................+.........+.......................................................+
+......................................................+...........+......................................................+
+.....................................................+.............+.....................................................+
+....................................................+...............+....................................................+
+...................................................+.................+...................................................+
+..................................................+...................+..................................................+
+.................................................+.....................+.................................................+
+................................................+.......................+................................................+
+...............................................+.........................+...............................................+
+..............................................+...........................+..............................................+
+.............................................+.............................+.............................................+
+............................................+...............................+............................................+
+...........................................+.................................+...........................................+
+..........................................+...................................+..........................................+
+.........................................+.....................................+.........................................+
+........................................+.......................................+........................................+
+.......................................+.........................................+.......................................+
+......................................+...........................................+......................................+
+.....................................+.............................................+.....................................+
+....................................+...............................................+....................................+
+...................................+.................................................+...................................+
+..................................+...................................................+..................................+
+.................................+.....................................................+.................................+
+................................+.......................................................+................................+
+...............................+.........................................................+...............................+
+..............................+...........................................................+..............................+
+.............................+.CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC.+.............................+
+............................+..C............................r............................C..+............................+
+...........................+...C...........................r.r...........................C...+...........................+
+..........................+....C..........................r...r..........................C....+..........................+
+.........................+.....C.........................r.....r.........................C.....+.........................+
+........................+......C........................r.......r........................C......+........................+
+.......................+.......C.......................r.........r.......................C.......+.......................+
+......................+........C......................r...........r......................C........+......................+
+.....................+.........C.....................r.............r.....................C.........+.....................+
+....................+..........C....................r...............r....................C..........+....................+
+...................+...........C...................r.................r...................C...........+...................+
+..................+............C..................r...................r..................C............+..................+
+.................+.............C.................r.....................r.................C.............+.................+
+................+..............C................r.......................r................C..............+................+
+...............+...............C...............r.........................r...............C...............+...............+
+..............+................C..............r...........................r..............C................+..............+
+.............+.................C.............r.ooooooooooooooooooooooooooo.r.............C.................+.............+
+............+..................C............r..o............F............o..r............C..................+............+
+...........+...................C...........r...o...........F.F...........o...r...........C...................+...........+
+..........+....................C..........r....o..........F...F..........o....r..........C....................+..........+
+.........+.....................C.........r.....o.........F.....F.........o.....r.........C.....................+.........+
+........+......................C........r......o........F.......F........o......r........C......................+........+
+.......+.......................C.......r.......o.......F.........F.......o.......r.......C.......................+.......+
+......+........................C......r........o......F...........F......o........r......C........................+......+
+.....+.........................C.....r.........o.....F.eeeeeeeeeee.F.....o.........r.....C.........................+.....+
+....+..........................C....r..........o....F..e....t....e..F....o..........r....C..........................+....+
+...+...........................C...r...........o...F...e...t.t...e...F...o...........r...C...........................+...+
+..+............................C..r............o..F....e..t...t..e....F..o............r..C............................+..+
+.+.............................C.r.............o.F.....e.t.ooo.t.e.....F.o.............r.C.............................+.+
++..............................Cr..............oF......et..oVo..te......Fo..............rC..............................++
+.+.............................C.r.............o.F.....e.t.ooo.t.e.....F.o.............r.C.............................+.+
+..+............................C..r............o..F....e..t...t..e....F..o............r..C............................+..+
+...+...........................C...r...........o...F...e...t.t...e...F...o...........r...C...........................+...+
+....+..........................C....r..........o....F..e....t....e..F....o..........r....C..........................+....+
+.....+.........................C.....r.........o.....F.eeeeeeeeeee.F.....o.........r.....C.........................+.....+
+......+........................C......r........o......F...........F......o........r......C........................+......+
+.......+.......................C.......r.......o.......F.........F.......o.......r.......C.......................+.......+
+........+......................C........r......o........F.......F........o......r........C......................+........+
+.........+.....................C.........r.....o.........F.....F.........o.....r.........C.....................+.........+
+..........+....................C..........r....o..........F...F..........o....r..........C....................+..........+
+...........+...................C...........r...o...........F.F...........o...r...........C...................+...........+
+............+..................C............r..o............F............o..r............C..................+............+
+.............+.................C.............r.ooooooooooooooooooooooooooo.r.............C.................+.............+
+..............+................C..............r...........................r..............C................+..............+
+...............+...............C...............r.........................r...............C...............+...............+
+................+..............C................r.......................r................C..............+................+
+.................+.............C.................r.....................r.................C.............+.................+
+..................+............C..................r...................r..................C............+..................+
+...................+...........C...................r.................r...................C...........+...................+
+....................+..........C....................r...............r....................C..........+....................+
+.....................+.........C.....................r.............r.....................C.........+.....................+
+......................+........C......................r...........r......................C........+......................+
+.......................+.......C.......................r.........r.......................C.......+.......................+
+........................+......C........................r.......r........................C......+........................+
+.........................+.....C.........................r.....r.........................C.....+.........................+
+..........................+....C..........................r...r..........................C....+..........................+
+...........................+...C...........................r.r...........................C...+...........................+
+............................+..C............................r............................C..+............................+
+.............................+.CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC.+.............................+
+..............................+...........................................................+..............................+
+...............................+.........................................................+...............................+
+................................+.......................................................+................................+
+.................................+.....................................................+.................................+
+..................................+...................................................+..................................+
+...................................+.................................................+...................................+
+....................................+...............................................+....................................+
+.....................................+.............................................+.....................................+
+......................................+...........................................+......................................+
+.......................................+.........................................+.......................................+
+........................................+.......................................+........................................+
+.........................................+.....................................+.........................................+
+..........................................+...................................+..........................................+
+...........................................+.................................+...........................................+
+............................................+...............................+............................................+
+.............................................+.............................+.............................................+
+..............................................+...........................+..............................................+
+...............................................+.........................+...............................................+
+................................................+.......................+................................................+
+.................................................+.....................+.................................................+
+..................................................+...................+..................................................+
+...................................................+.................+...................................................+
+....................................................+...............+....................................................+
+.....................................................+.............+.....................................................+
+......................................................+...........+......................................................+
+.......................................................+.........+.......................................................+
+........................................................+.......+........................................................+
+.........................................................+.....+.........................................................+
+..........................................................+...+..........................................................+
+...........................................................+.+...........................................................+
+............................................................+............................................................+
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

2
std::vector<S> V有一个无用的空格。
Yytsi '16

1
印象深刻的答案!+1你可以打高尔夫球更一些,虽然:在if(A(i)<=R(k)&&A(j)<=R(k)&&k%2?A(i)==R(k)||A(j)==R(k)&&||&|为-3个字节; 并且int r=R(w.size()-1),...可以变为int l=w.size(),r=R(l-1),...for(k=0;k<w.size();++k)然后变为for(k=0;k<l;++k)另一个-3字节。
凯文·克鲁伊森

通过使用#import并导入字符串而不是iostream节省了另外4个字节。iostream仅对于printSquare()是必需的;
Anedar

1
从C ++ 11开始,您可以使用using S=std::string;来保护每个字节的两个字节typedef
Baum mit Augen

1
@grooveplex如答案中所述:“注意:此解决方案使用点('。')作为“空格”字符以使空白变得更明显。要使用空格,则必须更改S(s,46)到S(s,32)。分数保持不变。”
阿内达(Anedar)

4

Mathematica,167个字节

Grid@Fold[g=Normal@SparseArray[h=f;a_List/;Plus@@Abs[a-#-1]==#->#3,2{#,#}+1]+a[#2,#/2+1]&[Length@#+1,#,#2]&;h=f=(h=g;a=ArrayPad)[#,1,#2]&;h@##&,{{#}},{##2}]/.Rule[0,]&

输入必须是字符列表。

例如

Grid@Fold[ ... ]/.Rule[0,]&["i","n","p","u","t"]

3

JavaScript(ES6),252个字节

f=([c,...r],a,n,d)=>c?a?d?f(r,[...Array(m=n+n+3)].map((_,i,b)=>b.map((_,j)=>(a[i-g]||[])[j-g]||(i+j-h&&j+h-i&&h+i-j&&i+j-h*3?` `:c)),h=m>>1,g=m-n>>1),m,0):f(r,[b=[...a].fill(c),...a,b].map(b=>[c,...b,c]),n+2,1):f(r,[[c]],1,0):a.map(b=>b.join``).join`\n`
<input oninput=o.textContent=this.value&&f(this.value)><pre id=o>

其中\n代表文字换行符。


3

JavaScript的(ES6),204 229 238

返回字符串数组的匿名函数

s=>[...s].map(v=>{T=(x,y)=>((r=[...c[y+z]||' '.repeat(z+z)])[z+x]=r[z-x]=v,c[z+y]=c[z-y]=r.join``);for(y=o-~o,o*=++i%2+1;y--;i%2?T(q,y):T(o,q,T(q,o)))q=y-o;++o},l=s.length,z=(2<<-~l/2)-3-l%2,c=[i=o=0])&&c

少打高尔夫球

s=>(
  l = s.length,
  z = (2 << l/2 + .5) - 3 - l%2, // zero position, total width and height are z+z+1
  c = [], // output canvas
  o = 0, // base drawing offset from z
  [...s].map( (v, i) => {
    // execute for each char v at position i in input string
    // helper function to put current char v at position (z+y, z+x),(z+y,z-x),(z-y,z+x),(z-y,z-x)
    // as the image has vertical and horizontal symmetry
    T=(x,y) =>(
      r = [...c[y+z]||' '.repeat(z+z)]), // convert string to array to set char at given pos
      r[z+x] = v, 
      r[z-x] = v,
      c[z+y] = c[z-y] = r.join`` // array to string again
    ); 
    // shorter than if(i%2) {for ...}else{for ...}
    // o is doubled at every other step
    //   and incremented (below) at each step
    for(y=o+o, i % 2 ? 0 : o=o*2; y >= 0; y--)
      q = y-o,
      i % 2 // even and odd steps have different shapes
        ? ( T(o,q), T(q,o))
        : T(q,y);
    ++o 
  }),
  c
)

测试

f=
s=>[...s].map(v=>{T=(x,y)=>((r=[...c[y+z]||' '.repeat(z+z)])[z+x]=r[z-x]=v,c[z+y]=c[z-y]=r.join``);for(y=o-~o,o*=++i%2+1;y--;i%2?T(q,y):T(o,q,T(q,o)))q=y-o;++o},l=s.length,z=(2<<-~l/2)-3-l%2,c=[i=o=0])&&c

function update()
{
  O.textContent=f(I.value).join`\n`
}

update()
<input id=I value=CodeGolf oninput='update()'>
<pre id=O></pre>


2

Java- 429417字节

int r,l;int D(int n){return Math.abs(n-r);}int R(int n){return n=(n!=0)?((n%2!=0)?(R(n-1)+1):(2*R(n-1))+2):n;}char[][]C(String a){char[]c=a.toCharArray();l=a.length();r=R(l-1);int s=r*2+1,i,j,k;char[][]x=new char[s][s];for(i=0;i<s;i++){for(j=0;j<s;j++){x[i][j]=' ';for(k=0;k<l;k++){if(D(i)<=R(k)&&D(j)<=R(k)){if(k%2!=0){if(D(i)==R(k)||D(j)==R(k)){x[i][j]=c[k];}}else{if(D(i)+D(j)==R(k)){x[i][j]=c[k];}}}}}}return x;}

感谢@Anedar,因为这对此答案的结构有影响

取消高尔夫:

int r, l;

int D(int n) {
    return Math.abs(n - r);
}

int R(int n) {
    return n = (n != 0) ? ((n % 2 != 0) ? (R(n - 1) + 1) : (2 * R(n - 1)) + 2) : n;
}

char[][] C(String a) {
    char[] c = a.toCharArray();
    l = a.length();
    r = R(l - 1);
    int s = r * 2 + 1, i, j, k;
    char[][] x = new char[s][s];
    for (i = 0; i < s; i++) {
        for (j = 0; j < s; j++) {
            x[i][j] = ' ';
            for (k = 0; k < l; k++) {
                if (D(i) <= R(k) && D(j) <= R(k)) {
                    if (k % 2 != 0) {
                        if (D(i) == R(k) || D(j) == R(k)) {
                            x[i][j] = c[k];
                        }
                    } else if (D(i) + D(j) == R(k)) {
                        x[i][j] = c[k];
                    }
                }
            }
        }
    }
    return x;
}

1

JavaScript(ES6),322字节

非竞争性

var L=s=>{var A=Array,a,D,c,d=-1,i,k,w,O,Z,P=(u,v)=>{Z=O-(d>>1);a[D?u-v+O:u+Z][D?u+v+O-d+1:v+Z]=c},G=(c,i)=>(D=~i&1)?(d+=2,w=2*d-1):(d+=d+1,w=d);s=[...s];s.map(G);a=A(w).fill(0).map(a=>A(w).fill` `);O=w>>1;d=-1;s.map((C,i)=>{G(0,i);c=C;for(k=d;k--;)P(0,k),P(d-1,k),P(k,0),P(k,d-1)});return a.map(v=>v.join("")).join("\n")}

减少凝视

var L=s=>{
    var A=Array,a,D,c,d=-1,i,k,w,O,Z
    P=(u,v)=>{
        Z=O-(d>>1)
        a[D?u-v+O:u+Z][D?u+v+O-d+1:v+Z]=c},
    G=(c,i)=>(D=~i&1)?(d+=2,w=2*d-1):(d+=d+1,w=d)
    s=[...s]
    s.map(G)
    a=A(w).fill(0).map( a=>A(w).fill` `)
    O=w>>1
    d=-1
    s.map((C,i)=>{
        G(0,i)
        c=C
        for(k=d;k--;)
            P(0,k),P(d-1,k),P(k,0),P(k,d-1)})
    return a.map(v=>v.join("")).join("\n")
}

测试

L("arty*")

通过省略开头var L=.join("\n")return语句中的-17个字节可轻松获得收益,但再次键入它们以测试代码:D

这是一次两次通过的答案,在使用旋转和平移在第二次通过中w绘制点之前,评估绘制正方形或菱形图所需的宽度()Pd是平移或旋转之前未旋转的正方形的尺寸(高度或宽度)。

通过Mathematica答案中的 √2细节进行旋转和缩放。从该线程的第一个答案中使用带标记的字符串模板。感谢双方的学习。对问题和答案进行投票。


1

PowerShell中,269个 245 238字节

$m,$a=$args
if($a){$a|%{$c=$_
$w=$m[0].Length
$m=&({$i=0
($m|%{$_+'.'*$w+'.'})+(0..$w|%{'..'*$w+'.'})|%{($s=$_|% t*y)[--$i]=$c
-join$s}},{$m|%{$_+$c}
$c*$w+$c})[++$k%2]}
$m=($w=$m[0].Length-1)..1+0..$w|%{$m[$_]|%{-join($_[$w..1]+$_)}}}
$m

取消高尔夫,解释和测试脚本:

$f={

# $args is input char array
# $m is canvas.
# This script draws a right-bottom quadrant (axes included) on the canvas.
# If $args contains one char only then $m contains this char and whole image at same time.
$m,$a=$args                             # $a is other chars array
if($a){
    $a|%{
        $c=$_                           # $c is current char
        $w=$m[0].Length                 # $w is width of the canvas
        $m=&({                          # $m is the result of executing one of the two scriptblocks
            # draw Diamond quadrant
            $i=0
            ($m|%{$_+'.'*$w+'.'})+      # expand the canvas
            (0..$w|%{'..'*$w+'.'})|%{   # add new lines
                ($s=$_|% t*y)[--$i]=$c  # take a diamond edge on each line of the canvas
                -join$s
            }
        },{
            # draw Square quadrant
            $m|%{$_+$c}                 # expand the canvas
            $c*$w+$c                    # add new line
        })[++$k%2]                      # get a scriptblock from the array and execute it
    }

    # We have a right-bottom part of drawn AsciiArt now. Flip, flip, paste it.
    $w=$m[0].Length-1                   # width of the canvas
    $m=$w..1+0..$w|%{$m[$_]|%{          # flip vertical
        -join($_[$w..1]+$_)             # flip horizontal
    }}
}
$m

}

@(
,('c',@"
c
"@)
,('ca',@"
aaa
aca
aaa
"@)
,('cat',@"
....t....
...t.t...
..t...t..
.t.aaa.t.
t..aca..t
.t.aaa.t.
..t...t..
...t.t...
....t....
"@)
,('cats',@"
sssssssssss
s....t....s
s...t.t...s
s..t...t..s
s.t.aaa.t.s
st..aca..ts
s.t.aaa.t.s
s..t...t..s
s...t.t...s
s....t....s
sssssssssss
"@)
,('meows',@"
............s............
...........s.s...........
..........s...s..........
.........s.....s.........
........s.......s........
.......s.........s.......
......s...........s......
.....s.wwwwwwwwwww.s.....
....s..w....o....w..s....
...s...w...o.o...w...s...
..s....w..o...o..w....s..
.s.....w.o.eee.o.w.....s.
s......wo..eme..ow......s
.s.....w.o.eee.o.w.....s.
..s....w..o...o..w....s..
...s...w...o.o...w...s...
....s..w....o....w..s....
.....s.wwwwwwwwwww.s.....
......s...........s......
.......s.........s.......
........s.......s........
.........s.....s.........
..........s...s..........
...........s.s...........
............s............
"@)
,('codegolf',@"
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
f............................l............................f
f...........................l.l...........................f
f..........................l...l..........................f
f.........................l.....l.........................f
f........................l.......l........................f
f.......................l.........l.......................f
f......................l...........l......................f
f.....................l.............l.....................f
f....................l...............l....................f
f...................l.................l...................f
f..................l...................l..................f
f.................l.....................l.................f
f................l.......................l................f
f...............l.........................l...............f
f..............l...........................l..............f
f.............l.ooooooooooooooooooooooooooo.l.............f
f............l..o............g............o..l............f
f...........l...o...........g.g...........o...l...........f
f..........l....o..........g...g..........o....l..........f
f.........l.....o.........g.....g.........o.....l.........f
f........l......o........g.......g........o......l........f
f.......l.......o.......g.........g.......o.......l.......f
f......l........o......g...........g......o........l......f
f.....l.........o.....g.eeeeeeeeeee.g.....o.........l.....f
f....l..........o....g..e....d....e..g....o..........l....f
f...l...........o...g...e...d.d...e...g...o...........l...f
f..l............o..g....e..d...d..e....g..o............l..f
f.l.............o.g.....e.d.ooo.d.e.....g.o.............l.f
fl..............og......ed..oco..de......go..............lf
f.l.............o.g.....e.d.ooo.d.e.....g.o.............l.f
f..l............o..g....e..d...d..e....g..o............l..f
f...l...........o...g...e...d.d...e...g...o...........l...f
f....l..........o....g..e....d....e..g....o..........l....f
f.....l.........o.....g.eeeeeeeeeee.g.....o.........l.....f
f......l........o......g...........g......o........l......f
f.......l.......o.......g.........g.......o.......l.......f
f........l......o........g.......g........o......l........f
f.........l.....o.........g.....g.........o.....l.........f
f..........l....o..........g...g..........o....l..........f
f...........l...o...........g.g...........o...l...........f
f............l..o............g............o..l............f
f.............l.ooooooooooooooooooooooooooo.l.............f
f..............l...........................l..............f
f...............l.........................l...............f
f................l.......................l................f
f.................l.....................l.................f
f..................l...................l..................f
f...................l.................l...................f
f....................l...............l....................f
f.....................l.............l.....................f
f......................l...........l......................f
f.......................l.........l.......................f
f........................l.......l........................f
f.........................l.....l.........................f
f..........................l...l..........................f
f...........................l.l...........................f
f............................l............................f
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
"@)
#,('codegolfes',"")
) | % {
    $s, $e = $_
    $c = $s-split''-ne''
    $r = &$f @c
    $r = $r-join"`n"
    "$($e-eq$r): $s`n$r"
}
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.