18
\$\begingroup\$

Input description

A string (for best results, all characters should be printable and be the same width).

Output description

A character star following the pattern:

0  0  0
 1 1 1 
  222  
0123210
  222  
 1 1 1 
0  0  0

where 0, 1 ... are subsequent characters of the string. The output does not necessarily have to be one string - printing the star char by char into the console is fine.

Example

>> star('overflow')
>>
o      o      o
 v     v     v 
  e    e    e  
   r   r   r   
    f  f  f    
     l l l     
      ooo      
overflowolfrevo
      ooo      
     l l l     
    f  f  f    
   r   r   r   
  e    e    e  
 v     v     v 
o      o      o
\$\endgroup\$
4
  • \$\begingroup\$ I think "a string" should be a little more clear, do you mean a string consisting of only printable ASCII? \$\endgroup\$ Feb 22, 2016 at 18:36
  • \$\begingroup\$ I think "a string" is sufficient. Of course, for best results you probably want to avoid tabs or other characters that would distort the shape of the output, but that doesn't mean you can't include them. Still, I updated the description for clarity. \$\endgroup\$
    – shooqie
    Feb 22, 2016 at 18:41
  • 6
    \$\begingroup\$ It's less a question of whether that gives the nicest results but of whether answers have to support it. Some languages might have to use somewhat different code if e.g. non-ASCII (Unicode) characters have to be supported as well. Likewise, some code might be shorter if we can assume that unprintables won't appear (specifically linefeeds). \$\endgroup\$ Feb 22, 2016 at 18:52
  • \$\begingroup\$ Whether or not you can print all characters is irrelevant. If you can make your code shorter, even if it limits you from printing certain characters (like Unicode), then so be it. I think that's the spirit of code-golfing anyway. As long as your program supports common characters, it's fine. If it doesn't, then I don't think the challenge specification is the problem. \$\endgroup\$
    – shooqie
    Feb 22, 2016 at 19:40

21 Answers 21

5
\$\begingroup\$

Canvas, 13 10 bytes

⤢:⁸/n!n↔┼┼

Try it here!

Explanation

⤢:⁸/n!n↔++
⤢          join input with newlines
 :         duplicate it
  ⁸        push the input again
   /       make a diagonal out of it
    n      overlap the two
     !     transpose that result
      n    overlap with the first duplicated one
       ↔   mirror it horizontally
        ++ quad-palindromize without changing characters
\$\endgroup\$
0
5
\$\begingroup\$

05AB1E, 15 12 bytes

gI¨„+×Λ1Iθ8Λ

Try it online.

Explanation:

g             # Push the length of the (implicit) input-string
 I            # Push the input-string again
  ¨           # Remove its last character
   „+×        # Push string "+×"
      Λ       # Use the Canvas builtin with these three arguments
              # (after which the result is output immediately as result)
       1      # Push 1
        Iθ    # Push the last character of the input
          8   # Push 8
           Λ  # Use the Canvas builtin with these three arguments again,
              # writing on top of the previous Canvas result
              # (after which the result is output immediately as result)

The Canvas Builtin uses three arguments to draw a shape:

  • Character/string to draw: the input minus its last character in this case
  • Length of the lines we'll draw: the length of the input in this case
  • The direction to draw in: the "+×", which translates to directions [0,4,4,0,2,6,6,2,1,5,5,1,3,7,7,3], where each digit represents a certain direction:
7   0   1
  ↖ ↑ ↗
6 ← X → 2
  ↙ ↓ ↘
5   4   3

So the /[0,4,4,0,2,6,6,2,1,5,5,1,3,7,7,3] in this case translates to the directions \$[↑,↓,↓,↑,→,←,←,→,↗,↙,↙,↗,↘,↖,↖,↘]\$.

Here a step-by-step explanation of how it draws for example input abcd with these steps (with the other two Canvas arguments being 4 and "abc"):

Step 1: Draw 4 characters ("abca") in direction 0↑:

a
c
b
a

Step 2: Draw 4-1 characters ("bca") in direction 4↓:

a
b
c
a

Step 3: Draw 4-1 characters ("bca") in direction 4↓:

a
b
c
a
b
c
a

Step 4: Draw 4-1 characters ("bca") in direction 0↑:

a
b
c
a
c
b
a

Step 5: Draw 4-1 characters ("bca") in direction 2→:

a
b
c
abca
c
b
a

Etc., resulting in:

a  a  a
 b b b 
  ccc  
abcacba
  ccc  
 b b b 
a  a  a

After which we replace the middle a with the last character of the input-string with 1Iθ8Λ, where Λ is another call of the Canvas builtin with the options: 1 for the length; is the last character of the input; and 8 to reset the direction-option back to the start (although in this case, the 8 could also be any of the eight directions 0-7 for the same result, since it already ended at the center anyway).

\$\endgroup\$
4
\$\begingroup\$

Charcoal, 7 4 bytes

P*⮌S

Try it online! (Link to the verbose version.)

\$\endgroup\$
2
\$\begingroup\$

Pyth, 37 bytes

jKm:*;t*2lz[dtlzt_d)*3@zdtlz)pzt_zj_K

Try it here!

\$\endgroup\$
2
\$\begingroup\$

APL (Dyalog Unicode), 74 49 45 bytesSBCS

{⍵@(⌈x÷2)∘⍉∘(⌽⍵@(2/¨⍳x))⍣2⊢''⍴⍨2⍴x←≢⍵}(⊢,1↓⌽)

Try it online!

-11 thanks to ovs

Explanation:

{⍵@(⌈x÷2)∘⍉∘(⌽⍵@(2/¨⍳x))⍣2⊢''⍴⍨2⍴x←≢⍵}(⊢,1↓⌽)
                                      (⊢,1↓⌽) ⍝ turn "abc" into "abcba"
{                                x←≢⍵}        ⍝ x is the length of that string (passed as an argument)
                           ''⍴⍨2⍴             ⍝ Generate a blank string of x times x
                        ⍣2⊢                   ⍝ Do twice:
 ⍵@(⌈x÷2)∘⍉∘(⌽⍵@(2/¨⍳x))                      ⍝ Update our blank string with our argument (the mirrored string):
              ⍵@(2/¨⍳x)                       ⍝ Add our element horizontally, replace with our (mirrored) argument
            (⌽         )                      ⍝ Rotate vertically
          ⍉∘                                  ⍝ Transpose (double rotate, vertical and horizontal)
 ⍵@(⌈x÷2)∘                                    ⍝ Add our argument vertically to the string

Since we're doing these transposes twice, we'll have rotated our image on all four quadrants.

\$\endgroup\$
5
  • \$\begingroup\$ You can save a few bytes by assigning ≢⍵ to a variable: Try it online! \$\endgroup\$
    – ovs
    Feb 12, 2021 at 12:27
  • \$\begingroup\$ Removing some duplicated code and two minor changes (' ' -> '' and 2×⍨x -> 2×x) bring this down to 58 bytes. \$\endgroup\$
    – ovs
    Feb 12, 2021 at 12:39
  • \$\begingroup\$ Thank you, integrated :) \$\endgroup\$
    – Ven
    Feb 12, 2021 at 12:45
  • \$\begingroup\$ 43 bytes \$\endgroup\$
    – ovs
    Feb 12, 2021 at 13:18
  • \$\begingroup\$ Updated,thanks again \$\endgroup\$
    – Ven
    Feb 12, 2021 at 13:30
2
\$\begingroup\$

PowerShell Core, 155 99 bytes

param($a)($r=0..($l=$a.length-2)|%{" "*$_+(,$a[$_]*3-join" "*($l-$_))})
$a+-join$a[$l..0]
$r[$l..0]

Try it online!

56 bytes saved thanks to @mazzy!

Explanation

# Build an array containing the top of the star
($r=0..($l=$a.length-2)|%{" "*$_+(,$a[$_]*3-join" "*($l-$_))})
# Prints the middle line
$a+-join$a[$l..0]
# Prints the array with the top of the star reversed
$r[$l..0]
\$\endgroup\$
3
  • 1
    \$\begingroup\$ write-host?! Try it online! \$\endgroup\$
    – mazzy
    Jun 20, 2021 at 13:45
  • 1
    \$\begingroup\$ golfed a little Try it online! \$\endgroup\$
    – mazzy
    Jun 20, 2021 at 14:26
  • \$\begingroup\$ @mazzy Wow, I overlooked so many obvious improvements and you added lots of smart changes. Thanks! \$\endgroup\$
    – Julian
    Jun 20, 2021 at 21:40
2
\$\begingroup\$

Uiua, 38 37 36 bytes

⊏⊙⊂⍥(⍉⊂⇌↘1.)2⊞(×↥⊙⊃=↥=1↧,,).+1⇡⧻,@ ⇌

-2 thanks to chunes.

Draws a quadrant, then replicates it. Since we count from 0, not 1, we need a +1 / -1, otherwise the center would be 0 as well (because multiplying the maximum of 0 and 0 is... 0), and it'd get transformed to a space.

Explanation:

⊏⊙⊂⍥(⍉⊂⇌↘1.)2⊞(×↥⊙⊃=↥=1↧,,).+1⇡⧻,@ ⇌
                                   ⇌ # reverse the input string
                                 @   # put a space on the stack
                                ,    # over: copy the string back to the top of the stack
                              ⇡⧻     # range 1..length
                            +1       # add 1 so it's 1-indexed
                           .         # duplicate
             ⊞(           )          # for each combination (of the duplicates), this gives us a matrix row:column
                        ,,           # copy the 2 top values on the stack
                     =1↧             # is the minimum 1?
                 ⊙                   # store this result, temporarily remove it from the stack to...
                  ⊃                  # ...fork: do both operations to the same elements on the stack
                    ↥                # maximum
                   =                 # equal
                ↥                    # maximum of this fork
               ×                     # times the stored result
                                     # We now have the first quadrant
   ⍥(      )2                        # do twice
          .                          # copy the quadrant
        ↘1                           # drop the first element (because the middle would be duplicated otherwise)
       ⇌                             # reverse it (needs to be mirror)
      ⊂                              # append the quadrant with the mirror
     ⍉                               # transpose it so the pattern works on both sides
 ⊙                                   # store this result, temporarily remove it from the stack to...
  ⊂                                  # join the string with the space
⊏                                    # index into the strings with our matrix, zero being the empty space we just appended

Try it online!

\$\endgroup\$
3
  • 1
    \$\begingroup\$ ⊃(=|=1↧|↥) can be ⊙⊃=↥=1↧,, for -1. \$\endgroup\$
    – chunes
    Feb 21 at 16:50
  • \$\begingroup\$ Edited, thanks :) \$\endgroup\$
    – Ven
    Feb 21 at 17:12
  • \$\begingroup\$ -1 more by rearranging some things. \$\endgroup\$
    – chunes
    Feb 21 at 17:27
1
\$\begingroup\$

Perl, 97 93 + 2 = 95 bytes

$i=y///c-2;push@a,map{$"x$j++.$_.($"x$i--.$_)x2}/.(?!$)/g;say for@a,s/.$//r.reverse,reverse@a

Requires -nlE flags:

$ perl -nlE'$i=y///c-2;push@a,map{$"x$j++.$_.($"x$i--.$_)x2}/.(?!$)/g;say for@a,s/.$//r.reverse,reverse@a' <<< 'overflow'
o      o      o
 v     v     v
  e    e    e
   r   r   r
    f  f  f
     l l l
      ooo
overflowolfrevo
      ooo
     l l l
    f  f  f
   r   r   r
  e    e    e
 v     v     v
o      o      o

Ungolfed:

$i=y///c-2;
push @a, map{
  $" x $j++ . 
  $_ . 
  ($" x $i-- . $_)x2
} /.(?!$)/g;
say for @a, s/.$//r.reverse, reverse@a
\$\endgroup\$
1
\$\begingroup\$

Seriously, 57 bytes

╩╜#dXΣR;╝;lr;R3Z`i' *;(;;))@(((@)' *;)kΣ`M;R@k`'
j`Mi╛╜+@

Yes, that newline is supposed to be there. Yes, Seriously still sucks at string manipulation. Hexdump (reversible with xxd -r):

00000000: cabd 2364 58e4 523b bc3b 6c72 3b52 335a  ..#dX.R;.;lr;R3Z
00000010: 6069 2720 2a3b 283b 3b29 2940 2828 2840  `i' *;(;;))@(((@
00000020: 2927 202a 3b29 6be4 604d 3b52 406b 6027  )' *;)k.`M;R@k`'
00000030: 0a6a 604d 69be bd2b 40                   .j`Mi..+@

I'll update this with an explanation once I finish writing it up. It's kinda long.

\$\endgroup\$
1
\$\begingroup\$

ES6, 153 bytes

s=>[...a=(t=[...s.slice(0,-1)]).map((c,i)=>(a=Array(t.length).fill` `,a[i]=c,a.join``+c+a.reverse().join``)),s+t.reverse().join``,...a.reverse()].join`\n`

Ungolfed:

function star(s) {
    r = [];
    h = s.length - 1;
    for (i = 0; i < h; i++) {
        a = [...' '.repeat(h)];
        a[i] = s[i];
        a = a.concat(s[i]).concat(a.reverse());
        r.push(a.join(''));
    }
    return r.concat(s + [...s.slice(0,h)].reverse().join('')).concat(r.reverse()).join("\n");
}

Alternative solution, also 153 bytes:

s=>[...a=(t=[...s].reverse().slice(1)).map((c,i)=>(a=Array(l+l+1).fill` `,a[i]=a[l]=a[l+l-i]=c,a.join``),l=t.length),s+t.join``,...a.reverse()].join`\n`

Ungolfed:

function star(s) {
    r = [];
    h = s.length - 1;
    for (i = 0; i < h; i++) {
        a = [...' '.repeat(h + h + 1)];
        a[i] = s[i];
        a[h] = s[i];
        a[h + h - i] = s[i];
        r.push(a.join(''));
    }
    return r.concat(s + [...s].reverse().slice(1).join('')).concat(r.reverse()).join("\n");
}

Note: The \n inside `s is a literal newline character.

\$\endgroup\$
1
\$\begingroup\$

Vyxal, 22 bytes

Ṫƛ¥n꘍⁰L‹↲$+&›;$Jvømøm⁋

Try it Online!

Ṫ                      # String[:-1] 
 ƛ           ;         # Map...
  ¥n꘍                  # Prepend (register) spaces
     ⁰L‹↲              # Justify to the left by correct spacing
         $+            # Append the correct letter
           &›          # Increment the register
              $J       # Append the input value
                vøm    # Palindromise each
                   øm  # Palindromise
                     ⁋ # Join by newlines
\$\endgroup\$
1
\$\begingroup\$

Japt -R, 18 bytes

¬ÔËiDúEÃhU Ômê û ê

Try it

¬ÔËiDúEÃhU Ômê û ê     :Implicit input of string U                                 > "1234"
¬                      :Split                                                      > ["1","2","3","4"]
 Ô                     :Reverse                                                    > ["4","3","2","1"]
  Ë                    :Map each D at 0-based index E
   i                   :  Prepend
    DúE                :    D right-padded with spaces to length E                 > ["4","3","2 ","1  "]
       Ã               :End map                                                    > ["44","33","2 2","1  1"]
        hU             :Replace the first element with U                           > ["1234","33","2 2","1  1"]
           Ô           :Reverse                                                    > ["1  1","2 2","33","1234"]
            m          :Map
             ê         :  Palindromise                                             > ["1  1  1","2 2 2","333","1234321"]
               û       :Centre pad each with spaces to the length of the longest   > ["1  1  1"," 2 2 2 ","  333  ","1234321"]
                 ê     :Palidromise                                                > ["1  1  1"," 2 2 2 ","  333  ","1234321","  333  "," 2 2 2 ","1  1  1"]
                       :Implicit output joined with newlines
\$\endgroup\$
1
\$\begingroup\$

Python 3, 123 bytes

def f(s):n=len(s);o=[((s[i]+" "*(n-i-2))*2+s[i]).center(2*n)for i in range(n-1)];print("\n".join(o+[s+s[-2::-1]]+o[::-1]))
\$\endgroup\$
1
\$\begingroup\$

Haskell, 72 bytes

f(h:t)|_:s<-' '<$t,d<-s++h#s=(h:d)#zipWith(#)d(f t)
f x=[x]
s#t=s:t++[s]

Try it online!

Defines f :: String -> [String].

\$\endgroup\$
1
\$\begingroup\$

J, 39 38 37 bytes

(' ',|.){~[:(>.*=+.1=<.)"0/~#\.,1}.#\

Try it online!

Consider over:

First imagine a table of coordinates like:

┌───┬───┬───┬───┬───┬───┬───┐
│4 4│4 3│4 2│4 1│4 2│4 3│4 4│
├───┼───┼───┼───┼───┼───┼───┤
│3 4│3 3│3 2│3 1│3 2│3 3│3 4│
├───┼───┼───┼───┼───┼───┼───┤
│2 4│2 3│2 2│2 1│2 2│2 3│2 4│
├───┼───┼───┼───┼───┼───┼───┤
│1 4│1 3│1 2│1 1│1 2│1 3│1 4│
├───┼───┼───┼───┼───┼───┼───┤
│2 4│2 3│2 2│2 1│2 2│2 3│2 4│
├───┼───┼───┼───┼───┼───┼───┤
│3 4│3 3│3 2│3 1│3 2│3 3│3 4│
├───┼───┼───┼───┼───┼───┼───┤
│4 4│4 3│4 2│4 1│4 2│4 3│4 4│
└───┴───┴───┴───┴───┴───┴───┘

For each element, ask "Are they equal or does either equal one" =+.*e.,? The answer will be 1 if true, 0 otherwise. Multiply that answer by the max of the two numbers >.*:

4 0 0 4 0 0 4
0 3 0 3 0 3 0
0 0 2 2 2 0 0
4 3 2 1 2 3 4
0 0 2 2 2 0 0
0 3 0 3 0 3 0
4 0 0 4 0 0 4

Now just index into the original input, reversed, with a space prepended (' ',|.){~:

o  o  o
 v v v 
  eee  
overevo
  eee  
 v v v 
o  o  o
\$\endgroup\$
1
\$\begingroup\$

APL (Dyalog), 36 bytes

{(' ',⌽⍵)[1+∘.(⌈×=∨1=⌊)⍨(⌽,1∘↓)⍳≢⍵]}

Try it on APLgolf!

This takes the same approach as Jonah's J answer.

(⌽,1∘↓)⍳≢⍵ turns 'over' into the array 4 3 2 1 2 3 4.

Applying ∘.(⌈×=∨1=⌊)⍨ makes a table of \$(\alpha,\omega) \mapsto \max(\alpha,\omega) \times [\alpha=\omega \vee 1=\min(\alpha,\omega)]\$

4 0 0 4 0 0 4
0 3 0 3 0 3 0
0 0 2 2 2 0 0
4 3 2 1 2 3 4
0 0 2 2 2 0 0
0 3 0 3 0 3 0
4 0 0 4 0 0 4

And then we add 1 and index into (' ',⌽⍵).

\$\endgroup\$
0
\$\begingroup\$

APL(Dyalog Unicode), 49 47 bytes SBCS

{(⊢⍪1↓⊖)(⊢,0 1↓⌽)⍵@(2/¨⍳a)⊢⍵@a⊢⍉⍵@a⊢''⍴⍨2/a←≢⍵}

Try it on APLgolf!

A dfn submission which takes the string as right argument.

-2 borrowing from Ven's answer.

\$\endgroup\$
0
\$\begingroup\$

Python 3.8 (pre-release), 111 bytes

def f(s):l=len(s);r=range(1-l,l);[print("".join([" ",s[~max(i,-i,j,-j)]][i*j**3==j*i**3]for i in r))for j in r]

Try it online!

How it works :

  • s[~max(i,-i,j,-j)] with j the number of the line and i the number of the column create this pattern
aaaaaaa
abbbbba
abcccba
abcdcba
abcccba
abbbbba
aaaaaaa
  • The condition i*j**3==j*i**3 is True exactly if i = 0 or j = 0 or i = ±j with j the line and i the column. This create this shape:
X  X  X
 X X X 
  XXX  
XXXXXXX
  XXX  
 X X X 
X  X  X
  • [" ", <char> ][ <condition> ] will print the char if the condition is true and will print " " otherwise. The result is then :
a  a  a
 b b b 
  ccc  
abcdcba
  ccc  
 b b b 
a  a  a
  • "".join(... for i in r) will concatenate the previous values to form a line
  • [print(...)for j in r] will print the line for each line
\$\endgroup\$
0
\$\begingroup\$

Perl 5 -F, 73 + 2 = 75 bytes

Scored by system in place at time of question.

pop@F;say$_,reverse+(map{say$;=$"x$b.$_.($"x($#F-$b++).$_)x2;$;,$/}@F),@F

Try it online!

\$\endgroup\$
0
\$\begingroup\$

YASEPL, 111 bytes

=i=1'=f$2=b®1-=t$b=g$`9@!$b-=t!f$5`1!m¥i,1!v$b-i-!s;" ",i~#m;" ",v~#m~#m<!+g}f,t+]7!1~!`2-2!m¥i,1~!+[2>s=g-|9`7
\$\endgroup\$
0
\$\begingroup\$

C (gcc), 121 120 118 bytes

-3 byte thanks to @ceilingcat

main(n,v,i,j)char**v;{for(i=n=strlen(*++v);--i>-n;puts(""))for(j=-n;++j<n;)putchar(i-j&&i+j&&i*j?32:n[~abs(i|j)+*v]);}

Try it online!

\$\endgroup\$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.