24
\$\begingroup\$

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.

\$\endgroup\$
13
  • 8
    \$\begingroup\$ What if input is 0 or 1? \$\endgroup\$
    – Karl Napf
    Nov 7, 2016 at 14:27
  • 8
    \$\begingroup\$ Related, though this might be different enough to not be a dupe. \$\endgroup\$ Nov 7, 2016 at 14:31
  • 3
    \$\begingroup\$ 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. \$\endgroup\$ Nov 7, 2016 at 14:31
  • 4
    \$\begingroup\$ 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." \$\endgroup\$ Nov 7, 2016 at 16:43
  • 1
    \$\begingroup\$ This is absolutely a duplicate. Solutions from the other challenge can be trivially modified to be valid and competitive here. \$\endgroup\$
    – user45941
    Nov 8, 2016 at 10:42

45 Answers 45

1
2
0
\$\begingroup\$

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
\$\endgroup\$
0
\$\begingroup\$

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.

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

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
##########
#        #
#        #
#        #
#        #
#        #
#        #
#        #
#        #
##########
\$\endgroup\$
0
\$\begingroup\$

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

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

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)
\$\endgroup\$
0
\$\begingroup\$

ForceLang, 316 314 bytes

def s set
s n io.readnum()
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
\$\endgroup\$
0
\$\begingroup\$

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
\$\endgroup\$
0
\$\begingroup\$

Python 2, 60 65 69 62 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... :(

\$\endgroup\$
2
  • \$\begingroup\$ Oops. I meant '#' (I was imagining calling the variable h and my brain goofed \$\endgroup\$
    – Cyoce
    Dec 2, 2016 at 1:41
  • \$\begingroup\$ @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. ;) \$\endgroup\$
    – hyper-neutrino
    Dec 2, 2016 at 1:50
0
\$\begingroup\$

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)

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

Ruby, 66 bytes

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

Common Lisp, SBCL, 64 63 bytes

(format t"#~v@{#~}#
~@*~v@{~@*#~v@t#
~}~@*#~v{#~}#"(-(read)2)1)

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
\$\endgroup\$
0
\$\begingroup\$

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
\$\endgroup\$
0
\$\begingroup\$

Vyxal Rj, 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
\$\endgroup\$
0
\$\begingroup\$

Dyalog APL, 31 29 24 23 bytes

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

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

Output (for argument 10)

    (' #'⌷⍨1∘+)¨(∘.∨⍨⍳∊1∘,) 10
##########
#        #
#        #
#        #
#        #
#        #
#        #
#        #
#        #
##########
\$\endgroup\$
0
\$\begingroup\$

Charcoal, 3 bytes

BN#

Try it online!

\$\endgroup\$
0
1
2

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