Kwadrat, diament, kwadrat, diament


41

Zadanie

Biorąc pod uwagę niepusty ciąg małych liter ASCII a- zweź jego pierwszy znak i:

  • Otocz go kwadratem kopii drugiej postaci,
  • Otocz to diamentem kopii trzeciej postaci,
  • Otocz to kwadratem czwartej postaci…

… Na przemian między kwadratowymi i diamentowymi granicami do końca sznurka. Puste miejsce między granicami powinno być reprezentowane przez spacje ASCII ( ).

Aby dodać kwadratową ramkę, narysuj kwadrat dokładnie wokół całego obecnego „roboczego układu”:

                                               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

Aby dodać obramowanie diamentowe , narysuj wyśrodkowany kształt rombu, który dotyka najbardziej zewnętrznego kwadratu po przekątnej , ale nie prostopadle :

                                                     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            

Twój program musi wypisać końcową tablicę.

  • Każda linia może zawierać dowolną liczbę końcowych spacji.
  • Możesz wypisać listę ciągów znaków reprezentujących linie lub pojedynczy ciąg znaków oddzielony znakiem nowej linii z opcjonalnym końcowym znakiem nowego wiersza.
  • Wiodące / końcowe puste linie są niedozwolone.
  • Wiodące kolumny przestrzeni są również niedozwolone.

To jest . Najkrótszy kod w bajtach wygrywa.

Przypadki testowe

Powyższe przykłady są spodziewane produkty o cat, cats, meowi meows, w celu przeczytania. Kilka innych spraw wartych załatwienia:

  • Jako dane wejściowe atwój program powinien wypisać:

    a
    
  • Jako dane wejściowe abtwój program powinien wypisać:

    bbb
    bab
    bbb
    
  • Jako dane wejściowe codegolftwój program powinien wypisać:

    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
    

Realizacja referencyjna

Napisałem kod Julii, że wyjście twojego programu powinno pasować (przynajmniej wizualnie). Wypróbuj online!

Odpowiedzi:


5

MATL, 63 54 bajty

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

Wypróbuj online!

Wyjaśnienie

            % 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 bajtów

Mój pierwszy golf na tej stronie. Przekaż ciąg wejściowy do funkcji f, npf('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 bajtów dzięki @Sunny Pun: Usuń nawiasy podczas przekazywania ciągów szablonów.

-2 bajty dzięki @ETHproductions: Zmień S-i-1na S+~i, l%2||o--na o-=~l&1.

-18 bajtów na sugestie @Sunny Pun, @ edc65: Usuń prompt()i console.log()na korzyść formatu funkcji, nie .join()końcowa tablica ciągów


2
Liczę 256 bajtów. Czy jest jakaś szansa, że ​​liczysz znaki nowej linii jako dwa bajty ( \r\n) zamiast jednego ( \n)?
Mego

2
Uaktywnienie tej funkcji powinno oszczędzać bajty poprzez usunięcie prompti console.log. Możesz także użyć ciągów szablonów, które oszczędzają 2 bajty na wywołanie funkcji na ciągach (jak fill(' ')staje się fill` `)
Sunny Pun

4
Witamy w PPCG! Aby zgodzić się z tym, co powiedzieli inni, możesz skrócić S-i-1do S+~ii i%2||o--do o-=~i&1.
ETHproductions

@Mego Ponownie sprawdziłem liczbę, 256 jest poprawnych. Dzięki!
darrylyeo

Myślę, że a=Array(D*2+1).fill .map((_,$,a)=>[...a])oszczędza ci 10 bajtów.
Neil,

11

Haskell, 138 137 136 bajtów

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]

Definiuje funkcję, fktóra zwraca wynik jako listę linii

-1 bajt dzięki @Lynn


8

C ++ - 373 366 362 359 bajtów

-1 przez @TuukkaX, -6 przez @Kevin Cruijssen, -4 przez import, -3 przez @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

Uwaga: W tym rozwiązaniu kropka („.”) Jest znakiem „spacji”, dzięki czemu puste spacje są bardziej widoczne. Aby zamiast tego użyć spacji, należy zmienić S(s,46)na S(s,32). Wynik pozostaje taki sam.

Nie golfowany:

#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;
}

Spróbuj bez golfa online

Jak używać:

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;
}

Wynik:

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> Vma bezużyteczne białe znaki.
Yytsi

1
Imponująca odpowiedź! +1 Można golf to choć trochę więcej: W a może być i dla -3 bajtów; i może stać się, a następnie stać na kolejne -3 bajty. if(A(i)<=R(k)&&A(j)<=R(k)&&k%2?A(i)==R(k)||A(j)==R(k)&&||&|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)
Kevin Cruijssen

zapisał kolejne 4 bajty, używając #import i importując ciąg zamiast iostream. iostream jest niezbędny tylko dla printSquare ();
Anedar,

1
Począwszy od C ++ 11, można użyć using S=std::string;do zabezpieczenia dwóch bajtów na typedef.
Baum mit Augen

1
@grooveplex Jak stwierdzono w odpowiedzi: „Uwaga: w tym rozwiązaniu kropka („. ”) jest znakiem„ spacji ”, dzięki czemu puste spacje są bardziej widoczne. Aby zamiast tego użyć spacji, należy zmienić S (s, 46) do S (s, 32). Wynik pozostaje taki sam. ”
Anedar

4

Mathematica, 167 bajtów

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,]&

Dane wejściowe muszą być listą znaków.

na przykład

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

3

JavaScript (ES6), 252 bajty

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>

Gdzie \nreprezentuje dosłowny znak nowej linii.


3

JavaScript (ES6), 204 229 238

Anonimowa funkcja zwracająca tablicę ciągów

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

Mniej golfa

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
)

Test

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 - 429 417 bajtów

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;}

Podziękowania należą się @Anedar, ponieważ miało to wpływ na strukturę tej odpowiedzi

Nie golfowany:

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 bajty

Niekonkurencyjny

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")}

Mniej Golphed

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")
}

Przetestuj za pomocą

L("arty*")

Łatwe zyski poprzez pominięcie wiodącej var L=i .join("\n")zwrotnej instrukcji dla -17 bajtów, ale wpisz je ponownie, aby przetestować kod : D

Jest to odpowiedź dwuprzebiegowa, oceniająca szerokość ( w) wymaganą do wykreślenia wykresu kwadratowego lub diamentowego, przed wykreśleniem punktów Pw drugim przejściu przy użyciu obrotu i translacji. dto wymiar (wysokość lub szerokość) nieobróconego kwadratu przed przesunięciem lub obrotem.

Obracanie i skalowanie według √2 szczegółów z odpowiedzi Mathematica . Zastosowanie szablonu oznaczonego ciągu od pierwszej odpowiedzi w tym wątku . Dzięki obu za naukę. Głosy na pytanie i odpowiedzi.


1

PowerShell, 269 245 238 bajtów

$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

Skryte niepoznane, wyjaśnione i przetestowane:

$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"
}
Korzystając z naszej strony potwierdzasz, że przeczytałeś(-aś) i rozumiesz nasze zasady używania plików cookie i zasady ochrony prywatności.
Licensed under cc by-sa 3.0 with attribution required.