Scala convert IndexedSeq[AnyVal] to Array[Int]

Viewed 2960

I'm trying to solve Codility's GenomicRangeQuery using Scala and to that end I wrote the following function:

def solution(s: String, p: Array[Int], q: Array[Int]): Array[Int] = {
  for (i <- p.indices) yield {
    val gen = s.substring(p(i), q(i) + 1)
    if (gen.contains('A')) 1
    else if (gen.contains('C')) 2
    else if (gen.contains('G')) 3
    else if (gen.contains('T')) 4
  }
}

I haven't done a lot of testing but it seems to solve the problem.

My issue is the for comprehension returns an scala.collection.immutable.IndexedSeq[AnyVal], while the function must return an Array[Int] and therefore it's throwing a type mismatch error.

Is there any way to make the for comprehension return an Array[Int] or transform the IndexedSeq[AnyVal] into an Array[Int]?

3 Answers
Related