Skip to main content
-1 byte (ngn)
Source Link

Dyalog APL, 41 3939 38 bytes

↑{⌽(⌽,⊃,A↑⊢)⍵↑A⍴' vibgyor'}¨-⍳A←⌈⎕×1+÷7⍳A←⌈⎕×8÷7 

Try it online!Try it online!

A similar approach to others: A←⌈⎕×1+÷7A←⌈⎕×8÷7 finds the height of the rainbow (also the width of the longest 'half row' to the left/right of the centre) and assigns it to A for later use, while ¨-⍳ iterates through the values 1..A, negating them to select on the correct side when used with .

A⍴' vibgyor' generates a 'half row' and ⍵↑ selects the correct length substring. (⌽,⊃,A↑⊢) generates the full row in reverse (which takes fewer characters to do), starting with a reversed half row (), then the centre character taken from the beginning of the half row string () and finally a right padded version of the half row (A↑⊢). The final reverses the row into the correct orientation and turns the vector of rows into a 2D array.

Edit: -2 thanks to dzaima

Edit: -1 thanks to ngn

Dyalog APL, 41 39 bytes

↑{⌽(⌽,⊃,A↑⊢)⍵↑A⍴' vibgyor'}¨-⍳A←⌈⎕×1+÷7 

Try it online!

A similar approach to others: A←⌈⎕×1+÷7 finds the height of the rainbow (also the width of the longest 'half row' to the left/right of the centre) and assigns it to A for later use, while ¨-⍳ iterates through the values 1..A, negating them to select on the correct side when used with .

A⍴' vibgyor' generates a 'half row' and ⍵↑ selects the correct length substring. (⌽,⊃,A↑⊢) generates the full row in reverse (which takes fewer characters to do), starting with a reversed half row (), then the centre character taken from the beginning of the half row string () and finally a right padded version of the half row (A↑⊢). The final reverses the row into the correct orientation and turns the vector of rows into a 2D array.

Edit: -2 thanks to dzaima

Dyalog APL, 41 39 38 bytes

↑{⌽(⌽,⊃,A↑⊢)⍵↑A⍴' vibgyor'}¨-⍳A←⌈⎕×8÷7 

Try it online!

A similar approach to others: A←⌈⎕×8÷7 finds the height of the rainbow (also the width of the longest 'half row' to the left/right of the centre) and assigns it to A for later use, while ¨-⍳ iterates through the values 1..A, negating them to select on the correct side when used with .

A⍴' vibgyor' generates a 'half row' and ⍵↑ selects the correct length substring. (⌽,⊃,A↑⊢) generates the full row in reverse (which takes fewer characters to do), starting with a reversed half row (), then the centre character taken from the beginning of the half row string () and finally a right padded version of the half row (A↑⊢). The final reverses the row into the correct orientation and turns the vector of rows into a 2D array.

Edit: -2 thanks to dzaima

Edit: -1 thanks to ngn

added 41 characters in body; deleted 5 characters in body
Source Link

Dyalog APL, 4141 39 bytes

{⍕⍪{⌽(⌽,⊃,A↑⊢)⍵↑A⍴' vibgyor'}¨-⍳A←⍵+⌈⍵÷7}⍳A←⌈⎕×1+÷7 

Try it online!Try it online!

A similar approach to others: A←⍵+⌈⍵÷7A←⌈⎕×1+÷7 finds the height of the rainbow (also the width of the longest 'half row' to the left/right of the centre) and assigns it to A for later use, while ¨-⍳ iterates through the values 1..A, negating them to select on the correct side when used with .

A⍴' vibgyor' generates a 'half row' and ⍵↑ selects the correct length substring. (⌽,⊃,A↑⊢) generates the full row in reverse (which takes fewer characters to do), starting with a reversed half row (), then the centre character taken from the beginning of the half row string () and finally a right padded version of the half row (A↑⊢). The final reverses the row into the correct orientation.

The and takesturns the vector of rows and reshapes it into a column vector, and formats the resultant vector as a string2D array.

Edit: -2 thanks to dzaima

Dyalog APL, 41 bytes

{⍕⍪{⌽(⌽,⊃,A↑⊢)⍵↑A⍴' vibgyor'}¨-⍳A←⍵+⌈⍵÷7} 

Try it online!

A similar approach to others: A←⍵+⌈⍵÷7 finds the height of the rainbow (also the width of the longest 'half row' to the left/right of the centre) and assigns it to A for later use, while ¨-⍳ iterates through the values 1..A, negating them to select on the correct side when used with .

A⍴' vibgyor' generates a 'half row' and ⍵↑ selects the correct length substring. (⌽,⊃,A↑⊢) generates the full row in reverse (which takes fewer characters to do), starting with a reversed half row (), then the centre character taken from the beginning of the half row string () and finally a right padded version of the half row (A↑⊢). The final reverses the row into the correct orientation.

The takes the vector of rows and reshapes it into a column vector, and formats the resultant vector as a string.

Dyalog APL, 41 39 bytes

{⌽(⌽,⊃,A↑⊢)⍵↑A⍴' vibgyor'}¨-⍳A←⌈⎕×1+÷7 

Try it online!

A similar approach to others: A←⌈⎕×1+÷7 finds the height of the rainbow (also the width of the longest 'half row' to the left/right of the centre) and assigns it to A for later use, while ¨-⍳ iterates through the values 1..A, negating them to select on the correct side when used with .

A⍴' vibgyor' generates a 'half row' and ⍵↑ selects the correct length substring. (⌽,⊃,A↑⊢) generates the full row in reverse (which takes fewer characters to do), starting with a reversed half row (), then the centre character taken from the beginning of the half row string () and finally a right padded version of the half row (A↑⊢). The final reverses the row into the correct orientation and turns the vector of rows into a 2D array.

Edit: -2 thanks to dzaima

Source Link

Dyalog APL, 41 bytes

{⍕⍪{⌽(⌽,⊃,A↑⊢)⍵↑A⍴' vibgyor'}¨-⍳A←⍵+⌈⍵÷7} 

Try it online!

A similar approach to others: A←⍵+⌈⍵÷7 finds the height of the rainbow (also the width of the longest 'half row' to the left/right of the centre) and assigns it to A for later use, while ¨-⍳ iterates through the values 1..A, negating them to select on the correct side when used with .

A⍴' vibgyor' generates a 'half row' and ⍵↑ selects the correct length substring. (⌽,⊃,A↑⊢) generates the full row in reverse (which takes fewer characters to do), starting with a reversed half row (), then the centre character taken from the beginning of the half row string () and finally a right padded version of the half row (A↑⊢). The final reverses the row into the correct orientation.

The takes the vector of rows and reshapes it into a column vector, and formats the resultant vector as a string.