New Function : Heap's algorithm

This commit is contained in:
Divlo
2019-10-11 21:30:05 +02:00
parent 23c3e10b0f
commit eaec55fcbb
7 changed files with 84 additions and 2 deletions

View File

@ -366,4 +366,24 @@ function armstrongNumber(number) {
} else {
return `${number} n'est pas un nombre d'Armstrong, car ${resultString.slice(2)} = ${formatNumberResult(result)}.`;
}
}
// Retourne un tableau contenant toutes les possibilités d'anagramme d'un mot
function stringPermutations(string) {
let results = [];
if (string.length === 1) {
results.push(string);
return results;
}
for (let i = 0; i < string.length; i++) {
let firstChar = string[i];
let charsLeft = string.substring(0, i) + string.substring(i + 1);
let innerPermutations = stringPermutations(charsLeft);
for (let i = 0; i < innerPermutations.length; i++) {
results.push(firstChar + innerPermutations[i]);
}
}
return results;
}

View File

@ -254,7 +254,25 @@ $(function () {
}
});
$("#submitHeapAlgorithm").click(function()
{
let value = $('#value').val();
if(isEmptyValue(value))
{
$('.results').html(emptyMessageError);
}
else
{
let stringPermutationsResult = stringPermutations(value);
let result = "";
for (element in stringPermutationsResult) {
result = result + stringPermutationsResult[element] + "<br>";
}
$('.results').html(`Il y a ${formatNumberResult(stringPermutationsResult.length)} possibilités d'anagramme pour le mot "${value}" qui contient ${value.length} caractères, la liste : <br><br> ${result}`);
}
});
/* Permet d'afficher l'heure en temps réel sur le footer */
window.onload = realDateTime('realDateTime');