Last active
August 22, 2020 14:04
-
-
Save thiagolenz/da0cdf0442f000c0aea6bf3de683576c to your computer and use it in GitHub Desktop.
Combinações de Senhas com pares de números
This file contains hidden or 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 java.lang.StringBuilder | |
| fun main() { | |
| val pairs = mutableListOf<Pair<Int, Int>>() | |
| pairs.add(Pair(0, 9)) | |
| pairs.add(Pair(7, 4)) | |
| pairs.add(Pair(5, 2)) | |
| pairs.add(Pair(8, 1)) | |
| pairs.add(Pair(3,6)) | |
| pairs.add(Pair(7, 4)) | |
| val senhas = definirSenhasPossiveis(pairs) | |
| senhas.forEach (::println) | |
| println(" Total de senhas = ${senhas.size}") | |
| } | |
| fun definirSenhasPossiveis (pairs: List<Pair<Int, Int>>): Set<String> { | |
| val binaryPositions = getCombinacoesBinarias() | |
| val senhas = mutableSetOf<String>() | |
| binaryPositions.forEach { position -> | |
| val chars = position.toCharArray() | |
| val finalString = StringBuilder() | |
| for (i in pairs.indices) { | |
| val pair = pairs[i] | |
| finalString.append(if (chars [i] == '0') pair.first else pair.second) | |
| } | |
| senhas.add(finalString.toString()) | |
| } | |
| return senhas | |
| } | |
| fun getCombinacoesBinarias (): List<String> { | |
| val binaryPositions = mutableListOf<String>() | |
| for (i in 0..63) { | |
| binaryPositions.add(toBinary(i, 6)) | |
| } | |
| return binaryPositions | |
| } | |
| fun toBinary(x: Int, len: Int): String = String.format( | |
| "%" + len + "s", | |
| Integer.toBinaryString(x) | |
| ).replace(" ".toRegex(), "0") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment