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

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Could you make me a hexagon please?

Today, we're going to make an ASCII hexagon. You must write a program or function that takes a positive integer n, and outputs a hexagon grid of size n, made up of asterisks. For example, a hexagon of size 2 looks like this:

 * *
* * *
 * *

While a hexagon of size 3 looks like this:

 * * *
 * * * *
* * * * *
 * * * *
 * * *

You may use any of the default input and output methods, for example STDIO/STDOUT, function arguments and return values or reading/writing a file.

You may assume that input is always valid, so if it's not a positive integer, your program may do whatever you want. You do however have to handle the special case of a size 1 hexagon, which happens to be a single asterisk:

*

Leading and trailing whitespace is allowed as long as the output is visually the same.

Examples:

1:
*
2:
 * *
* * *
 * *
3:
 * * *
 * * * *
* * * * *
 * * * *
 * * *
4:
 * * * *
 * * * * *
 * * * * * *
* * * * * * *
 * * * * * *
 * * * * *
 * * * *
5:
 * * * * *
 * * * * * *
 * * * * * * *
 * * * * * * * *
* * * * * * * * *
 * * * * * * * *
 * * * * * * *
 * * * * * *
 * * * * *
6:
 * * * * * *
 * * * * * * *
 * * * * * * * *
 * * * * * * * * *
 * * * * * * * * * *
* * * * * * * * * * *
 * * * * * * * * * *
 * * * * * * * * *
 * * * * * * * *
 * * * * * * *
 * * * * * *
12:
 * * * * * * * * * * * *
 * * * * * * * * * * * * *
 * * * * * * * * * * * * * *
 * * * * * * * * * * * * * * *
 * * * * * * * * * * * * * * * *
 * * * * * * * * * * * * * * * * *
 * * * * * * * * * * * * * * * * * *
 * * * * * * * * * * * * * * * * * * *
 * * * * * * * * * * * * * * * * * * * *
 * * * * * * * * * * * * * * * * * * * * *
 * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * *
 * * * * * * * * * * * * * * * * * * * * * *
 * * * * * * * * * * * * * * * * * * * * *
 * * * * * * * * * * * * * * * * * * * *
 * * * * * * * * * * * * * * * * * * *
 * * * * * * * * * * * * * * * * * *
 * * * * * * * * * * * * * * * * *
 * * * * * * * * * * * * * * * *
 * * * * * * * * * * * * * * *
 * * * * * * * * * * * * * *
 * * * * * * * * * * * * *
 * * * * * * * * * * * *

As usual, this is , so standard loopholes apply, and you should try to write the shortest possible program measured in bytes. Of course, some languages are inherently shorter or longer than others, so remember that the goal is not necessarily to have the shortest overall byte count, but to beat submissions in the same or similar languages.

May the best golfer win!

Answer*

Draft saved
Draft discarded
Cancel
1
  • \$\begingroup\$ Palindromise is now , so this is 7 bytes: Try it Online! \$\endgroup\$ Commented Jul 14, 2023 at 14:27

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