I was solving this question on a site that gives you a 1d array called grid
grid = ['top left', 'top middle', 'top right',
'middle left', 'center', 'middle right',
'bottom left', 'bottom middle', 'bottom right']
and you're expected to write a method fire(x,y)
that takes in two coordinates and gives you back where you hit the grid.
For example:
fire(0,0) # 'top left'
fire(1,2) # 'bottom middle'
Here is my solution, I used NumPy for it.
import numpy as np
def fire(x,y):
#the grid is preloaded as you can see in description
oneDArray = np.array(grid)
twoDArray = oneDArray.reshape(3,3)
return twoDArray[y][x]
I'm looking to get some feedback on the answer.