Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

Return to Answer

added 114 characters in body
Source Link
Olivier Grégoire
  • 14.5k
  • 3
  • 33
  • 56

Java (JDK 10), 186184 bytes

n->{int h=n+n/7,w=-~h*2+1,i=h+1,w=i*2+1,j,k=0;var o=new char[i][w];for(;i-->0;o[i][w/2]=o[i][w/2+1])for(j=w/2;j-->0;)o[i][j]=o[i][w+~j]=i<h?j<1?32:o[i+1][j-1]:" vibgyor".charAt(k++%8);return o;}

Try it online! Try it online!

n->{ // IntFunction
 int h=n+n/7, // Declare that height = n + n/7
 w=-~h*2+1i=h+1, // that widthindex = (h+1)*2+1
h + 1
 i=h+1, w=i*2+1, // that indexwidth = h + 1(h+1)*2+1
 j, // j
 k=0; // that k = 0
 var o=new char[i][w]; // Declare a 2D char array
 for(; // Loop
 i-->0; // Until i is 0
 o[i][w/2]=o[i][w/2+1] // After each run, copy the middle letter.
 )
 for(j=w/2; // Loop on j = w/2
 j-->0; // Until j = 0
 ) //
 o[i][j] // copy letters to the left side,
 =o[i][w+~j] // and the right side
 =i<h // if it's not the last line
 ?j<1 // if it's the first (and last) character
 ?32 // set it to a space.
 :o[i+1][j-1] // else set it to the previous character on the next line.
 :" vibgyor".charAt(k++%8); // else assign the next letter.
 return o; // return everything
}

Credits

Java (JDK 10), 186 bytes

n->{int h=n+n/7,w=-~h*2+1,i=h+1,j,k=0;var o=new char[i][w];for(;i-->0;o[i][w/2]=o[i][w/2+1])for(j=w/2;j-->0;)o[i][j]=o[i][w+~j]=i<h?j<1?32:o[i+1][j-1]:" vibgyor".charAt(k++%8);return o;}

Try it online!

n->{ // IntFunction
 int h=n+n/7, // Declare that height = n + n/7
 w=-~h*2+1, // that width = (h+1)*2+1
 i=h+1, // that index = h + 1
 j, // j
 k=0; // that k = 0
 var o=new char[i][w]; // Declare a 2D char array
 for(; // Loop
 i-->0; // Until i is 0
 o[i][w/2]=o[i][w/2+1] // After each run, copy the middle letter.
 )
 for(j=w/2; // Loop on j = w/2
 j-->0; // Until j = 0
 ) //
 o[i][j] // copy letters to the left side,
 =o[i][w+~j] // and the right side
 =i<h // if it's not the last line
 ?j<1 // if it's the first (and last) character
 ?32 // set it to a space.
 :o[i+1][j-1] // else set it to the previous character on the next line.
 :" vibgyor".charAt(k++%8); // else assign the next letter.
 return o; // return everything
}

Java (JDK 10), 184 bytes

n->{int h=n+n/7,i=h+1,w=i*2+1,j,k=0;var o=new char[i][w];for(;i-->0;o[i][w/2]=o[i][w/2+1])for(j=w/2;j-->0;)o[i][j]=o[i][w+~j]=i<h?j<1?32:o[i+1][j-1]:" vibgyor".charAt(k++%8);return o;}

Try it online!

n->{ // IntFunction
 int h=n+n/7, // Declare that height = n + n/7
 i=h+1, // that index = h + 1
 w=i*2+1, // that width = (h+1)*2+1
 j, // j
 k=0; // that k = 0
 var o=new char[i][w]; // Declare a 2D char array
 for(; // Loop
 i-->0; // Until i is 0
 o[i][w/2]=o[i][w/2+1] // After each run, copy the middle letter.
 )
 for(j=w/2; // Loop on j = w/2
 j-->0; // Until j = 0
 ) //
 o[i][j] // copy letters to the left side,
 =o[i][w+~j] // and the right side
 =i<h // if it's not the last line
 ?j<1 // if it's the first (and last) character
 ?32 // set it to a space.
 :o[i+1][j-1] // else set it to the previous character on the next line.
 :" vibgyor".charAt(k++%8); // else assign the next letter.
 return o; // return everything
}

Credits

added 1442 characters in body
Source Link
Olivier Grégoire
  • 14.5k
  • 3
  • 33
  • 56

Explanation

n->{ // IntFunction
 int h=n+n/7, // Declare that height = n + n/7
 w=-~h*2+1, // that width = (h+1)*2+1
 i=h+1, // that index = h + 1
 j, // j
 k=0; // that k = 0
 var o=new char[i][w]; // Declare a 2D char array
 for(; // Loop
 i-->0; // Until i is 0
 o[i][w/2]=o[i][w/2+1] // After each run, copy the middle letter.
 )
 for(j=w/2; // Loop on j = w/2
 j-->0; // Until j = 0
 ) //
 o[i][j] // copy letters to the left side,
 =o[i][w+~j] // and the right side
 =i<h // if it's not the last line
 ?j<1 // if it's the first (and last) character
 ?32 // set it to a space.
 :o[i+1][j-1] // else set it to the previous character on the next line.
 :" vibgyor".charAt(k++%8); // else assign the next letter.
 return o; // return everything
}

Explanation

n->{ // IntFunction
 int h=n+n/7, // Declare that height = n + n/7
 w=-~h*2+1, // that width = (h+1)*2+1
 i=h+1, // that index = h + 1
 j, // j
 k=0; // that k = 0
 var o=new char[i][w]; // Declare a 2D char array
 for(; // Loop
 i-->0; // Until i is 0
 o[i][w/2]=o[i][w/2+1] // After each run, copy the middle letter.
 )
 for(j=w/2; // Loop on j = w/2
 j-->0; // Until j = 0
 ) //
 o[i][j] // copy letters to the left side,
 =o[i][w+~j] // and the right side
 =i<h // if it's not the last line
 ?j<1 // if it's the first (and last) character
 ?32 // set it to a space.
 :o[i+1][j-1] // else set it to the previous character on the next line.
 :" vibgyor".charAt(k++%8); // else assign the next letter.
 return o; // return everything
}
Source Link
Olivier Grégoire
  • 14.5k
  • 3
  • 33
  • 56

Java (JDK 10), 186 bytes

n->{int h=n+n/7,w=-~h*2+1,i=h+1,j,k=0;var o=new char[i][w];for(;i-->0;o[i][w/2]=o[i][w/2+1])for(j=w/2;j-->0;)o[i][j]=o[i][w+~j]=i<h?j<1?32:o[i+1][j-1]:" vibgyor".charAt(k++%8);return o;}

Try it online!

Prints an extra leading and trailing space for each multiple of 7.

AltStyle によって変換されたページ (->オリジナル) /