9
A collection of N dimensional coordinates are provided. An example is below:
{2,3,4}
This can be thought of as a 3 dimensional array with 2x's, 3y's and 4z's; there may be any number of dimensions. In the example, there are 24 total nodes. Each node can be indexed using {x,y,z}. To access the 5th node, the provided indices would be {0, 1, 0} based on the table below.
## | x y z
0 1 2
-----------
0 | 0 0 0
1 | 0 0 1
2 | 0 0 2
3 | 0 0 3
4 | 0 1 0
5 | 0 1 1
6 | 0 1 2
7 | 0 1 3
8 | 0 2 0
...
23 | 1 2 3
The purpose of this application is to work backwards to determine an index if given a node number.
If asked for the "y" index of the 8th node, the program should print "2".
With the following input provided:
{2,3,4}|8|1
<List of Coordinates>|<Node>|<Index>
The following should be printed:
2
You can assume that the input will be provided in some convenient manner in your language of choice and does not require bounds checking. For example you may assume that the provided index of choice ("y" in the example) is valid with respect to the provided coordinates. You may use 0 or 1 based indexing; the example presumes 0 based.
This is sort of the reverse of this question: Index of a multidimensional array
1Perhaps add a few test cases – Luis Mendo – 2017-05-04T23:14:55.793
1Can we let the coordinates run from 1 to x instead of 0 to x-1? So node #0 would be (1,1,1) and node #23 (2,3,4). – nimi – 2017-05-05T15:55:05.070
@nimi Yes, 1 based indexing is fine. – Mark Johnson – 2017-05-06T15:17:43.620