From a605b781f429952160e281274f87de68fa1f7a2b Mon Sep 17 00:00:00 2001 From: Divlo Date: Fri, 20 Mar 2020 11:45:19 +0100 Subject: [PATCH] backend: AJout de Heap's algorithm + .env.example --- backend/.env.example | 1 + backend/assets/functions/functionObject.js | 4 +- .../assets/functions/main/convertDistance.js | 2 +- .../assets/functions/main/heapAlgorithm.js | 46 +++++++++++++++++++ 4 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 backend/.env.example create mode 100644 backend/assets/functions/main/heapAlgorithm.js diff --git a/backend/.env.example b/backend/.env.example new file mode 100644 index 0000000..86648e1 --- /dev/null +++ b/backend/.env.example @@ -0,0 +1 @@ +OpenWeatherMap_API_KEY = "" \ No newline at end of file diff --git a/backend/assets/functions/functionObject.js b/backend/assets/functions/functionObject.js index fd7d290..a785f5d 100644 --- a/backend/assets/functions/functionObject.js +++ b/backend/assets/functions/functionObject.js @@ -6,6 +6,7 @@ const { armstrongNumberOutput } = require('./ma const { weatherRequestOutput } = require('./main/weatherRequest'); const { convertCurrencyOutput } = require('./main/convertCurrency'); const { calculateAgeOutput } = require('./main/calculateAge'); +const { heapAlgorithmOutput } = require('./main/heapAlgorithm'); const functionObject = { randomNumber : randomNumberOutput, @@ -16,7 +17,8 @@ const functionObject = { armstrongNumber : armstrongNumberOutput, weatherRequest : weatherRequestOutput, convertCurrency : convertCurrencyOutput, - calculateAge : calculateAgeOutput + calculateAge : calculateAgeOutput, + heapAlgorithm : heapAlgorithmOutput }; // Choisi la fonction à exécuter diff --git a/backend/assets/functions/main/convertDistance.js b/backend/assets/functions/main/convertDistance.js index 0662944..3ce5d73 100644 --- a/backend/assets/functions/main/convertDistance.js +++ b/backend/assets/functions/main/convertDistance.js @@ -43,7 +43,7 @@ exports.convertDistanceOutput = ({ res, next }, argsObject) => { const result = convertDistance(number, numberUnit, finalUnit); if (!result) { - return sendResponse(res, generalError); + return errorHandling(next, generalError); } return res.status(200).json(result); diff --git a/backend/assets/functions/main/heapAlgorithm.js b/backend/assets/functions/main/heapAlgorithm.js new file mode 100644 index 0000000..dc505a4 --- /dev/null +++ b/backend/assets/functions/main/heapAlgorithm.js @@ -0,0 +1,46 @@ +const errorHandling = require('../../utils/errorHandling'); +const { requiredFields } = require('../../config/errors'); + +/** + * @description Retourne un tableau contenant toutes les possibilités d'anagramme d'un mot. + * @param {String} string - La chaîne de caractère à permuter + * @returns {Array} + * @examples heapAlgorithm('abc') → ["abc", "acb", "bac", "bca", "cab", "cba"] + */ +function heapAlgorithm(string) { + let results = []; + + if (string.length === 1) { + results.push(string); + return results; + } + + for (let indexString = 0; indexString < string.length; indexString++) { + const firstChar = string[indexString]; + const charsLeft = string.substring(0, indexString) + string.substring(indexString + 1); + const innerPermutations = heapAlgorithm(charsLeft); + for (let indexPermutation = 0; indexPermutation < innerPermutations.length; indexPermutation++) { + results.push(firstChar + innerPermutations[indexPermutation]); + } + } + return results; +} + +/* OUTPUTS */ +exports.heapAlgorithmOutput = ({ res, next }, argsObject) => { + let { string } = argsObject; + + // S'il n'y a pas les champs obligatoire + if (!(string)) { + return errorHandling(next, requiredFields); + } + + // Si la chaîne de caractère dépasse LIMIT_CHARACTERS caractères + const LIMIT_CHARACTERS = 8; + if (string.length > LIMIT_CHARACTERS) { + return errorHandling(next, { message: `Par souci de performance, vous ne pouvez pas exécuter cette fonction avec un mot dépassant ${LIMIT_CHARACTERS} caractères.`, statusCode: 400 }); + } + + const result = heapAlgorithm(string); + return res.status(200).json(result); +} \ No newline at end of file