Skip to content

Instantly share code, notes, and snippets.

@LeandrodeLimaC
Created May 19, 2022 21:32
Show Gist options
  • Save LeandrodeLimaC/afe673b1a926d7ec1db81d600edc99b7 to your computer and use it in GitHub Desktop.
Save LeandrodeLimaC/afe673b1a926d7ec1db81d600edc99b7 to your computer and use it in GitHub Desktop.
Desafio de Code Dojo Alliar :)
{
"presets": [
"@babel/preset-env"
],
"plugins": [
[
"@babel/plugin-proposal-decorators",
{
"legacy": true
}
],
"@babel/plugin-proposal-object-rest-spread"
]
}

Sub-arrays dado uma subtração

Recentemente o time de UX da Alliar viu a necessidade de implementar um sitema de recomendação para mensurar a satisfação dos usuários com o sistema de agendamento de exames. Os dados coletados são guardados em uma lista durante um período de tempo determinado, guardando a nota mais recebida pelos clientes.

Fazer um programa que calcule o número de intervalo que a nota decai de N-1. Ex:

INPUT: [4,3,2,1] OUTPUT: 9 períodos

4 períodos de 1 - [4], [3], [2], [1]

3 períodos de 2 - [4,3], [3,2], [2,1]

2 períodos de 3 - [4,3,2], [3,2,1]

// Sync object
/** @type {import('@jest/types').Config.InitialOptions} */
const config = {
verbose: true,
};
module.exports = config;
// Or async function
module.exports = async () => {
return {
verbose: true
};
};
{
"name": "desafio-1",
"version": "1.0.0",
"description": "",
"main": "cashMachine.js",
"scripts": {
"test": "jest --silent"
},
"jest": {
"testPathIgnorePatterns": ["/node_modules/"],
"testFileExtensions": [
"es6", "js"
],
"moduleFileExtensions": ["js", "json", "es6"]
},
"author": "",
"license": "ISC",
"devDependencies": {
"@babel/preset-env": "^7.16.11",
"chai": "^4.3.6",
"esm": "^3.2.25",
"jest": "^27.5.1",
"mocha": "^3.2.0",
"node-fetch": "^1.7.3",
"sinon-chai": "^2.14.0",
"sinon-stub-promise": "^4.0.0"
},
"dependencies": {
"@babel/plugin-proposal-decorators": "^7.17.9",
"@babel/plugin-proposal-object-rest-spread": "^7.17.3",
"babel": "^6.23.0",
"babel-install": "^2.1.0"
}
}
// INPUT: [4,3,2,1]
// OUTPUT: 9 períodos
// 4 períodos de 1 - [4], [3], [2], [1]
// 3 períodos de 2 - [4,3], [3,2], [2,1]
// 2 períodos de 3 - [4,3,2], [3,2,1]
const calcRangeArrays = (array) => {
let periods = array.length;
// const newArray = [...array]
// newArray.pop()
// [4,3,2,1]
let MaxEventNumber = array.length - 1
for (let i = 0; i <= MaxEventNumber; i++) {
array.forEach((element, index) => {
const nextPosValue = array[++index]
let isPeriod = element - 1 === nextPosValue
!isPeriod || periods++
});
}
}
export default calcRangeArrays
// ---------------------------------------------
// INPUT: [4,3,2,1]
// OUTPUT: 9 períodos
// 4 períodos de 1 - [4], [3], [2], [1]
// 3 períodos de 2 - [4,3], [3,2], [2,1]
// 2 períodos de 3 - [4,3,2], [3,2,1]
// export default function calcRangeArrays(array) {
// let periods = array.length; // 6
// array.forEach((element, index) => {
// const nextPosValue = array[++index]
// let isPeriod = element - 1 === nextPosValue
// !isPeriod || periods++
// });
// return periods;
// }
// INPUT: [3, 2, 2, 1]
// OUTPUT: 6 períodos
// 4 períodos de 1 - [3], [2], [2], [1]
// 2 períodos de 2 - [3,2], [2,1]
// 0 períodos de 3 -
const chai = require("chai");
import calcRangeArrays from './range';
describe('Arrange Arrays', () => {
describe('Teste Scenario #1', () => {
const periods = calcRangeArrays([4, 3, 2, 1]);
it('should exist', () => {
chai.expect(periods).to.be.exist;
});
it('should return corretly number of ranges', () => {
chai.expect(periods).to.eql(9);
});
})
describe('Teste Scenario #2', () => {
const periods = calcRangeArrays([3, 2, 2, 1]);
it('should return corretly number of ranges', () => {
chai.expect(periods).to.eql(6);
});
})
describe('Teste Scenario #3', () => {
const periods = calcRangeArrays([15, 16, 10, 9, 6, 7, 17]);
it('should return corretly number of ranges', () => {
chai.expect(periods).to.eql(2);
});
})
describe('Teste Scenario #3', () => {
const periods = calcRangeArrays([5, 2, 8, 1, 4, 3, 2, 1,]);
it('should return corretly number of ranges', () => {
chai.expect(periods).to.eql(13);
});
})
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment