I have a List that contains, 1s and -1s. The goal I'm after is to find the position in the List when the total is -1.
List[Int] = List(1, -1, 1, -1, 1, 1, -1, 1, -1, 1, -1, 1, 1, 1, 1, 1, -1, -1, -1, 1, -1,
-1, 1, 1, -1, -1, 1, 1, -1, 1, 1, -1, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, -1, 1, -1, -1, 1)
But my code is not working.
Here are my attempts(I spaced the code out for better reading) Note: floor
is the val that holds the List of Ints.
floor.foldLeft(0) { ( (x,y) => x+y == -1 ) }.indexOf(-1)
floor.foldLeft(0) ( (x,y) => { (x + y == -1) {x.indexOf(-1)} } )
floor.foldLeft(0) { (x,y) => { if (x + y == -1) { indexOf(-1) } } }
I'd like to know what I'm doing wrong here. I'm really after the why more than the answer in and of itself.