Created
January 13, 2019 04:06
-
-
Save jkcgs/571c195cdf7a182dfa3229ec3b477652 to your computer and use it in GitHub Desktop.
Validar RUT en Kotlin. Yes, very cool code.
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
class RUT { | |
companion object { | |
fun validateRUT(rut : String) : Boolean { | |
if (!rut.matches(Regex("[0-9]{1,2}(.?[0-9]{3}){2}-?[0-9kK]"))) { | |
return false | |
} | |
// Very cool code :-) | |
val rutf = rut.toLowerCase().replace(Regex("[-.]"), "") | |
val rutl = rutf.takeLast(1); var sum = 0; var i = 0 | |
for (x in rutf.dropLast(1).reversed()){ | |
sum += (x.toString().toInt() * ((i % 6) + 2)); i+=1 | |
} | |
val div = 11 - (sum % 11) | |
return (if (div == 11) 0 else div) == (if (rutl == "k") 10 else rutl.toInt()) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment