1
\$\begingroup\$

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: ')))

\$\endgroup\$
2
\$\begingroup\$

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!

\$\endgroup\$
2
\$\begingroup\$

Charcoal, 24 bytes

F⁴«×o⁺÷Iθ⁴﹪÷﹪Iθ⁴X²↔⊖鲶↷

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

F⁴«

Loop over the four sides of the rounded rectangle.

×o⁺

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

÷Iθ⁴

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

﹪÷﹪Iθ⁴X²↔⊖ι²

... 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.

\$\endgroup\$
0
\$\begingroup\$

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)}`)
\$\endgroup\$
  • \$\begingroup\$ 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 \$\endgroup\$ – tsh Jun 29 '18 at 7:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.