Skip to content

Instantly share code, notes, and snippets.

@nwagu
Last active May 30, 2020 07:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nwagu/f746b725e8988c85057e0087bc4ef0bd to your computer and use it in GitHub Desktop.
Save nwagu/f746b725e8988c85057e0087bc4ef0bd to your computer and use it in GitHub Desktop.
A simple Kotlin function to check if a two-digit number is larger than its digit swap.
import org.junit.Assert.assertEquals
import org.junit.Test
class DigitSwapCompare {
@Test
fun testFunction() {
assertEquals(isLargerOfDigitSwap(14), false)
assertEquals(isLargerOfDigitSwap(53), true)
assertEquals(isLargerOfDigitSwap(99), true)
}
fun isLargerOfDigitSwap(number: Int) : Boolean {
if (number < 10 || number > 99)
throw Exception("Input should be a 2 digit number")
val firstDigit = number / 10
val secondDigit = number % 10
val swap = (secondDigit * 10) + firstDigit
return number >= swap
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment