New Function : Heap's algorithm
This commit is contained in:
@ -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;
|
||||
}
|
@ -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');
|
||||
|
||||
|
Reference in New Issue
Block a user