# Draw a hollow square of # with given width [closed]

I got this challenge from Codingame and am curious about better solutions than mine:

Given a width via standard input draw a hollow square of '#' in given width and length.

Example:

5 results in

#####
#   #
#   #
#   #
#####


I used python to solve this so i am particulary interested in other python code. But please feel free to post your solution in any language you want.

• What if input is 0 or 1? Nov 7, 2016 at 14:27
• Related, though this might be different enough to not be a dupe. Nov 7, 2016 at 14:31
• Welcome to PPCG! For future questions, I encourage you to use the Sandbox where you can get meaningful feedback on a challenge before posting it to the main page. Nov 7, 2016 at 14:31
• Reading through the answers, I'm not convinced it's a dupe. Most of the answers here (golfing and regular languages) are roughly half the size of the answers on "Print N Squared." Nov 7, 2016 at 16:43
• This is absolutely a duplicate. Solutions from the other challenge can be trivially modified to be valid and competitive here.
– user45941
Nov 8, 2016 at 10:42

# Stuck, 29 27 Bytes

Pretty darn long for a "golfing" language, but I have forgotten how a lot of it works :P

i_2-_u'#*N+_'#' u*'#N+++u*u


Explanation:

i_2-_u                           # take input and triplicate, subtracting 2 (5 -> [3,3,5])
'#*N+_                     # create the top and bottom rows
'#' u*'#N+++u*       # create input - 2 copies of middle rows
u      # rotate left 1 to get correct order, implicit output


# C#, 154 152 bytes

Golfed:

void F(int n){Console.Write($"{new string('#',n)}\n");for(int i=2;i<n;i++)Console.Write($"#{new string(' ',n-2)}#\n");Console.Write(new string('#',n));}


Ungolfed:

    void F(int n)
{
Console.Write($"{new string('#', n)}\n"); for (int i = 2; i < n; i++) Console.Write($"#{new string(' ', n - 2)}#\n");

Console.Write(new string('#', n));
}


EDIT1: Loop range optimization.

## Lithp, 117 bytes

Line split in two for readability:

#N::((var X (repeat "#" N))(print X)(each (seq 3 N) (scope #X::((print (+ "#"
(repeat " " (- N 2)) "#")))))(print X))


Sample usage:

% square.lithp
(
(import "lists")
(def s #N::((var X (repeat "#" N))(print X)(each (seq 3 N) (scope #X::((print (+ "#" (repeat " " (- N 2)) "#")))))(print X)))
(s 10)
)

Output:
\$ ./run square.lithp
##########
#        #
#        #
#        #
#        #
#        #
#        #
#        #
#        #
##########


## dc, 83 bytes

?sd0[1+[#]Pdld>k]sklkx0[1+[
]P0[#]P[1+[ ]Pdld2->x]sxlxxk[#]Pdld2->r]srld2<r[
]P0lkx


This is mostly straightforward. It's kinda late so I won't write up an explanation now, but I might edit one in later.

(Note: I'm new to dc so there are probably obvious improvements to be made here.)

## Python 2, 92 bytes

def f(j):r=range(j);print'\n'.join(''.join(' #'[x*y<1or max(x,y)==j-1]for y in r)for x in r)


# ForceLang, 316 314 bytes

def s set
s g goto
def w io.write "#"
s W io.writeln
if n=0
g 3
if n=1
g 4
label 0
if i=n
g 1
s i 1+i
w
g 0
label 1
s i 0
s m n+-2
W()
label a
if i=m
g b
s i 1+i
s j 0
w
label c
if j=m
g d
s j 1+j
io.write " "
g c
label d
W "#"
g a
label b
s i 0
label 2
if i=n
g 3
s i 1+i
w
g 2
label 4
w
label 3


## T-SQL 351 bytes

This is so wrong... but anyway:

DECLARE @ INT=1,@n INT=10,@a VARCHAR(MAX),@b VARCHAR(MAX),@c CHAR='#',@s VARCHAR(MAX)=''WHILE @<=@n BEGIN SET @a=concat(@a,@c)SET @b=concat(@b,(SELECT CASE WHEN @=1THEN @c WHEN @=@n THEN @c ELSE' 'END))SET @=@+1 END SET @=1 WHILE @<=@n BEGIN SET @s=@s+(SELECT CASE WHEN @=1THEN @a WHEN @=@n THEN CHAR(13)+@a ELSE CHAR(13)+@b END)SET @=@+1 END PRINT @s


Ungolfed:

DECLARE     @ INT=1,
@n INT=10,
@a VARCHAR(MAX),
@b VARCHAR(MAX),
@c CHAR='#',
@s VARCHAR(MAX)=''
WHILE @<=@n
BEGIN
SET @a=concat(@a,@c)
SET @b=concat(@b,(SELECT CASE WHEN @=1THEN @c WHEN @=@n THEN @c ELSE' 'END))
SET @=@+1;
END
SET @=1
WHILE @<=@n
BEGIN
SET @s=@s+
(SELECT
CASE
WHEN @=1THEN @a
WHEN @=@n THEN CHAR(13)+@a
ELSE CHAR(13)+@b
END
)
SET @=@+1
END
PRINT @s


## Python 2, 60656962 61 bytes

x=input();h='#';print h*x+'\n'+(h+' '*(x-2)+h+'\n')*(x-2)+h*x


My first attempt at this problem, I will golf it down hopefully.

+5 bytes I forgot to put newlines between the lines :(
+4 bytes I forgot to add 2 to the last x :(
-7 bytes If I don't subtract 2 from the input at the beginning and subtract it where it would make sense to do so, I can actually save 7 bytes :)
-1 byte Thanks to @Cyoce for helping me save 1 byte by assigning '#' to a variable (which I called h because that's what he was thinking in the comments :P)

Still 1 byte longer than before... :(

• Oops. I meant '#' (I was imagining calling the variable h and my brain goofed Dec 2, 2016 at 1:41
• @Cyoce Oh. Okay, that makes sense. :) It saves one byte, but I needed to add a few bytes to compensate for an overlooked bug. But at least my code is only 61 bytes, not 62. ;) Dec 2, 2016 at 1:50

# BrainF***, 152 bytes

+++++[>++>+++++++<<-],[>>>+>+>+>+<<<<<<-]>>>>[-<<.>>]++++[>>>++++++++<<<-]>--<<[<<.>.>>>[>>.<<-]<<<.>->>>[<+<+>>-]<<[>>+<<-]>--<<]<<.>>>>>[<<<<.>>>>-]


Hopefully this can be golfed a bit more.

Explanation:

+++++[>++>+++++++<<-]           Initializes to [0, 10, 35]. 10 is the charcode for a newline, 35 is the charcode for '#'
,                               Gets the input (as an ASCII char, which is converted to an integer)
[>>>+>+>+>+<<<<<<-]             Copies the input to 4 different spaces
>>>>[-<<.>>]                    Prints the appropriate number of '#'
++++[>>>++++++++<<<-]           Puts 32 in the next available space (32 is the charcode of a space)
>--<<--                         Subtracts 2 from one of the input values and the value on which to loop
[
<<.>.>>>                      Print a newline and a '#'
[>>.<<-]                      Print n - 2 spaces
<<<.>->>>                     Print another '#'
[<+<+>>-]<<[>>+<<-]>--<<      Copy n to two other spaces, then copy it back
]
<<.>>>>>                        Print a newline
[<<<<.>>>>-]                    Print the appropriate number of '#'


Try it here (visualizer) or here (repl)

# Ruby, 66 bytes

n=gets.to_i
puts ?#*n
if n>1
puts [?#+' '*(n-2)+?#]*(n-2),?#*n end


# Common Lisp, SBCL, 64 63 bytes

(format t"#~v@{#~}#
~@*~v@{~@*#~v@t#


For n=1 will give:

##
##


but I guess it's ok, since author's solution doesn't seem to work for n=1 (tested here but maybe it's because it's other Python version?)

Other than that it should work fine.

### Explanation

format  ;printing function
~v@{#~} ;loop n times, where n equals current argument
;printing "#" - gives n "#"s
~@*     ;go back to first argument - reuse (-(read)2)
~v@t    ;add n spaces where n is current argument


# 05AB1E, 6 bytes

'#Ž9¦Λ


Try it online!

'#Ž9¦Λ  # full program
Λ  # draw lines of length...
# implicit input...
Λ  # made from...
'#      # literal...
Λ  # s with directions in [N, NE, E, SE, S, SW, W, NW] with indices in each character in...
Ž9¦   # 2460
# implicit output


# VyxalRj, 15 bytes

₌Ẋ‹%ƛ1c\#*ð∨;∑²


Try it Online!

I'm actually proud of this answer, because it took a bit of thinking and effort to obtain.

## Explained

₌Ẋ‹%ƛ1c\#*ð∨;∑²
₌Ẋ‹             # Push cartesian_product(range(1, input + 1), range(1, input + 1)), input - 1
%            # modulo those
ƛ       ;   # over each item X:
1c         #    does X contain 1?
\#*      #    repeat "#" that many times
ð∨    #    and logical or with space
∑²  # join into a single string and format as a grid


# Dyalog APL, 312924 23 bytes

Tacit Function that takes number of lines as argument, returns hollow square

(' #'⌷⍨1∘+)¨(∘.∨⍨⍳∊1∘,)

## Output (for argument 10)

    (' #'⌷⍨1∘+)¨(∘.∨⍨⍳∊1∘,) 10
##########
#        #
#        #
#        #
#        #
#        #
#        #
#        #
#        #
##########


# Charcoal, 3 bytes

ＢＮ#


Try it online!