Created
April 23, 2021 00:36
-
-
Save ggondim/895fde0ffc50221fb0fa5eeecf22a59b to your computer and use it in GitHub Desktop.
Array Combination (n, m=2)- Combinação simples de n elementos com m = 2 - Checagem com fatorial
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
const fatorial = (n) => n === 0 ? 1 : n * fatorial(n - 1); | |
const combinacoes = (n, m) => fatorial(n) / (fatorial(m) * fatorial(n - m)); | |
function combinacoesDoisElementos(array: any[]) { | |
return array.reduce((combinacoes, item, i) => array | |
.slice(i+1) | |
.map(c => [item, c]) | |
.concat(combinacoes), []); | |
} | |
function assertCombinacao(array) { | |
return assertCombinacao(array).length === combinacoes(array.length, 2); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment