Draw a hollow square of # with given width

21

2

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.

sebingel

Posted 2016-11-07T14:25:10.217

Reputation: 349

7What if input is 0 or 1? – Karl Napf – 2016-11-07T14:27:35.947

8Related, though this might be different enough to not be a dupe. – AdmBorkBork – 2016-11-07T14:31:20.200

3

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.

– AdmBorkBork – 2016-11-07T14:31:59.147

@KarlNapf Run his test code and see what the result is. – mbomb007 – 2016-11-07T14:32:04.263

@TimmyD Hardly necessary, in this case. – Shaun Wild – 2016-11-07T14:39:15.633

Partially related – Kevin Cruijssen – 2016-11-07T14:50:35.767

4Reading 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." – AdmBorkBork – 2016-11-07T16:43:11.517

Does "via standard input" in "Given a width via standard input" mean that we have to read from stdin or that we can use any of PPCG's standard input methods?

– nimi – 2016-11-07T19:04:08.050

@ElPedro The post has been reopened for the past several hours. – AdmBorkBork – 2016-11-07T20:18:34.917

Yes, and I left my code at work because I wrote it in my lunch break and now I have to wait until tomorrow to post by which time it may have been closed again. Just saying, if answers have already been posted and others may be working on an alternative solution is it right to close it and so waste their time? Just a question. Any opinions welcome. I'm not saying that I am right. – ElPedro – 2016-11-07T20:31:10.373

1This is absolutely a duplicate. Solutions from the other challenge can be trivially modified to be valid and competitive here. – Mego – 2016-11-08T10:42:21.927

Answers

1

Pyke, 11 bytes

ttDd*n+*.X#

Try it here!

ttDd*n+*    - a square of spaces n-2*n-2 big
        .X# - surround in `#`

Blue

Posted 2016-11-07T14:25:10.217

Reputation: 26 661

12

Charcoal, 6 bytes

Code:

NβBββ#

Explanation:

Nβ        # Get input from the command line and store into β
   B      # Draw a hollow box with...
     β     #  Width β
      β    #  Height β
       #   #  Filled with the character '#'
           # Implicitly output the box

Try it online!

Adnan

Posted 2016-11-07T14:25:10.217

Reputation: 41 965

1I was trying to figure out how to read input in Charcoal. Now I know :) – Emigna – 2016-11-07T15:22:14.683

1@Emigna Note that can also be used in an expression, like int(input()) in Python. If this challenge were "draw a hollow rectangle with given width and height," the solution could be BNN#. – DLosc – 2016-11-08T01:50:53.433

Does Charcoal use a non-UTF8 charset? – OldBunny2800 – 2016-11-08T02:49:55.670

That looks like 6 characters, not 6 bytes. β is in plenty of alternate 8-bit character sets, but I'm dubious about N(which is not N) – Sparr – 2016-11-08T03:22:41.167

3@Sparr Charcoal uses its own codepage. – Conor O'Brien – 2016-11-08T04:25:28.747

That seems... silly. – Sparr – 2016-11-08T04:58:29.083

@Sparr But it's legal, and used in lots of other languages, like Jelly. – Fund Monica's Lawsuit – 2016-11-08T05:46:36.843

8

MATL, 12 bytes

:G\1>&*~35*c

Try it online!

Explanation

:     % Input n implicitly. Push range [1 2 ... n]
      % STACK: [1 2 3 4 5]
G     % Push n again
      % STACK: [1 2 3 4 5], 5
\     % Modulo
      % STACK: [1 2 3 4 0]
1>    % Does each entry exceed 1?
      % STACK: [0 1 1 1 0]
&*    % Matrix with all pair-wise products
      % STACK: [0 0 0 0 0;
                0 1 1 1 0;
                0 1 1 1 0;
                0 1 1 1 0;
                0 0 0 0 0]
~     % Negate
      % STACK: [1 1 1 1 1;
                1 0 0 0 1;
                1 0 0 0 1;
                1 0 0 0 1;
                1 1 1 1 1]
35*   % Multiply by 35
      % STACK: [35 35 35 35 35;
                35  0  0  0 35;
                35  0  0  0 35;
                35  0  0  0 35;
                35 35 35 35 35]
c     % Convert to char. 0 is interpreted as space. Display implicitly
      % STACK: ['#####';
                '#   #';
                '#   #';
                '#   #';
                '#####']

Luis Mendo

Posted 2016-11-07T14:25:10.217

Reputation: 87 464

6

Python 2, 62 54 bytes

f=lambda n:'#'*n+'\n#%s#'%(' '*(n-2))*(n-2)+'\n'+'#'*n

Returns #\n# when the input is 1

55 Bytes version that prints

def f(n):a=n-2;print'#'*n,'\n#%s#'%(' '*a)*a,'\n'+'#'*n

62 Bytes version that works for any input:

f=lambda n:'#'*n+'\n#%s#'%(' '*(n-2))*(n-2)+('\n'+'#'*n)*(n>1)

Rod

Posted 2016-11-07T14:25:10.217

Reputation: 17 588

2You do not have to say f= unless you use it––which you don't. – Daniel – 2016-11-07T18:57:26.370

@Dopapp I know, but I think it's more fair that way (in comparison to full functions / programs) – Rod – 2016-11-08T11:25:54.510

@Rod Your choice, but we have a policy about anonymous functions that allows them. – Erik the Outgolfer – 2016-11-08T12:50:43.793

6

Jolf, 8 bytes

,ajj"###
,ajj      draw a box with height (input) and width (input)
    "###  with a hash border

Conor O'Brien

Posted 2016-11-07T14:25:10.217

Reputation: 36 228

The right tool for the job :) – Emigna – 2016-11-07T15:05:49.573

Out of curiosity, why are three # required? – Kevin Cruijssen – 2016-11-07T15:54:49.833

3@KevinCruijssen Each specify the horizontal struts, vertical struts, and corner pieces. – Conor O'Brien – 2016-11-07T16:50:57.870

5

COW, 426 405 348 330 bytes

MoOMoOMoOMoOMoOMoOMoOMoOMoOMoOMMMmoOMMMMoOMoOMoOMoOMoOMoOMoOMoOMoO
MoOMoOMoOMoOMoOMoOMoOMoOMoOMoOMoOMoOMoOMMMmoOMMMMoOMoOMoOmoOoomMMM
moOMMMMOOmOomOoMoomoOmoOMOomoomOoMMMmoOMMMMOoMOoMOOmOomOomOomOoMoo
moOmoOMoomoOMMMmoOmoOMMMMOoMOoMOOmOomOomOomOoMoomoOmoOmoOmoOMOomoo
mOomOomOoMoomoOmoOMOomoomOomOomOomOoMoomoOmoOmoOMOOmOoMoomoOMOomoo

Try it online! Change the number in the second line to any number to change the output.

The COW interpreter that I'm using here was written in Perl (and is newer than this challenge), but you can still get the same result by inputting the code here.

Explanation

; Note: [n] means "value stored in the nth block of memory".
MoOMoOMoOMoOMoOMoOMoOMoOMoOMoO                                                  ;Stores 10 in [0].  10 is the code point for carriage return
MMMmoOMMMMoOMoOMoOMoOMoOMoOMoOMoOMoOMoOMoOMoOMoOMoOMoOMoOMoOMoOMoOMoOMoOMoO     ;Stores 32 in [1].  32 is the code point for whitespace
MMMmoOMMMMoOMoOMoO                                                              ;Stores 35 in [2].  35 is the code point for #
moOoom                                                                          ;Reads STDIN for an integer, and stores it in [3]
MMMmoOMMM                                                                       ;Copies [3] into [4] 
MOO                                                                             ;Loop as long as [4] is non-zero
    mOomOoMoo                                                                   ;Navigate to [2] and print the character with that code point
    moOmoOMOo                                                                   ;Navigate to [4] and decrement
moo                                                                             ;End loop
mOoMMMmoOMMMMOoMOo                                                              ;Copy [3] into [4] and decrement [4] twice
MOO                                                                             ;Loop as long as [4] is non-zero
    mOomOomOomOoMoo                                                             ;Navigate to [0] and print the character with that code point
    moOmoOMoo                                                                   ;Navigate to [2] and print the character with that code point
    moOMMMmoOmoOMMMMOoMOo                                                       ;Navigate to [3] and copy it into [5], then decrement [5] twice
    MOO                                                                         ;Loop as long as [5] is non-zero
        mOomOomOomOoMoo                                                         ;Navigate to [1] and print the character with that code point
        moOmoOmoOmoOMOo                                                         ;Navigate to [5] and decrement
    moo                                                                         ;End loop
    mOomOomOoMoo                                                                ;Navigate to [2] and print the character with that code point
    moOmoOMOo                                                                   ;Navigate to [4] and decrement
moo                                                                             ;End loop
mOomOomOomOoMoo                                                                 ;Navigate to [0] and print the character with that code point
moOmoOmoO                                                                       ;Navigate to [3]
MOO                                                                             ;Loop as long as [3] is non-zero
    mOoMoo                                                                      ;Navigate to [2] and print the character with that code point
    moOMOo                                                                      ;Navigate to [3] and decrement
moo                                                                             ;End loop

Gabriel Benamy

Posted 2016-11-07T14:25:10.217

Reputation: 2 827

4

Java 7, 113 112 110 bytes

String c(int n){String r="";for(int i=n,j;i-->0;r+="\n")for(j=0;j<n;r+=i*j<1|n-i<2|n-j++<2?"#":" ");return r;}

1 byte saved thanks to @OlivierGrégoire;
2 bytes saved thanks to @cliffroot.

Derived solution based on my Creating a Crossed Square answer.

Try it here.

Kevin Cruijssen

Posted 2016-11-07T14:25:10.217

Reputation: 67 575

1Could you shave a byte by doing the following for(int i=n,j;i-->0;r+="\n")? Since we don't care which is the bottom line or the top one, it doesn't make any sense to keep that order, right? – Olivier Grégoire – 2016-11-08T12:45:45.947

1@OlivierGrégoire Thanks! I've also edited it in my Creating a Crossed Square answer, since there the same could be done. – Kevin Cruijssen – 2016-11-08T12:56:08.487

4

PowerShell v2+, 48 47 bytes

param($n)($z='#'*$n--);,("#$(' '*--$n)#")*$n;$z

-1 byte thanks to JohnLBevan

Takes input $n, sets $z as $n hashmarks, with $n post-decremented. Encapsulates that in parens to place a copy on the pipeline. Then uses the comma operator to create an array of pre-decremented $n lines of #,spaces,#. Those are left on the pipeline. Then places $z again on the pipeline. Output via implicit Write-Output at the end introduces a newline between elements, so we get that for free.

Since the OP's code doesn't work for input n <= 1, I took that to mean we don't need to support input 1, either.

Examples

PS C:\Tools\Scripts\golfing> 2..6|%{"$_";.\draw-a-hollow-square.ps1 $_;""}
2
##
##

3
###
# #
###

4
####
#  #
#  #
####

5
#####
#   #
#   #
#   #
#####

6
######
#    #
#    #
#    #
#    #
######

AdmBorkBork

Posted 2016-11-07T14:25:10.217

Reputation: 41 581

You can knock another byte off: param($n)($z='#'*$n--);,("#$(' '*--$n)#")*$n;$z – JohnLBevan – 2016-11-08T19:51:14.590

1@JohnLBevan Good idea with the script block. Thanks! – AdmBorkBork – 2016-11-08T19:52:38.123

4

Python 2, 59 58 bytes

n=i=input()
while i:print'#%s#'%((' #'[i%n<2])*(n-2));i-=1

repl.it

Note: An input of 1 produces an output of ##, but a hollow square would never be produced for an input less than 3, so I guess this is fine.

Jonathan Allan

Posted 2016-11-07T14:25:10.217

Reputation: 67 804

3

C, 98 bytes

f(n,i){i=n*(n+1);while(i--){putchar(i%(n+1)==n?10:i<n||i>n*n-1||i%(n+1)==0||i%(n+1)==n-1?35:32);}}

Usage:

f(5)

Karl Napf

Posted 2016-11-07T14:25:10.217

Reputation: 4 131

3

05AB1E, 20 bytes

'#×D¹Íð×'#.ø¹Í×sJ¹ä»

Try it online!

Or 18 bytes if we can ignore 1 <= n:

F„ #N¹<%_è¹Í×'#.ø,

Try it online!

Emigna

Posted 2016-11-07T14:25:10.217

Reputation: 50 798

3

JavaScript, 61 58 bytes

Saved 3 bytes thanks to @lmis!

n=>(b='#'[r='repeat'](n))+`
#${' '[r](n-=2)}#`[r](n)+`
`+b

(Doesn't handle 0 or 1)

For 13 extra bytes (at 71 bytes), you can!

n=>n?n-1?(b='#'[r='repeat'](n))+`
#${' '[r](n-=2)}#`[r](n)+`
`+b:'#':''

These solutions are fairly simple: they do a lot of storage to not repeat themselves to save a few bytes. Unminified without the variablsm it would look like:

n => // Anonymous function definition (Param `n` is the size)
    '#'.repeat(n) +      // # `n` times to form the top
    `
#${' '.repeat(n - 2)}#`  // Followed by a newline followed by a hash and `n` - 2 spaces and
                         // another hash to make one of the middle lines
    .repeat(n - 2) +     // The above middle lines repeated `n` - 2 times
    '#'.repeat(n)        // Followed by the top line again

Try it!

<script type="text/babel">var f=n=>n?n-1?(b='#'[r='repeat'](n))+`\n#${' '[r](n-=2)}#`[r](n)+`\n`+b:'#':'',b,r;function c(){document.getElementById('pre').textContent = f(+document.getElementById('input').value);}</script><input id="input" onkeydown="c();" onkeyup="c();" onchange="c();" onclick="c();" placeholder="Size"><pre id="pre"></pre>

Artyer

Posted 2016-11-07T14:25:10.217

Reputation: 1 697

By adding !n?'':n==1?'#':, an extra 15 bytes at the beginning of the function body, you can handle inputs 0 and 1. – Kayla – 2016-11-08T05:12:16.090

1n=>(b='#'[r='repeat'](n)) and then #${" "[r](n-=2)} etc. saves you 3 bytes by avoiding to repeat repeat :) – Lmis – 2016-11-08T10:15:57.827

3

WinDbg, 206 200 182 170 bytes

.if@$t0{r$t3=2000000;f@$t3 L@$t0 23;f2*@$t3 L@$t0 20;eb2*@$t3 23;eb2*@$t3+@$t0-1 23;da@$t3 L@$t0;j1<@$t0'.for(r$t1=@$t0-2;@$t1;r$t1=@$t1-1){da2*@$t3 L@$t0};da@$t3 L@$t0'}

-6 bytes from removing parens from .if and using j instead of second .if

-18 bytes by using f instead of a .for to construct the strings.

-12 bytes by not NULL-terminating strings, instead passing length to da

Input is passed in through the pseudo-register $t0 (eg r $t0 = 5; {above-code}).

Explanation:

.if @$t0                                                *Verify width($t0) at least 1 
{                                                       *(registers have unsigned values) 
    r $t3 = 2000000;                                    *Set $t3 to address where the 
                                                        *string will be constructed
    f @$t3 L@$t0 23;                                    *Put width($t0) '#' at 2000000($t3)
    f 2 * @$t3 L@$t0 20;                                *Put width($t0) ' ' at 4000000(2*$t3)
    eb 2 * @$t3 23;                                     *Put '#' on left of ' ' string
    eb 2 * @$t3 + @$t0 - 1 23;                          *Put '#' on right of ' ' string
    da @$t3 L@$t0;                                      *Print the top of the box
    j 1 < @$t0                                          *If width($t1) at least 2
    '
        .for (r $t1 = @$t0 - 2; @$t1; r $t1 = @$t1 - 1) *Loop width($t0)-2 times to...
        {
            da 2 * @$t3 L@$t0                           *...print the sides of the box
        };
        da @$t3 L@$t0                                   *Print the bottom of the box
    '
}

Sample output:

0:000> r$t0=0
0:000> .if@$t0{r$t3=2000000;f@$t3 L@$t0 23;f2*@$t3 L@$t0 20;eb2*@$t3 23;eb2*@$t3+@$t0-1 23;da@$t3 L@$t0;j1<@$t0'.for(r$t1=@$t0-2;@$t1;r$t1=@$t1-1){da2*@$t3 L@$t0};da@$t3 L@$t0'}

0:000> r$t0=1
0:000> .if@$t0{r$t3=2000000;f@$t3 L@$t0 23;f2*@$t3 L@$t0 20;eb2*@$t3 23;eb2*@$t3+@$t0-1 23;da@$t3 L@$t0;j1<@$t0'.for(r$t1=@$t0-2;@$t1;r$t1=@$t1-1){da2*@$t3 L@$t0};da@$t3 L@$t0'}
Filled 0x1 bytes
Filled 0x1 bytes
02000000  "#"

0:000> r$t0=2
0:000> .if@$t0{r$t3=2000000;f@$t3 L@$t0 23;f2*@$t3 L@$t0 20;eb2*@$t3 23;eb2*@$t3+@$t0-1 23;da@$t3 L@$t0;j1<@$t0'.for(r$t1=@$t0-2;@$t1;r$t1=@$t1-1){da2*@$t3 L@$t0};da@$t3 L@$t0'}
Filled 0x2 bytes
Filled 0x2 bytes
02000000  "##"
02000000  "##"

0:000> r$t0=5
0:000> .if@$t0{r$t3=2000000;f@$t3 L@$t0 23;f2*@$t3 L@$t0 20;eb2*@$t3 23;eb2*@$t3+@$t0-1 23;da@$t3 L@$t0;j1<@$t0'.for(r$t1=@$t0-2;@$t1;r$t1=@$t1-1){da2*@$t3 L@$t0};da@$t3 L@$t0'}
Filled 0x5 bytes
Filled 0x5 bytes
02000000  "#####"
04000000  "#   #"
04000000  "#   #"
04000000  "#   #"
02000000  "#####"

milk

Posted 2016-11-07T14:25:10.217

Reputation: 3 043

2

Python, 109 bytes

n=int(input())
for x in range(n):
 r=list(' '*n);r[0]=r[-1]='#'
 if x%(n-1)==0:r='#'*n
 print("".join(r))

sebingel

Posted 2016-11-07T14:25:10.217

Reputation: 349

1You can replace list(' '*n) with [' ']*n. You can also replace x%(n-1) with x%~-n – Post Rock Garf Hunter – 2016-11-07T14:58:31.900

also, if you turn the for block into a list comprehension you can save more than 20 bytes – Rod – 2016-11-07T15:11:28.067

Also, swith to Python 2, drop int() and the brackets around print. – Artyer – 2016-11-07T19:16:18.230

Use <1 instead of ==0. – mbomb007 – 2016-11-07T19:55:08.307

2

C, 83 82 80 78 77 Bytes

i,j;f(n){for(i=n;i--;puts(""))for(j=n;j--;putchar(i*j&&i^n-1&&j^n-1?32:35));}

Sneak in a multiply and save a byte...

i,j;f(n){for(i=n;i--;puts(""))for(j=n;j--;putchar(i&&j&&i^n-1&&j^n-1?32:35));}

Also count down j and save a few more...

i,j;f(n){for(i=n;i--;puts(""))for(j=0;j++<n;putchar(i&&j^1&&i^n-1&&j^n?32:35));}

Count down i from n to zero and save a few bytes...

i,j;f(n){for(i=0;i++<n;puts(""))for(j=0;j++<n;putchar(i^1&&j^1&&i^n&&j^n?32:35));}

A bit easier to understand and 1 byte more

i,j;f(n){for(i=0;i++<n;puts(""))for(j=0;j++<n;putchar(i==1|i==n|j==1|j==n?35:32));}

cleblanc

Posted 2016-11-07T14:25:10.217

Reputation: 3 360

Do you need && instead of &? – corvus_192 – 2016-11-07T20:56:45.053

Yes, it needs to be logical &. I can use multiply but it requires too many parenthesis... – cleblanc – 2016-11-07T21:23:56.127

2

Ruby, 39 bytes

->n{puts a=?#*n,[?#+' '*(n-=2)+?#]*n,a}

Turns out to be shorter this way than all the fancy stuff I was trying. Be advised that this doesn't handle 0 or 1 at all.

Lee W

Posted 2016-11-07T14:25:10.217

Reputation: 521

2

Python 2, 50 bytes

m=input()-2
for c in'#'+' '*m+'#':print'#'+m*c+'#'

Works for n>=2. Prints each line with a pound sign, n-2 of the appropriate symbol, then another pound sign.

Aliasing the pound symbol gives same length:

m=input()-2;p='#'
for c in p+' '*m+p:print p+m*c+p

Other attempts:

lambda n:'#'*n+('\n#'+' '*(n-2)+'#')*(n-2)+'\n'+'#'*n

lambda n:'#'*n+'\n#%s#'%((n-2)*' ')*(n-2)+'\n'+'#'*n

lambda n:'\n'.join(['#'*n]+['#'+' '*(n-2)+'#']*(n-2)+['#'*n])

n=input();s='#'+' '*(n-2)+'#'
for c in s:print[s,'#'*n][c>' ']

s='##'+' #'*(input()-2)+'##'
for c in s[::2]:print s[c>' '::2]

s='#'+' '*(input()-2)+'#'
for c in s:print s.replace(' ',c)

xnor

Posted 2016-11-07T14:25:10.217

Reputation: 115 687

2

Haskell, 49 bytes

n%b='#':(b<$[3..n])++"#\n"
f n=(n%)=<<init(n%' ')

Works for n>=2. Defines the operation of sandwiching a character between # for an n-character newline-terminated string, then applies it twice to make a 2D grid.

Call like:

*Main> putStrLn$ f 5
#####
#   #
#   #
#   #
#####

xnor

Posted 2016-11-07T14:25:10.217

Reputation: 115 687

1

Groovy, 51 50 bytes

{n->a="*"*n+"\n";n-=2;print(a+"*${' '*n}*\n"*n+a)}

Magic Octopus Urn

Posted 2016-11-07T14:25:10.217

Reputation: 19 422

1

PHP, 81 69 bytes

for($n=-1+$i=$argv[1];$i--;)echo str_pad("#",$n," #"[$i%$n<1]),"#\n";

Run with -r; provide input as argument.

Throws a DivisionByZeroError for input=1.

Titus

Posted 2016-11-07T14:25:10.217

Reputation: 13 814

1

R, 68 70 bytes

Works for n > 1. Thanks to @Billywob for a couple of bytes swapping out the array for a matrix.

cat(rbind(b<-'#',cbind(b,matrix(' ',n<-scan()-2,n),b),b,'
'),sep='')

Uses rbind and cbind to put rows and columns of #'s around an n-2 square matrix of spaces. Newlines are bound to the rows as well. The newline in the source is significant. Input is from STDIN

MickyT

Posted 2016-11-07T14:25:10.217

Reputation: 11 735

Nice! I had no idea that a newline in a string implicitly adds \n. You could save two bytes by using matrix instead of array though. – Billywob – 2016-11-08T08:54:40.120

1

Common Lisp, 150 130 bytes

-20 thanks to @Cyoce and @AlexL.

(defun s(v)(format t"~v,,,vA~%"v #\# #\#)(dotimes(h(- v 2))(format t"~v,,,vA~A~%"(- v 1)#\  #\# #\#))(format t"~v,,,vA"v #\# #\#))

Usage:

* (s 5)
#####
#   #
#   #
#   #
#####

Basically uses format twice for the top and bottom and a loop for the rows in between. The format call for the top and bottom outputs a line starting with # and padded to the appropriate width with #s. The format call for the rows in between works similarly, except the padding is spaces and a # gets printed at the end of the line.

Note: I'm rather new to Lisp and expect to have a lot of room for improvement on this.

artificialnull

Posted 2016-11-07T14:25:10.217

Reputation: 481

Why not name it s? Or do am anonymous function? – Cyoce – 2016-12-02T00:52:25.073

I don't know Lisp, but are all of the spaces between a word and an open bracket to its right necessary? Like, does it have to be dotimes (h (- v 2)) or could it be dotimes(h(- v 2))? – HyperNeutrino – 2016-12-02T02:12:56.030

@AlexL. yeah, there are a lot of opportunities for improvement here. A ton of whitespace can be removed between parentheses and other symbols. I'll just do that really quickly – artificialnull – 2016-12-02T02:23:23.533

0

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)

TFeld

Posted 2016-11-07T14:25:10.217

Reputation: 19 246

0

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

SuperJedi224

Posted 2016-11-07T14:25:10.217

Reputation: 11 342

0

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

Nelz

Posted 2016-11-07T14:25:10.217

Reputation: 321

0

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)

HyperNeutrino

Posted 2016-11-07T14:25:10.217

Reputation: 26 575

0

Ruby, 66 bytes

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

noel

Posted 2016-11-07T14:25:10.217

Reputation: 11

0

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

user65167

Posted 2016-11-07T14:25:10.217

Reputation:

0

Charcoal, 3 bytes (non-competing)

BN#

Try it online!

Erik the Outgolfer

Posted 2016-11-07T14:25:10.217

Reputation: 38 134

0

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

HyperNeutrino

Posted 2016-11-07T14:25:10.217

Reputation: 26 575

Oops. I meant '#' (I was imagining calling the variable h and my brain goofed – Cyoce – 2016-12-02T01:41:25.257

@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. ;) – HyperNeutrino – 2016-12-02T01:50:43.177

0

Haskell, 67 bytes

l#n=l<$[1..n]
f n=unlines$'#'#n:('#':' '#(n-2)++"#")#(n-2)++['#'#n]

Usage example:

Prelude> putStrLn $ f 4
####
#  #
#  #
####

How it works:

l#n=l<$[1..n]                      -- helper function that makes n copies of l

   '#'#n                           -- make a string of n copies of #, followed by
                        #(n-2)     -- n-2 copies of
     '#':' '#(n-2)++"#"            -- # followed by n-2 times spaces, followed by #
                           ['#'#n] -- and a final string with n copies of #
unlines                            -- join with newlines in-between

nimi

Posted 2016-11-07T14:25:10.217

Reputation: 34 639

0

Jelly, 13, bytes

,þ%µỊṀ€€ị⁾# Y

TryItOnline! or try 0 to 15

How?

,þ%µỊṀ€€ị⁾# Y - Main link: n
 þ            - outer product with
,             -    pair:   [[[1,1],[2,1],...,[n,1]],[[1,2],[2,2],...,[n,2]], ... ,[[1,n],[2,n],...,[n,n]]]
  %           - mod n:     [[[1,1],[2,1],...,[0,1]],[[1,2],[2,2],...,[0,2]], ... ,[[1,0],[2,0],...,[0,0]]]
   µ          - monadic chain separation
    Ị         - abs(z)<=1: [[[1,1],[0,1],...,[1,1]],[[1,0],[0,0],...,[1,0]], ... ,[[1,1],[0,1],...,[1,1]]]
      €€      - for each for each
     Ṁ        - maximum:   [[1,    1,    ...,1],    [1,    0,    ..., 1],    ... ,[1,    1,    ..., 1]   ]
        ị     - index into (1 based)
         ⁾#   - "# ":      ["##...#","# ...#", ...,"##...#"]
           Y  - join with line feeds

Jonathan Allan

Posted 2016-11-07T14:25:10.217

Reputation: 67 804

0

Racket 113 bytes

(let*((d display)(g(λ()(for((i n))(d"#")))))(g)(d"\n")(for((i(- n 2)))(d"#")(for((i(- n 2)))(d" "))(d"#\n"))(g))

Ungolfed:

(define (f n)
  (let* ((d display)
         (g (λ () 
              (for ((i n))
                (d "#"))
              (d "\n"))))
    (g)
    (for ((i (- n 2)))
      (d "#")
      (for ((i (- n 2)))
        (d " ") )
      (d "#\n"))
    (g)))

Testing:

(f 5)

Output:

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

rnso

Posted 2016-11-07T14:25:10.217

Reputation: 1 635

0

Pip, 16 bytes

15 bytes of code, +1 for -n flag.

(Y_Xa-2WR'#s)My

Works for input >= 2. Try it online!

Explanation of somewhat ungolfed version

First, we define a function y that takes a string argument, repeats it a-2 times (where a is the first command-line input), and wraps the result in #.

Y _ X a-2 WR '#
  _              Identity function
    X a-2        String-repeated by a-2
          WR '#  Wrapped in #
Y                Yank the resulting function into y

Next, we apply this function twice--once normally, then again with map--to obtain the square as a list of strings:

y M (y s)
    (y s)  Call function y with s (preinitialized to " ") as argument
y M        Map y to each character of the resulting string

For input of 4, (y s) results in "# #" and y M (y s) in ["####"; "# #"; "# #"; "####"]. This latter value is then printed, with the -n flag causing it to be newline-separated.

Golfing tricks

To get from the ungolfed to the golfed version:

  • Remove spaces.
  • Y is an operator, which means we can use it in an expression. Instead of Y... followed by (ys), we can just do (Y...s).
  • The problem is, we have to yank the function before we reference it again as y; so yM(Y_Xa-2WR'#s) won't work. Solution: swap the operands of the Map operator. As long as one of them is a function and the other is an iterable type, it doesn't matter what order they come in.

DLosc

Posted 2016-11-07T14:25:10.217

Reputation: 21 213

0

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.

paldir

Posted 2016-11-07T14:25:10.217

Reputation: 109

0

SpecBAS - 57 bytes

1 INPUT n: a$="#"*n,n-=2,b$="#"+" "*n+"#"#13: ?a$'b$*n;a$

? is shorthand for PRINT, #13 is carriage return which can be tacked on to the end of a string without needing a + to join them.

The apostrophe moves print cursor down one line.

Brian

Posted 2016-11-07T14:25:10.217

Reputation: 1 209

0

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

Kade

Posted 2016-11-07T14:25:10.217

Reputation: 7 463

0

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

Andrakis

Posted 2016-11-07T14:25:10.217

Reputation: 361

0

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

poi830

Posted 2016-11-07T14:25:10.217

Reputation: 1 265