-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Rozwiązanie zadania Split Integer #1028
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
8e9a71f
8aa5b28
c965be9
8fbb69e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,20 +2,52 @@ | |
|
|
||
| const splitInteger = require('./splitInteger'); | ||
|
|
||
| test(`should split a number into equal parts | ||
| if a value is divisible by a numberOfParts`, () => { | ||
|
|
||
| test('should split a number into equal parts if divisible', () => { | ||
| const parts = splitInteger(6, 2); | ||
| expect(parts).toEqual([3, 3]); | ||
| expect(parts).toHaveLength(2); | ||
| expect(parts.reduce((a, b) => a + b, 0)).toBe(6); | ||
| expect(parts.every(Number.isInteger)).toBe(true); | ||
| expect(Math.max(...parts) - Math.min(...parts)).toBeLessThanOrEqual(1); | ||
| expect(parts.every((v, i) => i === 0 || parts[i] >= parts[i - 1])).toBe(true); | ||
| }); | ||
|
|
||
| test(`should return a part equals to a value | ||
| when splitting into 1 part`, () => { | ||
|
|
||
| test('should split a number into almost equal parts', () => { | ||
| const parts = splitInteger(32, 6); | ||
| expect(parts).toEqual([5, 5, 5, 5, 6, 6]); | ||
| expect(parts).toHaveLength(6); | ||
| expect(parts.reduce((a, b) => a + b, 0)).toBe(32); | ||
| expect(parts.every(Number.isInteger)).toBe(true); | ||
| expect(Math.max(...parts) - Math.min(...parts)).toBeLessThanOrEqual(1); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add the explicit sortedness assertion here as well. The test already checks
Comment on lines
+16
to
+21
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add two missing example tests required by the task description: 1) |
||
| expect(parts.every((v, i) => i === 0 || parts[i] >= parts[i - 1])).toBe(true); | ||
| }); | ||
|
|
||
| test('should sort parts ascending if they are not equal', () => { | ||
|
|
||
| test('should handle value < numberOfParts', () => { | ||
| const parts = splitInteger(2, 5); | ||
| expect(parts).toEqual([0, 0, 0, 1, 1]); | ||
| expect(parts).toHaveLength(5); | ||
| expect(parts.reduce((a, b) => a + b, 0)).toBe(2); | ||
| expect(parts.every(Number.isInteger)).toBe(true); | ||
| expect(Math.max(...parts) - Math.min(...parts)).toBeLessThanOrEqual(1); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add the explicit sortedness assertion for this test too. Although you assert the exact array |
||
| expect(parts.every((v, i) => i === 0 || parts[i] >= parts[i - 1])).toBe(true); | ||
| }); | ||
|
|
||
| test('should add zeros if value < numberOfParts', () => { | ||
| test('should return [8] when splitting 8 into 1 part', () => { | ||
| const parts = splitInteger(8, 1); | ||
| expect(parts).toEqual([8]); | ||
| expect(parts).toHaveLength(1); | ||
| expect(parts.reduce((a, b) => a + b, 0)).toBe(8); | ||
| expect(parts.every(Number.isInteger)).toBe(true); | ||
| expect(Math.max(...parts) - Math.min(...parts)).toBeLessThanOrEqual(1); | ||
| expect(parts.every((v, i) => i === 0 || parts[i] >= parts[i - 1])).toBe(true); | ||
| }); | ||
|
|
||
| test('should split 17 into 4 almost equal parts [4, 4, 4, 5]', () => { | ||
| const parts = splitInteger(17, 4); | ||
| expect(parts).toEqual([4, 4, 4, 5]); | ||
| expect(parts).toHaveLength(4); | ||
| expect(parts.reduce((a, b) => a + b, 0)).toBe(17); | ||
| expect(parts.every(Number.isInteger)).toBe(true); | ||
| expect(Math.max(...parts) - Math.min(...parts)).toBeLessThanOrEqual(1); | ||
| expect(parts.every((v, i) => i === 0 || parts[i] >= parts[i - 1])).toBe(true); | ||
| }); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add an explicit assertion that the returned array is sorted in ascending (non-decreasing) order for this test. The spec requires the array to be sorted even though other properties (sum, integer elements, max-min) are already checked. Example assertion to add after the existing expects:
This should be inserted after the last expect in this test so sorting is verified for
splitInteger(6, 2).