Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import pascalTriangle from '../pascalTriangle';

describe('pascalTriangle', () => {
it('should calculate Pascal Triangle coefficients for specific line number', () => {
it('should return the smallest valid line', () => {
expect(pascalTriangle(0)).toEqual([1]);
});

it('should calculate coefficients for ordinary line numbers', () => {
expect(pascalTriangle(1)).toEqual([1, 1]);
expect(pascalTriangle(2)).toEqual([1, 2, 1]);
expect(pascalTriangle(3)).toEqual([1, 3, 3, 1]);
Expand Down
6 changes: 5 additions & 1 deletion src/algorithms/math/pascal-triangle/pascalTriangle.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ export default function pascalTriangle(lineNumber) {

for (let numIndex = 1; numIndex < currentLineSize; numIndex += 1) {
// See explanation of this formula in README.
currentLine[numIndex] = (currentLine[numIndex - 1] * (lineNumber - numIndex + 1)) / numIndex;
const previousCoefficient = currentLine[numIndex - 1];
const coefficientMultiplier = lineNumber - numIndex + 1;
const nextCoefficient = (previousCoefficient * coefficientMultiplier) / numIndex;

currentLine[numIndex] = nextCoefficient;
}

return currentLine;
Expand Down
27 changes: 27 additions & 0 deletions work/R51-T026/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# R51-T026 — pascalTriangle

## Refactor
The coefficient calculation now names its intermediate values:
- `previousCoefficient`
- `coefficientMultiplier`
- `nextCoefficient`

This extracts the existing recurrence into readable steps. It does not change the loop, recurrence, public argument, returned array, coefficient order, or side effects.

## Preconditions
The function documentation in `src/algorithms/math/pascal-triangle/pascalTriangle.js` identifies `lineNumber` as zero-based. The Pascal Triangle README states that the recurrence applies for non-negative integer row numbers and valid coefficient indices. This task tests valid row numbers; it does not add input validation.

## Test fixtures
- Input `0`; independently expected output: `[1]`
- Input `5`; independently expected output: `[1, 5, 10, 10, 5, 1]`

The expected row for input 5 is the binomial-coefficient sequence for row 5.

## Check
From the repository root, run:

```sh
python3 work/R51-T026/check.py
```

The runner invokes the repository's Jest test script in non-watch mode, captures the actual output and exit status, and writes `results.json`. The check is not considered passed until the command exits with status 0 and the recorded output reports passing tests.
63 changes: 63 additions & 0 deletions work/R51-T026/check.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import json
import re
import subprocess
from datetime import datetime, timezone
from pathlib import Path

ROOT = Path(__file__).resolve().parents[2]
EVIDENCE = Path(__file__).resolve().parent
COMMAND = ["npm", "test", "--", "--runInBand"]


def version(command):
result = subprocess.run(
command,
cwd=ROOT,
capture_output=True,
text=True,
check=False,
)
return (result.stdout + result.stderr).strip()


print("Precondition: lineNumber is a nonnegative integer (zero-based).")
print("Fixtures: 0 -> [1]; 5 -> [1, 5, 10, 10, 5, 1]")

versions = {
"node": version(["node", "--version"]),
"npm": version(["npm", "--version"]),
"python": version(["python3", "--version"]),
}

print("Running:", " ".join(COMMAND))
result = subprocess.run(
COMMAND,
cwd=ROOT,
capture_output=True,
text=True,
check=False,
)

output = result.stdout + result.stderr
print(output)

match = re.search(r"Tests:\s+(\d+)\s+passed", output)
passed_tests = int(match.group(1)) if match else None

evidence = {
"timestamp_utc": datetime.now(timezone.utc).isoformat(),
"command": " ".join(COMMAND),
"versions": versions,
"exit_code": result.returncode,
"passed_tests": passed_tests,
"output": output,
}

EVIDENCE.mkdir(parents=True, exist_ok=True)
(EVIDENCE / "results.json").write_text(
json.dumps(evidence, indent=2) + "\n",
encoding="utf-8",
)

print("Evidence saved to work/R51-T026/results.json")
raise SystemExit(result.returncode)
15 changes: 15 additions & 0 deletions work/R51-T026/fixtures.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"task": "R51-T026",
"function": "pascalTriangle",
"precondition": "lineNumber is a nonnegative integer representing a zero-based row number",
"cases": [
{
"input": 0,
"expected": [1]
},
{
"input": 5,
"expected": [1, 5, 10, 10, 5, 1]
}
]
}
12 changes: 12 additions & 0 deletions work/R51-T026/results.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"timestamp_utc": "2026-11-29T13:34:36.172890+00:00",
"command": "npm test -- --runInBand",
"versions": {
"node": "v26.13.0",
"npm": "11.20.0",
"python": "Python 3.9.6"
},
"exit_code": 0,
"passed_tests": 588,
"output": "\n> javascript-algorithms-and-data-structures@0.0.4 test\n> jest --runInBand\n\nPASS src/data-structures/graph/__test__/Graph.test.js\nPASS src/data-structures/tree/red-black-tree/__test__/RedBlackTree.test.js\nPASS src/algorithms/math/matrix/__tests__/Matrix.test.js\nPASS src/data-structures/doubly-linked-list/__test__/DoublyLinkedList.test.js\nPASS src/data-structures/tree/__test__/BinaryTreeNode.test.js\nPASS src/algorithms/graph/floyd-warshall/__test__/floydWarshall.test.js\nPASS src/algorithms/graph/articulation-points/__test__/articulationPoints.test.js\nPASS src/data-structures/linked-list/__test__/LinkedList.test.js\nPASS src/data-structures/tree/avl-tree/__test__/AvlTRee.test.js\nPASS src/data-structures/tree/binary-search-tree/__test__/BinarySearchTreeNode.test.js\nPASS src/algorithms/math/complex-number/__test__/ComplexNumber.test.js\nPASS src/algorithms/graph/breadth-first-search/__test__/breadthFirstSearch.test.js\nPASS src/algorithms/graph/depth-first-search/__test__/depthFirstSearch.test.js\nPASS src/algorithms/sets/knapsack-problem/__test__/Knapsack.test.js\nPASS src/algorithms/graph/bridges/__test__/graphBridges.test.js\nPASS src/data-structures/graph/__test__/GraphVertex.test.js\nPASS src/data-structures/heap/__test__/MinHeap.test.js\nPASS src/algorithms/graph/eulerian-path/__test__/eulerianPath.test.js\nPASS src/data-structures/deque/__test__/Deque.test.js\nPASS src/data-structures/heap/__test__/MaxHeap.test.js\nPASS src/algorithms/graph/strongly-connected-components/__test__/stronglyConnectedComponents.test.js\nPASS src/data-structures/lru-cache/__test__/LRUCache.test.js\nPASS src/data-structures/lru-cache/__test__/LRUCacheOnMap.test.js\nPASS src/algorithms/math/fourier-transform/__test__/fastFourierTransform.test.js\nPASS src/data-structures/disjoint-set/__test__/DisjointSet.test.js\nPASS src/algorithms/tree/breadth-first-search/__test__/breadthFirstSearch.test.js\nPASS src/algorithms/tree/depth-first-search/__test__/depthFirstSearch.test.js\nPASS src/data-structures/tree/segment-tree/__test__/SegmentTree.test.js\nPASS src/algorithms/graph/bellman-ford/__test__/bellmanFord.test.js\nPASS src/algorithms/graph/dijkstra/__test__/dijkstra.test.js\nPASS src/algorithms/graph/hamiltonian-cycle/__test__/hamiltonianCycle.test.js\nPASS src/data-structures/hash-table/__test__/HashTable.test.js\nPASS src/data-structures/disjoint-set/__test__/DisjointSetItem.test.js\nPASS src/algorithms/math/prime-factors/__test__/primeFactors.test.js\nPASS src/data-structures/priority-queue/__test__/PriorityQueue.test.js\nPASS src/data-structures/tree/fenwick-tree/__test__/FenwickTree.test.js\nPASS src/algorithms/graph/kruskal/__test__/kruskal.test.js\nPASS src/algorithms/graph/prim/__test__/prim.test.js\nPASS src/data-structures/trie/__test__/Trie.test.js\nPASS src/algorithms/statistics/weighted-random/__test__/weightedRandom.test.js\nPASS src/algorithms/image-processing/seam-carving/__tests__/resizeImageWidth.node.js\nPASS src/data-structures/graph/__test__/GraphEdge.test.js\nPASS src/algorithms/math/square-root/__test__/squareRoot.test.js\nPASS src/algorithms/cryptography/polynomial-hash/__test__/PolynomialHash.test.js\nPASS src/data-structures/tree/binary-search-tree/__test__/BinarySearchTree.test.js\nPASS src/data-structures/trie/__test__/TrieNode.test.js\nPASS src/data-structures/heap/__test__/MinHeapAdhoc.test.js\nPASS src/data-structures/heap/__test__/MaxHeapAdhoc.test.js\nPASS src/algorithms/ml/knn/__test__/knn.test.js\nPASS src/algorithms/cryptography/polynomial-hash/__test__/SimplePolynomialHash.test.js\nPASS src/algorithms/sorting/counting-sort/__test__/CountingSort.test.js\nPASS src/algorithms/sets/combinations/__test__/combineWithoutRepetitions.test.js\nPASS src/data-structures/bloom-filter/__test__/BloomFilter.test.js\nPASS src/algorithms/string/rabin-karp/__test__/rabinKarp.test.js\nPASS src/utils/comparator/__test__/Comparator.test.js\nPASS src/algorithms/sets/permutations/__test__/permutateWithoutRepetitions.test.js\nPASS src/algorithms/string/regular-expression-matching/__test__/regularExpressionMatching.test.js\nPASS src/algorithms/graph/travelling-salesman/__test__/bfTravellingSalesman.test.js\nPASS src/data-structures/doubly-linked-list/__test__/DoublyLinkedListNode.test.js\nPASS src/algorithms/sets/combinations/__test__/combineWithRepetitions.test.js\nPASS src/algorithms/cryptography/hill-cipher/_test_/hillCipher.test.js\nPASS src/data-structures/stack/__test__/Stack.test.js\nPASS src/algorithms/uncategorized/best-time-to-buy-sell-stocks/__tests__/accumulatorBestTimeToBuySellStocks.test.js\nPASS src/algorithms/uncategorized/hanoi-tower/__test__/hanoiTower.test.js\nPASS src/algorithms/uncategorized/best-time-to-buy-sell-stocks/__tests__/peakvalleyBestTimeToBuySellStocks.test.js\nPASS src/algorithms/graph/topological-sorting/__test__/topologicalSort.test.js\nPASS src/algorithms/search/jump-search/__test__/jumpSearch.test.js\nPASS src/algorithms/sorting/insertion-sort/__test__/InsertionSort.test.js\nPASS src/algorithms/sorting/heap-sort/__test__/HeapSort.test.js\nPASS src/algorithms/sorting/bubble-sort/__test__/BubbleSort.test.js\nPASS src/algorithms/uncategorized/best-time-to-buy-sell-stocks/__tests__/dqBestTimeToBuySellStocks.test.js\nPASS src/algorithms/sorting/quick-sort/__test__/QuickSort.test.js\nPASS src/algorithms/sorting/merge-sort/__test__/MergeSort.test.js\nPASS src/algorithms/uncategorized/best-time-to-buy-sell-stocks/__tests__/dpBestTimeToBuySellStocks.test.js\nPASS src/algorithms/sorting/quick-sort/__test__/QuickSortInPlace.test.js\nPASS src/algorithms/sorting/selection-sort/__test__/SelectionSort.test.js\nPASS src/data-structures/queue/__test__/Queue.test.js\nPASS src/algorithms/search/interpolation-search/__test__/interpolationSearch.test.js\nPASS src/algorithms/sorting/shell-sort/__test__/ShellSort.test.js\nPASS src/algorithms/math/binary-floating-point/__tests__/bitsToFloat.test.js\nPASS src/algorithms/cryptography/caesar-cipher/__test__/caesarCipher.test.js\nPASS src/algorithms/cryptography/rail-fence-cipher/__test__/railFenceCipher.test.js\nPASS src/data-structures/linked-list/__test__/LinkedListNode.test.js\nPASS src/algorithms/search/binary-search/__test__/binarySearch.test.js\nPASS src/algorithms/graph/detect-cycle/__test__/detectDirectedCycle.test.js\nPASS src/algorithms/sets/permutations/__test__/permutateWithRepetitions.test.js\nPASS src/algorithms/math/euclidean-algorithm/__test__/euclideanAlgorithmIterative.test.js\nPASS src/algorithms/graph/detect-cycle/__test__/detectUndirectedCycleUsingDisjointSet.test.js\nPASS src/algorithms/graph/detect-cycle/__test__/detectUndirectedCycle.test.js\nPASS src/algorithms/sets/shortest-common-supersequence/__test__/shortestCommonSupersequence.test.js\nPASS src/algorithms/uncategorized/n-queens/__test__/nQueens.test.js\nPASS src/algorithms/sets/knapsack-problem/__test__/KnapsackItem.test.js\nPASS src/data-structures/disjoint-set/__test__/DisjointSetAdhoc.test.js\nPASS src/algorithms/string/longest-common-substring/__test__/longestCommonSubstring.test.js\nPASS src/algorithms/sorting/bucket-sort/__test__/BucketSort.test.js\nPASS src/algorithms/uncategorized/square-matrix-rotation/__test__/squareMatrixRotation.test.js\nPASS src/algorithms/ml/k-means/__test__/kMeans.test.js\nPASS src/algorithms/string/levenshtein-distance/__test__/levenshteinDistance.test.js\nPASS src/algorithms/math/primality-test/__test__/trialDivision.test.js\nPASS src/algorithms/math/horner-method/__test__/hornerMethod.test.js\nPASS src/algorithms/math/euclidean-algorithm/__test__/euclideanAlgorithm.test.js\nPASS src/algorithms/search/linear-search/__test__/linearSearch.test.js\nPASS src/algorithms/math/fibonacci/__test__/fibonacciNthClosedForm.test.js\nPASS src/algorithms/linked-list/reverse-traversal/__test__/reverseTraversal.test.js\nPASS src/algorithms/math/is-power-of-two/__test__/isPowerOfTwoBitwise.test.js\nPASS src/algorithms/sets/longest-common-subsequence/__test__/longestCommonSubsequence.test.js\nPASS src/algorithms/math/euclidean-distance/__tests__/euclideanDistance.test.js\nPASS src/algorithms/uncategorized/rain-terraces/__test__/dpRainTerraces.test.js\nPASS src/algorithms/uncategorized/rain-terraces/__test__/bfRainTerraces.test.js\nPASS src/algorithms/sets/longest-common-subsequence/__test__/longestCommonSubsequenceRecursive.test.js\nPASS src/algorithms/math/fast-powering/__test__/fastPowering.test.js\nPASS src/algorithms/sets/longest-increasing-subsequence/__test__/dpLongestIncreasingSubsequence.test.js\nPASS src/algorithms/sets/maximum-subarray/__test__/dpMaximumSubarray.test.js\nPASS src/algorithms/sets/maximum-subarray/__test__/bfMaximumSubarray.test.js\nPASS src/algorithms/math/fibonacci/__test__/fibonacciNth.test.js\nPASS src/algorithms/math/is-power-of-two/__test__/isPowerOfTwo.test.js\nPASS src/algorithms/sorting/radix-sort/__test__/RadixSort.test.js\nPASS src/algorithms/math/binary-floating-point/__tests__/floatAsBinaryString.test.js\nPASS src/algorithms/sets/maximum-subarray/__test__/dcMaximumSubarraySum.test.js\nPASS src/algorithms/string/z-algorithm/__test__/zAlgorithm.test.js\nPASS src/algorithms/uncategorized/jump-game/__test__/backtrackingJumpGame.test.js\nPASS src/algorithms/string/hamming-distance/__test__/hammingDistance.test.js\nPASS src/algorithms/uncategorized/jump-game/__test__/dpBottomUpJumpGame.test.js\nPASS src/algorithms/math/horner-method/__test__/classicPolynome.test.js\nPASS src/algorithms/uncategorized/jump-game/__test__/dpTopDownJumpGame.test.js\nPASS src/algorithms/stack/valid-parentheses/__test__/validParentheses.test.js\nPASS src/algorithms/uncategorized/recursive-staircase/__test__/recursiveStaircaseMEM.test.js\nPASS src/algorithms/math/least-common-multiple/__test__/leastCommonMultiple.test.js\nPASS src/algorithms/uncategorized/recursive-staircase/__test__/recursiveStaircaseDP.test.js\nPASS src/algorithms/string/knuth-morris-pratt/__test__/knuthMorrisPratt.test.js\nPASS src/algorithms/uncategorized/knight-tour/__test__/knightTour.test.js\nPASS src/algorithms/uncategorized/recursive-staircase/__test__/recursiveStaircaseBF.test.js\nPASS src/algorithms/uncategorized/recursive-staircase/__test__/recursiveStaircaseIT.test.js\nPASS src/algorithms/math/pascal-triangle/__test__/pascalTriangleRecursive.test.js\nPASS src/algorithms/math/bits/__test__/isPowerOfTwo.test.js\nPASS src/algorithms/uncategorized/jump-game/__test__/greedyJumpGame.test.js\nPASS src/algorithms/math/bits/__test__/countSetBits.test.js\nPASS src/algorithms/math/pascal-triangle/__test__/pascalTriangle.test.js\nPASS src/algorithms/math/fibonacci/__test__/fibonacci.test.js\nPASS src/algorithms/sets/fisher-yates/__test__/fisherYates.test.js\nPASS src/algorithms/math/bits/__test__/isPositive.test.js\nPASS src/algorithms/sets/cartesian-product/__test__/cartesianProduct.test.js\nPASS src/algorithms/math/bits/__test__/updateBit.test.js\nPASS src/algorithms/math/bits/__test__/isEven.test.js\nPASS src/algorithms/math/bits/__test__/fullAdder.test.js\nPASS src/algorithms/math/bits/__test__/multiply.test.js\nPASS src/algorithms/math/bits/__test__/multiplyUnsigned.test.js\nPASS src/algorithms/uncategorized/n-queens/__test__/QueensPosition.test.js\nPASS src/algorithms/linked-list/traversal/__test__/traversal.test.js\nPASS src/algorithms/math/bits/__test__/getBit.test.js\nPASS src/algorithms/sets/combination-sum/__test__/combinationSum.test.js\nPASS src/algorithms/string/palindrome/__test__/isPalindrome.test.js\nPASS src/algorithms/math/liu-hui/__test__/liuHui.test.js\nPASS src/algorithms/sets/power-set/__test__/caPowerSet.test.js\nPASS src/algorithms/math/integer-partition/__test__/integerPartition.test.js\nPASS src/algorithms/uncategorized/n-queens/__test__/nQueensBitwise.test.js\nPASS src/algorithms/math/bits/__test__/bitLength.test.js\nPASS src/algorithms/math/sieve-of-eratosthenes/__test__/sieveOfEratosthenes.test.js\nPASS src/algorithms/math/radian/__test__/radianToDegree.test.js\nPASS src/algorithms/math/radian/__test__/degreeToRadian.test.js\nPASS src/algorithms/math/bits/__test__/switchSign.test.js\nPASS src/algorithms/uncategorized/unique-paths/__test__/dpUniquePaths.test.js\nPASS src/algorithms/uncategorized/unique-paths/__test__/btUniquePaths.test.js\nPASS src/algorithms/math/bits/__test__/bitsDiff.test.js\nPASS src/algorithms/math/bits/__test__/multiplyByTwo.test.js\nPASS src/algorithms/uncategorized/unique-paths/__test__/uniquePaths.test.js\nPASS src/algorithms/sets/power-set/__test__/btPowerSet.test.js\nPASS src/algorithms/math/bits/__test__/clearBit.test.js\nPASS src/algorithms/sets/power-set/__test__/bwPowerSet.test.js\nPASS src/algorithms/math/factorial/__test__/factorialRecursive.test.js\nPASS src/algorithms/math/bits/__test__/divideByTwo.test.js\nPASS src/algorithms/math/bits/__test__/setBit.test.js\nPASS src/algorithms/math/fourier-transform/__test__/inverseDiscreteFourierTransform.test.js\nPASS src/algorithms/math/factorial/__test__/factorial.test.js\nPASS src/algorithms/math/fourier-transform/__test__/discreteFourierTransform.test.js\nPASS src/algorithms/sorting/__test__/Sort.test.js\nPASS src/data-structures/heap/__test__/Heap.test.js\nPASS src/playground/__test__/playground.test.js\n\nTest Suites: 178 passed, 178 total\nTests: 588 passed, 588 total\nSnapshots: 0 total\nTime: 3.521 s\nRan all test suites.\n"
}