diff --git a/.github/workflows/test.yml-template b/.github/workflows/test.yml-template new file mode 100644 index 00000000..bb13dfc4 --- /dev/null +++ b/.github/workflows/test.yml-template @@ -0,0 +1,23 @@ +name: Test + +on: + pull_request: + branches: [ master ] + +jobs: + build: + + runs-on: ubuntu-latest + + strategy: + matrix: + node-version: [20.x] + + steps: + - uses: actions/checkout@v2 + - name: Use Node.js ${{ matrix.node-version }} + uses: actions/setup-node@v1 + with: + node-version: ${{ matrix.node-version }} + - run: npm install + - run: npm test diff --git a/.prettierignore b/.prettierignore new file mode 100644 index 00000000..8225baa4 --- /dev/null +++ b/.prettierignore @@ -0,0 +1,2 @@ +/node_modules +/dist diff --git a/.prettierrc b/.prettierrc new file mode 100644 index 00000000..8c8d7fcb --- /dev/null +++ b/.prettierrc @@ -0,0 +1,8 @@ +{ + "tabWidth": 2, + "useTabs": false, + "semi": true, + "singleQuote": true, + "trailingComma": "all", + "printWidth": 80 +} diff --git a/src.zip b/src.zip new file mode 100644 index 00000000..b3eed4f1 Binary files /dev/null and b/src.zip differ diff --git a/src/splitInteger.js b/src/splitInteger.js index d3da7485..8734bf8d 100644 --- a/src/splitInteger.js +++ b/src/splitInteger.js @@ -1,23 +1,44 @@ +// 'use strict'; + +// /** +// * @param {number} value +// * @param {number} numberOfParts +// * +// * @returns {number[]} +// */ +// function splitInteger(value, numberOfParts) { +// const parts = []; +// let rest = value; + +// for (let partsLeft = numberOfParts; partsLeft > 0; partsLeft--) { +// const part = Math.floor(rest / partsLeft); + +// parts.push(part); +// rest -= part; +// } + +// return parts; +// } + +// module.exports = splitInteger; + 'use strict'; -/** - * @param {number} value - * @param {number} numberOfParts - * - * @returns {number[]} - */ function splitInteger(value, numberOfParts) { - const parts = []; - let rest = value; + const base = Math.floor(value / numberOfParts); + const remainder = value % numberOfParts; + const result = Array(numberOfParts).fill(base); - for (let partsLeft = numberOfParts; partsLeft > 0; partsLeft--) { - const part = Math.floor(rest / partsLeft); + for (let i = 0; i < remainder; i++) { + result[numberOfParts - 1 - i] += 1; + } - parts.push(part); - rest -= part; + // If value < numberOfParts, fill with zeros and ones + if (value < numberOfParts) { + return Array(numberOfParts - value).fill(0).concat(Array(value).fill(1)); } - return parts; + return result; } module.exports = splitInteger; diff --git a/src/splitInteger.test.js b/src/splitInteger.test.js index a610317d..a655d6b2 100644 --- a/src/splitInteger.test.js +++ b/src/splitInteger.test.js @@ -4,18 +4,18 @@ const splitInteger = require('./splitInteger'); test(`should split a number into equal parts if a value is divisible by a numberOfParts`, () => { - + expect(splitInteger(8, 2)).toEqual([4, 4]); }); test(`should return a part equals to a value when splitting into 1 part`, () => { - + expect(splitInteger(8, 1)).toEqual([8]); }); test('should sort parts ascending if they are not equal', () => { - + expect(splitInteger(7, 2)).toEqual([3, 4]); }); test('should add zeros if value < numberOfParts', () => { - + expect(splitInteger(3, 4)).toEqual([0, 1, 1, 1]); });