11
1
Alright, my second attempt at a code golf, let's see how this goes.
Pretend you have an array of 9 values. Now imagine that array in a 3x3 grid.
You need to return neighbors that number has as indexes of the array.
0 | 1 | 2
3 | 4 | 5
6 | 7 | 8
Rules:
- It's code golf, so shortest answer wins.
- The pretend array's index can start at 0 or 1. (all examples use 0 though)
- Just returning values values is frowned upon (like
if 3: return 046
) - The submission can be just a procedure/function/method, but an example would be nice
- The returned value can be in any order(like if input is 0 it could be 13 or 31)
- if you want, the output can be a list of numbers, e.g.
[0,4,6]
instead of046
- diagonals don't count, as seen by the examples.
Examples:
input:
0
output:
13
input:
3
output:
046
input:
4
output:
1357
4
It looks like this challenge could benefit from some time in the Sandbox. You can post your challenge there so others can review it and help you out with it before posting it to main. From your examples I'm guessing you're not counting diagonals. You may want to add this to the question itself. You also mention the requirement to output the indexes of the array that are neighbors. I think this could just be hardcoded for a 3x3 grid. Would it maybe be better to output the neighbors themselves?
– Poke – 2017-03-26T02:53:16.3407Just so you know, frowned upon isn't really something we do here; hardcoding the output is either allowed or it isn't. Since it's usually pretty hard to define what exactly counts as hardcoding, I'd personally just allow it or give the grid size as an additional input. – Dennis – 2017-03-26T03:35:03.160
1Can the output be a list of numbers, e.g.
[0,4,6]
instead of046
? – Laikoni – 2017-03-26T07:42:37.327@Laikoni Yes, a little bit too late because you've already answered it. – hcorion – 2017-03-26T21:59:49.903
@Dennis Yes, I wasn't quite sure how to put it. I like how the C and python answers did it, by providing both, but having the non-hard coded answer as the final. I wanted to encourage algorithms rather than hard-coding, but I wasn't sure if it was even possible (without overly long answers), and I didn't want to have no answers to my question. – hcorion – 2017-03-26T22:03:23.873