18
1
Imagine you have an array of integers, whose non-negative values are pointers to other positions in the same array, only that those values represent tunnels, so if the value in position A is positive and points to position B, then the value in position B must be also positive and point to position A to represent both ends of the tunnel. So:
Challenge
- Given an array of integers, check if the array complies with the restriction to be a tunneling array and return two distinct, coherent values for truthy and falsey.
- The values in the array will be below zero for non-tunnel positions, and zero or above for tunnel positions. If your array is 1-indexed, then the zero value represents a non-tunnel position. Non-tunnel values do not need to be checked.
- If a positive value in a cell points to itself, that's a falsey. If A points to B, B to C and C to A, that's a falsey. If a positive value points beyond the limits of the array, that's a falsey.
Examples
The following examples are 0-indexed:
[-1, -1, -1, 6, -1, -1, 3, -1, -1] Truthy (position 3 points to position 6 and vice versa)
[1, 0] Truthy (position 0 points to position 1 and vice versa)
[0, 1] Falsey (positions 0 and 1 point to themselves)
[4, 2, 1, -1, 0, -1] Truthy
[2, 3, 0, 1] Truthy
[1, 2, 0] Falsey (no circular tunnels allowed)
[-1, 2, -1] Falsey (tunnel without end)
[] Truthy (no tunnels, that's OK)
[-1, -2, -3] Truthy (no tunnels, that's OK)
[1, 0, 3] Falsey (tunnel goes beyond limits)
[1] Falsey (tunnel goes beyond limits)
[1, 0, 3, 7] Falsey (tunnel goes beyond limits)
This is code-golf, so may the shortest code for each language win!
3what should we return for
[0]
? – ngn – 2018-09-04T17:44:57.2201Expanding on ngn's question, are self tunnels allowed? What would the cases
[0,1]
and[0,-1,2]
give? – dylnan – 2018-09-04T17:50:52.5271@dylnan
[0,1]
is in the examples. "If a positive value in a cell points to itself, that's a falsey" – ngn – 2018-09-04T17:51:21.7231suggested test:
[2,3,0,1]
– ngn – 2018-09-04T17:55:30.597@ngn wouldn’t that make
[0]
falsey? – dylnan – 2018-09-04T18:14:44.397>
@dylnan not necessarily, 0 is not positive (unless you're French or Belgian) – ngn – 2018-09-04T19:19:44.063
@ngn if your array is 0-indexed then
[0]
is falsey, if it's 1-indexed then truthy. – Charlie – 2018-09-04T19:47:09.7231@JonathanAllan the tunnel values are values indicating possible array positions. If your array is 0-indexed then every value below 0 is not a tunnel value. If it's 1-indexed then every value below 1 is not a tunnel value. – Charlie – 2018-09-04T19:49:38.450
I just posted and then deleted an answer because I realized it raised an error on input of
[1, 2]
. Maybe this could be a test case, because we don't seem to have one yet where something points to something that points beyond the index limits. – mathmandan – 2018-09-05T21:05:22.643@mathmandan see test case
[1,0,3]
returning falsey. – Charlie – 2018-09-06T04:48:52.787@Charlie Right, but I meant something more like
[1, 0, 3, 7]
. So in this example2
points to3
, which IN TURN points outside the array. (My deleted code correctly returnedFalse
for the[1, 0, 3]
test case, but would produce an error on[1, 0, 3, 7]
. So I thought I'd make the suggestion.) – mathmandan – 2018-09-06T05:20:06.880@Charlie I undeleted my answer, so you can see what I'm talking about: https://codegolf.stackexchange.com/a/171786/36885
– mathmandan – 2018-09-06T15:45:52.030@mathmandan interesting. I have added the test case. – Charlie – 2018-09-06T15:48:12.010