aboutsummaryrefslogtreecommitdiff
path: root/challenge-084/frankivo/scala/ReverseInteger.scala
blob: 39028736d9a452ad823f541ca4584983486a1ab1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import scala.util.Try

object ReverseInteger {
  val examples = Seq(1234, -1234, 1231230512)

  def main(args: Array[String]): Unit = {
    examples
      .map(reverse)
      .foreach(println)
  }

  def reverse(n: Int): Int = {
    val dropN = if (n < 0) 1 else 0
    val multiplier = if (n < 0) -1 else 1
    Try(n.toString.drop(dropN).reverse.toInt * multiplier).getOrElse(0)
  }
}