Given a two dimensional array, return the co-ordinates of 'x'.
The co-ordinates should be zero indexed.
The solution should pass the example cases provided.
You may use any programming language.
You should assume you will always get an array as input.
Example test cases:
'Return an empty array if input is an empty array' => []
[]
'Return an empty array if no x found' => []
[
['o', 'o'],
['o', 'o']
]
'Return an empty array if more than one x found' => []
[
['x', 'o'],
['o', 'x']
]
'Return an empty array if more than one x found on the same line' => []
[
['x', 'x'],
['o', 'o']
]
'Return [0,0] when x at top left' => [0, 0]
[
['x', 'o'],
['o', 'o']
]
'Return [4,6] for the example below' => [4, 6]
[
['o', 'o', 'o', 'o', 'o', 'o', 'o', 'o'],
['o', 'o', 'o', 'o', 'o', 'o', 'o', 'o'],
['o', 'o', 'o', 'o', 'o', 'o', 'o', 'o'],
['o', 'o', 'o', 'o', 'o', 'o', 'o', 'o'],
['o', 'o', 'o', 'o', 'o', 'o', 'x', 'o'],
['o', 'o', 'o', 'o', 'o', 'o', 'o', 'o']
]
'Return [4,6] for the example below' => [4, 6]
[
['t', 't', 't', 'o', 'o', 'o', '~', 'o'],
['o', 'r', 'o', 'o', 'o', 'o', 't', 'o'],
['o', 'o', 'o', 's', 'o', 'o', 'o', 'o'],
['o', 'o', 'c', 'o', 'o', 'o', 'o', 'o'],
['o', 'o', 'a', 'a', 'a', 'o', 'x', 'o'],
['d', 'o', 'o', 'o', 'o', 'o', 'o', 'o']
]
Language: php7
Length: 178
Solution:
function s($a){foreach($a AS$x=>$c){$y=array_search("x",$c);if(is_int($y)){if(isset($r)){return array();}else{$r = array($x,$y);}}}if(isset($r)){return $r;}else{return array();}}
...four.... thought I'd tee off first :) First shot was a lot shorter, but only met half the test cases.