# Challenge

Given an integer greater or equal to 4, n, print a rounded rectangle of as close as possible (with a gap of 1) sides and a perimeter of n characters.

# Rules

• n is always 4 or greater, because otherwise the output wouldn't be a square
• The characters for the perimeter can be any non-whitespace character
• The rectangle should have equal sides when possible
• When not possible, the rectangle can be up to one character taller or wider than its other side
• The rectangle should have one whitespace character in each corner
• The rectangle can only ever have a gap of 1 (excluding corners) along the entire perimeter
• The gap can be on any side and any position along the perimeter

# Rules

Least amount of bytes wins!

# Examples

Input: 4
Output:
o
o o
o

Input: 5
Output:
oo
o  o
o

Input: 8
Output:
oo
o  o
o  o
oo

Input: 21
Output:
oooooo
o      o
o      o
o      o
o      o
o      o
ooooo


Here is the python3 code I used to generate the above examples:

import math

ch = 'o'

def print_rect(p):
side_length = math.ceil(p / 4)
height = side_length
remainder = p - side_length * 3
p = side_length + 2

if side_length - remainder >= 2 or not remainder:
remainder += 2
height -= 1

lines = [(ch * side_length).center(p)]
for i in range(height):
lines.append(ch + ch.rjust(side_length + 1))
lines.append((ch * remainder).center(p))

print('Output:')
for line in lines:
print(line)

print_rect(int(input('Input: ')))

# Python 2, 91 bytes

def f(n):t=(n+3)/4;c,s='o ';h=(n-t+1)/3;print'\n'.join([s+c*t]+[c+s*t+c]*h+[s+c*(n-t-h-h)])


Try it online!

# Charcoal, 24 bytes

Ｆ⁴«×o⁺÷Ｉθ⁴﹪÷﹪Ｉθ⁴Ｘ²↔⊖ι²¶↷


Try it online! Link is to verbose version of code. Explanation:

Ｆ⁴«


Loop over the four sides of the rounded rectangle.

×o⁺


Print a number of os given by the sum of...

÷Ｉθ⁴


... one quarter of the input as a number, rounded down, and...

﹪÷﹪Ｉθ⁴Ｘ²↔⊖ι²


... add an extra o a) on both horizontal sides if the input has a remainder (modulo 4) of 2 or 3, and b) on the first vertical side if the input is odd.

¶


Omit the corner.

↷


Pivot ready for the next side.

# JavaScript, 143

n=>(x=Math.floor(n/4),q=n%2==1?x+1:x, ${"o".repeat(q)}${Array(n%4==2||n%4==3?x+1:x).fill(o${" ".repeat(q)}o).join\n}${"o".repeat(x)})


• q=n%2==1?x+1:x -> q=n%2+x; .repeat -> [R='repeat']; Array..fill..join -> ...[R]; floor -> (no need, String#repeat would do floor); Tio
– tsh
Commented Jun 29, 2018 at 7:32