-
-
Notifications
You must be signed in to change notification settings - Fork 30.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
61 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
src/algorithms/math/pascal-triangle/__test__/pascalTriangle.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import pascalTriangle from '../pascalTriangle'; | ||
|
||
describe('pascalTriangle', () => { | ||
it('should calculate Pascal Triangle coefficients for specific line number', () => { | ||
expect(pascalTriangle(0)).toEqual([1]); | ||
expect(pascalTriangle(1)).toEqual([1, 1]); | ||
expect(pascalTriangle(2)).toEqual([1, 2, 1]); | ||
expect(pascalTriangle(3)).toEqual([1, 3, 3, 1]); | ||
expect(pascalTriangle(4)).toEqual([1, 4, 6, 4, 1]); | ||
expect(pascalTriangle(5)).toEqual([1, 5, 10, 10, 5, 1]); | ||
expect(pascalTriangle(6)).toEqual([1, 6, 15, 20, 15, 6, 1]); | ||
expect(pascalTriangle(7)).toEqual([1, 7, 21, 35, 35, 21, 7, 1]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/** | ||
* @param {number} lineNumber - zero based. | ||
* @return {number[]} | ||
*/ | ||
export default function pascalTriangle(lineNumber) { | ||
const currentLine = [1]; | ||
|
||
const currentLineSize = lineNumber + 1; | ||
|
||
for (let numIndex = 1; numIndex < currentLineSize; numIndex += 1) { | ||
// See explanation of this formula in README. | ||
currentLine[numIndex] = currentLine[numIndex - 1] * (lineNumber - numIndex + 1) / numIndex; | ||
} | ||
|
||
return currentLine; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters