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*

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
7
  • 1
    \$\begingroup\$ This gives wrong solution for 1 - " *". I think it should be asterisk without space in front? \$\endgroup\$ Commented Aug 1, 2018 at 15:12
  • \$\begingroup\$ @АндрейЛомакин - From OP: "Leading and trailing whitespace is allowed as long as the output is visually the same." A single star is visually the same as a single star with a space in front of it or at least that was my interpretation ;-) \$\endgroup\$ Commented Aug 1, 2018 at 17:42
  • \$\begingroup\$ But you are actually correct in that I contradicted what I have just said in my answer. I have updated the answer to clarify. Better now? BTW, nice job on finding an old answer and spotting a potential error. Respect. \$\endgroup\$ Commented Aug 1, 2018 at 18:27
  • 1
    \$\begingroup\$ I was attempting this challenge myself, and can't come up with anything better, was studing yours for inspiration. \$\endgroup\$ Commented Aug 1, 2018 at 18:57
  • \$\begingroup\$ I hope my humble effort has helped you. Sure we Wiĺl have some fun golfing together in the future. Enjoy PPCG. I sure do ☺ \$\endgroup\$ Commented Aug 1, 2018 at 22:53

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