Created
March 12, 2019 12:13
-
-
Save xxnjdlys/a56bdbba6a0288431820b86514efe50d to your computer and use it in GitHub Desktop.
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
// fibonacci number by index; | |
private fun fibonacciByIteration(n: Int): Int { | |
var x = 1 | |
var y = 1 | |
var z = 2 | |
for (i in 0 until n) { | |
x = y | |
y = z | |
z = x + y | |
} | |
return x | |
} | |
// fibonacci number by index; | |
private fun fibonacciByRecursion(n: Int): Int { | |
return if (n == 1 || n == 0) { | |
1 | |
} else fibonacciByRecursion(n - 1) + fibonacciByRecursion(n - 2) | |
} | |
/** | |
* compare two different version names like : 1.2.3.4 and 1.2.3.6.1 | |
*/ | |
fun compareVersionName(oldVersionName: String, newVersionName: String): Int { | |
val oldNumbers = oldVersionName.split("\\.".toRegex()) | |
val newNumbers = newVersionName.split("\\.".toRegex()) | |
// Avoid IndexOutOfBounds | |
val minIndex = Math.min(oldNumbers.size, newNumbers.size) | |
for (i in 0 until minIndex) { | |
val oldVersionPart = Integer.valueOf(oldNumbers[i]) | |
val newVersionPart = Integer.valueOf(newNumbers[i]) | |
if (oldVersionPart < newVersionPart) { | |
return -1 | |
} else if (oldVersionPart > newVersionPart) { | |
return 1 | |
} | |
} | |
if (oldNumbers.size != newNumbers.size) { | |
return if (oldNumbers.size > newNumbers.size) 1 else -1 | |
} | |
return 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment