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 =
zip3
let matchIndex (x, y, i) a
| x == y = i : a
(scanl1 (+) xs) -- Sums from the left
| otherwise = a
(scanr1 (+) xs) -- Sums from the right
[0 ..] -- Indices
in foldr
>>= (\(x, y, i) -> [i | x == y])
matchIndex
[]
(zip3
(scanl1 (+) xs) -- Sums from the left
(scanr1 (+) xs) -- Sums from the right
[0 ..] -- Indices
)


---------------------------TEST----------------------------
--------------------------- TEST -------------------------
main :: IO ()
main :: IO ()
main =
main =
mapM_
mapM_
print
print
(equilibriumIndices <$>
$ equilibriumIndices
[ [-7, 1, 5, 2, -4, 3, 0]
<$> [ [-7, 1, 5, 2, -4, 3, 0],
, [2, 4, 6]
[2, 4, 6],
, [2, 9, 2]
[2, 9, 2],
, [1, -1, 1, -1, 1, -1, 1]
[1, -1, 1, -1, 1, -1, 1],
, [1]
[1],
, []
[]
])</lang>
]</lang>
{{Out}}
{{Out}}
<pre>[3,6]
<pre>[3,6]