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']
]
Just because it was so much fun solving it by composing functions. :)
Language: Haskell (Lambdabot)
Length:
135100Solution:
Using lists as monad, nice occasion for the Kleisli operator
>=>
. You will need to import Data.List and Control.Monad. So I declare this to be a solution for the lambdabot.Because monadic composition (data flows from left to right) is used after normal composition (data flows from right to left) after monadic composition after normal composition, in this case I strongly prefer the use of the reverse Kleisli operator
<=<
to let the data flow consequently from right to left.Using the reverse Kleisli operator the lambda expressions have to be put in braces. This makes the solution 2 characters longer. It is therefore one of the rare cases, when the much more elegant applicative style
map.(,)<$>fst<*>snd
is shorter than the corresponding lambda expression in braces(\(a,l)->map((,)a)l)
. All in all, using the Kleisli operator in reverse, here, is twice an improvement in elegance and readability, but the solution is one character longer.Language: Haskell (Lambdabot)
Length: 101
Solution: