Equilibrium index: Difference between revisions
Content added Content deleted
(Updated the program to compile with Nim 1.4. Changed slightly the output. Added the output.) |
m (→{{header|Haskell}}: Tidied the Prelude functions version) |
||
Line 1,151: | Line 1,151: | ||
<lang haskell>equilibriumIndices :: [Int] -> [Int] |
<lang haskell>equilibriumIndices :: [Int] -> [Int] |
||
equilibriumIndices xs = |
equilibriumIndices xs = |
||
⚫ | |||
let matchIndex (x, y, i) a |
|||
(scanl1 (+) xs) -- Sums from the left |
|||
(scanr1 (+) xs) -- Sums from the right |
|||
⚫ | |||
in foldr |
|||
>>= (\(x, y, i) -> [i | x == y]) |
|||
matchIndex |
|||
[] |
|||
⚫ | |||
(scanl1 (+) xs) -- Sums from the left |
|||
(scanr1 (+) xs) -- Sums from the right |
|||
⚫ | |||
) |
|||
---------------------------TEST |
--------------------------- TEST ------------------------- |
||
main :: IO () |
main :: IO () |
||
main = |
main = |
||
mapM_ |
mapM_ |
||
print |
print |
||
$ equilibriumIndices |
|||
[ [-7, 1, 5, 2, -4, 3, 0] |
<$> [ [-7, 1, 5, 2, -4, 3, 0], |
||
[2, 4, 6], |
|||
[2, 9, 2], |
|||
[1, -1, 1, -1, 1, -1, 1], |
|||
[1], |
|||
[] |
|||
] |
]</lang> |
||
{{Out}} |
{{Out}} |
||
<pre>[3,6] |
<pre>[3,6] |