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*

Regular ASCII Polygons

There are 4 regular polygons that we can construct using ASCII art:

 . . .
 . . . . . . .
 . . . . . . . . . . . . .
 . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
 . . . . . . .
 . . .

That is, a triangle, a square, a hexagon and an octagon.

Your task is to take two inputs and produce the corresponding ASCII art polygon.

Input

The two inputs will be:

  • A string/list of printable ASCII characters (i.e. with code points in the range \33ドル\$ (!) to \126ドル\$ (~)
  • An indicator as to which polygon you should output. This indicator can be any \4ドル\$ distinct and consistent values (e.g. 1234, ABCD, etc.), but each must be only one character long, to avoid any exploitation of this input.

The string will be a perfect fit for the polygon, meaning that, if the shape to be outputted is:

The string may have repeated characters, but will always fit the appropriate lengths for the given shape/sequence, so there is no need to worry about too many/few characters.

Output

You should output a shape, indicated by the second input, in the format shown at the top of the challenge with the . replaced with the characters in the inputted string. Each character must be separated by a single space, and leading/trailing whitespace is acceptable, so long as the shape is preserved. The characters may be used in any order, but each character in the string must only appear once. For example, for a hexagon using ABCDABC, the following are valid:

 A B B A
C D A A B C
 B C D C

as each character from the string is used once. However,

 A A A B AD
A A A A B C ABC
 A A C A BC

are not valid (the first only has A, the second doesn't have a D and the third doesn't have spaces between characters).

This is so the shortest code in bytes wins

Test cases

These test cases use T, S, H and O to indicate which shape the output should be in. The string of characters is on the first line and the indicator on the second

i1+K8{^wsW
O
 i 1
+ K 8
{ ^ w
 s W
,r1~vi4l{W@+r<vzP;mq>:8gJcgg$j`$RFhDV
H
 , r 1 ~
 v i 4 l {
 W @ + r < v
z P ; m q > :
 8 g J c g g
 $ j ` $ R
 F h D V
0J++
S
0 J
+ +
Kn[Vyj
T
 K
 n [
V y j
#5~m
S
# 5
~ m
'*g(AUFV~Ou2n=172s'|S11q&j=+#
O
 ' * g
 ( A U F
V ~ O u 2
n = 1 7 2
s ' | S 1
 1 q & j
 = + #

Answer*

Draft saved
Draft discarded
Cancel
2
  • \$\begingroup\$ You can use an unnamed function as a submission here, so you can remove the f= for -2 bytes: Try it online! \$\endgroup\$ Commented Oct 29, 2019 at 0:15
  • \$\begingroup\$ @cairdcoinheringaahing Thank you for allowing this :) \$\endgroup\$ Commented Oct 29, 2019 at 10:32

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