1

Is there a way I can access individual elements of a row using the map function? I basically have a grid, and I need to return the row and column index each time I find a 0. The code shown below shows how I used the map function to return the index of the row. Not I need to return the index of the column (i.e. the index of EACH element in that row). I am new to Scala so I'll appreciate any form help :-)

def indices = sudoku.grid.map{
    row=>row.map{
        case 0=> sudoku.grid.indexOf(row) //this returns the index of the row. I need to return the index of the column(i.e. current element being accessed)
    case element=> element //
    }
}

2 Answers 2

1

I don't think your question is very clear, but if you swap

row=>row.map{
    case 0=> sudoku.grid.indexOf(row)
    case element => element
}

for

row => row.zipWithIndex.map{
    case (0, index) => index
    case (element, index) => element
}

Then it returns you the row index rather than the column index, as your code comment desired.

Sign up to request clarification or add additional context in comments.

Comments

0

The number of questions fromn this sudoku assignment is getting silly. And I'm not sure what you want as a result exactly.

However, if you want a set of coordinates of the zeros, how about something like this?

def zeroes (grid:Array[Array[Int]]) = {
for {
    row <- 0 to 8
    col <- 0 to 8
    if grid(col)(row) == 0
    } yield (col, row);
} 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.