0

i'm learning Scala, but I'm not able go trough the for loop in this code

    def adjacentElementsProduct(inputArray: Array[Int]): Int = {

    var maxSoFar = 0
    var maxHere = 0

    //for (i <- 0:Int to (inputArray.length-1))      <- error
    for (i <- 0 to inputArray.length-1)            //<- error
    {
          if( maxHere * inputArray(i) > 0 )
                maxHere *= inputArray(i)
          else
                maxHere = 0

          if( maxHere > maxSoFar )
                maxSoFar = maxHere

          maxSoFar
    }

}

Compiler results:

(without :Int)

  file.scala on line 6: error: type mismatch;
 found   : Unit
 required: Int
        for (i <- 0 to inputArray.length-1)
               ^

(with :Int)

file.scala on line 6: error: identifier expected but integer literal found.
    for (i <- 0:Int to (inputArray.length - 1) )
                                            ^
file.scala on line 19: error: ')' expected but '}' found.
}
^

What is wrong? How can i solve it?

Thanks a lot, Davide

2
  • If maxHere starts at 0, how can maxHere * inputArray(i) ever be > 0? Commented Sep 20, 2017 at 10:19
  • yeah my fault, i've corrected it with 1 as soon as the for loop worked Commented Sep 20, 2017 at 10:46

3 Answers 3

2

adjacentElementsProduct needs to return a Int, you need to put what you want to return at the end of the method (outside the for-loop).

Also you can simplify for (i <- 0 to inputArray.length-1) to for (i <- inputArray) and replace inputArray(i) with i.

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

Comments

2

If what you're trying to do is return the maximum product of adjacent non-zero integers, here's a more Scala-esque approach.

def adjacentElementsProduct(inputArray: Array[Int]): Int =
  inputArray.scanLeft(0)((a,b) => if (a==0) b else a*b).max

explanation (somewhat simplified)

Think of it this way: myArray.scanLeft(init)(op) will build a new Array that looks something like:

Array( init
     , op(init,             myArray(0))
     , op(previousOpResult, myArray(1))
     , op(previousOpResult, myArray(2))
     . . .
     )

2 Comments

oh ok, let me understand with that structure it scan the array from 0 to the end i guess, and it create an external temporary structure with all the products so it can find the max value?
@DavideAmbrosi; Explanation added.
1

the return value maxSoFar should out of the for loop {},like this :

def adjacentElementsProduct(inputArray: Array[Int]): Int = {

    var maxSoFar = 0
    var maxHere = 0

    //for (i <- 0:Int to (inputArray.length-1))      <- error
    for (i <- 0 to inputArray.length-1)            //<- error
    {
          if( maxHere * inputArray(i) > 0 )
                maxHere *= inputArray(i)
          else
                maxHere = 0

          if( maxHere > maxSoFar )
                maxSoFar = maxHere


    }
    maxSoFar

} 

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.