Given two equally sized arrays of integers A
and B
, compute the number of times A[i] > B[i]
and the number of times A[i] < B[i]
for each index i
.
New to scala, I put together the following solution:
import scala.io.Source
object Solution {
def main(args: Array[String]) {
val lines = Source.stdin.getLines
val alice = lines.next.split(" ").map(_.toInt)
val bob = lines.next.split(" ").map(_.toInt)
val competitions = alice zip bob
val scoreA = competitions.count(c => c._1 > c._2)
val scoreB = competitions.count(c => c._1 < c._2)
printf("%d %d", scoreA, scoreB)
}
}
I normally solve these problems using python, so I simply searched online for my usual programming constructs. I have a few questions.
- Is there a more general way to print a list of space separated integers? Currently my use of
printf
is limited to exactly two. - Are there any pitfalls I'm unaware of?
- Ideas of more concise solutions without sacrificing readability?
Any other insights would be greatly appreciated!