Skip to content

Instantly share code, notes, and snippets.

@xxnjdlys
Created March 12, 2019 12:13
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 xxnjdlys/a56bdbba6a0288431820b86514efe50d to your computer and use it in GitHub Desktop.
Save xxnjdlys/a56bdbba6a0288431820b86514efe50d to your computer and use it in GitHub Desktop.
// 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