This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Reverses an integer without using Strings | |
def reverse(n: Int): Int = { | |
val len = math.ceil(math.log10(n+1)).toInt | |
Stream.continually(n).take(len).zipWithIndex | |
.map{ case (m,p) => | |
(m % math.pow(10, len-p) / math.pow(10, len-p-1)).toInt * math.pow(10,p) | |
} | |
.sum.toInt | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment