17
\$\begingroup\$

Given n=m^2, return a list of integers that do not border the m x m grid of integers from 1 to n.

Examples

n=1 (m=1)

Grid:

[1]

Return:

[]

n=4 (m=2)

Grid:

[1,2]
[3,4]

Return:

[]

n=9 (m=3)

Grid:

[1,2,3]
[4,5,6]
[7,8,9]

Return:

[5]

n=16 (m=4)

Grid:

[ 1, 2, 3, 4]
[ 5, 6, 7, 8]
[ 9,10,11,12]
[13,14,15,16]

Return:

[6,7,10,11]

For higher values of m, this answer does a great visualization.


Rules:

  • You may take in either m or n (where n = m*m).
    • If taking in n you are allowed to have undefined behavior where there exists no m for n (E.G. 15).
    • n > 0, m > 0: Both must be integer values.
  • The output may be as a 1D/2D array, matrix or whitespace delimited
  • The output must be in order from least to greatest.
    • If outputting as a matrix this means it must be as it would be in the grid.
  • This is , lowest byte-count wins.
\$\endgroup\$
3
  • \$\begingroup\$ Complete fault on my end, I read it incorrectly. \$\endgroup\$ Feb 2 '18 at 21:46
  • 3
    \$\begingroup\$ @DevelopingDeveloper hey man, if I had a nickle for every time I did that I'd be able to buy a beer or two. \$\endgroup\$ Feb 2 '18 at 21:51
  • \$\begingroup\$ If outputting as a 2D array, can a single empty array be included in the result? \$\endgroup\$
    – Shaggy
    Feb 3 '18 at 9:41

34 Answers 34

1
2
1
\$\begingroup\$

Javascript (63 55 bytes)

m=>[...Array((m-2)*m)].map((x,i)=>i+m).filter(x=>x%m>1)

Explanation

m =>                    // function signature is m
    [...Array((m-2)*m)] // create an array of size m^2 - 2m,
                        // m^2 is the full array size, taking out 2m removes the top and bottom edges
                        // filled with undefined
    .map((x,i)=>i+m)    // fill with values m to n-m
                        // (skips the top and bottom edges of the square)
    .filter(x=>x%m>1)   // filter out x%m < 2, the left and right sides of the square
\$\endgroup\$
2
  • 1
    \$\begingroup\$ You only use n once, so you can just use m*m directly. Also, the JavaScript (ES6) tips page has a tip for getting rid of fill. \$\endgroup\$
    – Neil
    Feb 3 '18 at 2:36
  • \$\begingroup\$ You're right n=m*m is a holdover from a previous iteration when I had multiple references to n. And thanks for the tip on .fill, I figured there was a way to golf it, but I hadn't considered [...Array(x)]. \$\endgroup\$
    – asgallant
    Feb 5 '18 at 16:25
1
\$\begingroup\$

JavaScript, 47 bytes

  m=>{for(i=m;++i<m*m-m;)if(i%m>1)console.log(i)} // here's the 47-byte script

Also, 49 bytes:

  m=>{for(i=m;(i%m>1&&console.log(i))|++i<m*m-m;);} // here's the 49-byte script

\$\endgroup\$
5
  • 2
    \$\begingroup\$ Your solution presumes the input is assigned to a predefined variable, which we don't permit. \$\endgroup\$
    – Shaggy
    Feb 3 '18 at 11:08
  • \$\begingroup\$ Not only that, but this solution isn't a function or a full program and relies on other variables and values. For this to be considered valid, either rewrite this as a function or a full program (asking for input with prompt()). Here's a sub-optimal 55 bytes long solution for you: (m,a=[])=>{for(i=m;++i<m*m-m;)if(i%m>1)a[i]=i;return a} \$\endgroup\$ Feb 3 '18 at 11:46
  • \$\begingroup\$ Fixed in accordance with rules. \$\endgroup\$
    – JSGuy
    Feb 4 '18 at 15:54
  • \$\begingroup\$ You can golf it further by switching to an arrow function: m=>{for(i=m;++i<m*m-m;)if(i%m>1)console.log(i)}, 47 bytes. \$\endgroup\$
    – asgallant
    Feb 5 '18 at 17:17
  • \$\begingroup\$ alert is shorter than console.log. Welcome to PPCG, by the way. \$\endgroup\$
    – Shaggy
    Feb 6 '18 at 8:54
1
\$\begingroup\$

APL, 28 Bytes

Will run given value for m

1 1↓¯1 ¯1↓m m⍴⍳m*2

Try it...

Explanation

m*2 squares m

⍳m*2 gets list from 1 to m*2

m m⍴⍳m*2 wraps list to m by m matrix

¯1 ¯1↓m m⍴⍳m*2 drops -last row an column

1 1↓¯1 ¯1↓m m⍴⍳m*2 drops first row and column

\$\endgroup\$
1
  • \$\begingroup\$ Welcome to the site! Could you provide some way to test the submission, such as a link to an online interpreter so that others can tell that it is a working solution? \$\endgroup\$ Feb 7 '18 at 14:05
1
\$\begingroup\$

Perl, 30 29 bytes

perl -E 'say$_+=@a+2for(@a=2..~-<>)x@a' <<< 4
\$\endgroup\$
1
2

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.