Last active
May 30, 2020 07:23
-
-
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.
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
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