From cdb1e82733228619ad1e326057ceaf3e91a9ef19 Mon Sep 17 00:00:00 2001 From: Aniruddha Bongale Date: Sun, 22 Nov 2026 13:05:39 +0530 Subject: [PATCH] refactor(maths): simplify state variables in lucas recurrence (R50-T046) --- Maths/LucasSeries.js | 41 +++-- Maths/test/LucasSeries.test.js | 71 ++++++- work/R50-T046/check.py | 328 +++++++++++++++++++++++++++++++++ work/R50-T046/results.json | 80 ++++++++ 4 files changed, 495 insertions(+), 25 deletions(-) create mode 100644 work/R50-T046/check.py create mode 100644 work/R50-T046/results.json diff --git a/Maths/LucasSeries.js b/Maths/LucasSeries.js index e645872e2d..da6d0031f6 100644 --- a/Maths/LucasSeries.js +++ b/Maths/LucasSeries.js @@ -1,17 +1,20 @@ -/* - Program to get the Nth Lucas Number - Article on Lucas Number: https://en.wikipedia.org/wiki/Lucas_number - Examples: - > loopLucas(1) - 1 - > loopLucas(20) - 15127 - > loopLucas(100) - 792070839848372100000 -*/ - /** - * @param {Number} index The position of the number you want to get from the Lucas Series + * Calculates the Nth Lucas Number. + * The Lucas sequence is defined as L_0 = 2, L_1 = 1, and L_n = L_{n-1} + L_{n-2} for n >= 2. + * + * Preconditions: + * - Line 21: `if (index < 0)` throws TypeError('Index cannot be Negative') + * - Line 24: `if (Math.floor(index) !== index)` throws TypeError('Index cannot be a Decimal') + * + * Refactoring Note: + * Clarified state management in the iterative recurrence calculation by replacing + * ambiguous variables `a`, `b`, and `temp` with semantically descriptive variables + * `current` (representing L_i) and `next` (representing L_{i+1}). + * + * @param {number} index The 0-based position of the number in the Lucas sequence. + * @returns {number} The Nth Lucas Number. + * @throws {TypeError} If index is negative or decimal. + * @see https://en.wikipedia.org/wiki/Lucas_number */ function lucas(index) { // index can't be negative @@ -21,14 +24,14 @@ function lucas(index) { if (Math.floor(index) !== index) throw new TypeError('Index cannot be a Decimal') - let a = 2 - let b = 1 + let current = 2 + let next = 1 for (let i = 0; i < index; i++) { - const temp = a + b - a = b - b = temp + const nextLucas = current + next + current = next + next = nextLucas } - return a + return current } export { lucas } diff --git a/Maths/test/LucasSeries.test.js b/Maths/test/LucasSeries.test.js index 21d6d09042..9cb55d2004 100644 --- a/Maths/test/LucasSeries.test.js +++ b/Maths/test/LucasSeries.test.js @@ -1,15 +1,74 @@ import { lucas } from '../LucasSeries' describe('Nth Lucas Number', () => { - it('should return the 20th Lucas Number', () => { - expect(lucas(20)).toBe(15127) + describe('Existing tests', () => { + it('should return the 20th Lucas Number', () => { + expect(lucas(20)).toBe(15127) + }) + + it('should return the 0th Lucas Number', () => { + expect(lucas(0)).toBe(2) + }) + + it('should return the 100th Lucas Number', () => { + expect(lucas(100)).toBe(792070839848372100000) + }) }) - it('should return the 20th Lucas Number', () => { - expect(lucas(0)).toBe(2) + describe('Stated Fixture: Smallest valid input and ordinary input', () => { + it('handles smallest valid index: 0 (Precondition line 21: index >= 0)', () => { + const input = 0 + // Independently calculated: L_0 = 2 + const expected = 2 + const result = lucas(input) + + expect(result).toBe(expected) + }) + + it('handles ordinary input: 5 (Precondition line 21 & 24)', () => { + const input = 5 + // Independently calculated: L_0=2, L_1=1, L_2=3, L_3=4, L_4=7, L_5=11 + const expected = 11 + const result = lucas(input) + + expect(result).toBe(expected) + }) + + it('handles ordinary input: 20', () => { + const input = 20 + const expected = 15127 + const result = lucas(input) + + expect(result).toBe(expected) + }) }) - it('should return the 20th Lucas Number', () => { - expect(lucas(100)).toBe(792070839848372100000) + describe('Recurrence Sequence & Invariant Checks', () => { + it('verifies the first 8 Lucas numbers explicitly: [2, 1, 3, 4, 7, 11, 18, 29]', () => { + const expectedSeries = [2, 1, 3, 4, 7, 11, 18, 29] + for (let n = 0; n < expectedSeries.length; n++) { + expect(lucas(n)).toBe(expectedSeries[n]) + } + }) + + it('verifies the recurrence invariant L_n = L_{n-1} + L_{n-2} for n >= 2', () => { + for (let n = 2; n <= 25; n++) { + expect(lucas(n)).toBe(lucas(n - 1) + lucas(n - 2)) + } + }) + }) + + describe('Precondition enforcement: Negative and Decimal inputs', () => { + it('throws TypeError("Index cannot be Negative") for negative numbers (Line 21)', () => { + expect(() => lucas(-1)).toThrow(TypeError) + expect(() => lucas(-1)).toThrow('Index cannot be Negative') + expect(() => lucas(-10)).toThrow('Index cannot be Negative') + }) + + it('throws TypeError("Index cannot be a Decimal") for non-integer numbers (Line 24)', () => { + expect(() => lucas(0.5)).toThrow(TypeError) + expect(() => lucas(0.5)).toThrow('Index cannot be a Decimal') + expect(() => lucas(3.14159)).toThrow('Index cannot be a Decimal') + }) }) }) diff --git a/work/R50-T046/check.py b/work/R50-T046/check.py new file mode 100644 index 0000000000..6d23577004 --- /dev/null +++ b/work/R50-T046/check.py @@ -0,0 +1,328 @@ +#!/usr/bin/env python3 +""" +Automated check runner for Task R50-T046: +Simplify one responsibility in lucas (LucasSeries.js) +""" + +import json +import os +import subprocess +import sys +import time + +def get_tool_versions(repo_root): + env = os.environ.copy() + if "/opt/homebrew/bin" not in env.get("PATH", ""): + env["PATH"] = f"/opt/homebrew/bin:{env.get('PATH', '')}" + + def run_cmd(cmd): + try: + res = subprocess.run(cmd, shell=True, cwd=repo_root, capture_output=True, text=True, env=env) + return res.stdout.strip() + except Exception as e: + return str(e) + + node_ver = run_cmd("node -v") + npm_ver = run_cmd("npm -v") + git_branch = run_cmd("git rev-parse --abbrev-ref HEAD") + git_commit = run_cmd("git rev-parse HEAD") + python_ver = sys.version.split()[0] + + return { + "python": python_ver, + "node": node_ver, + "npm": npm_ver, + "git_branch": git_branch, + "git_commit": git_commit + } + +def run_native_tests(repo_root): + env = os.environ.copy() + if "/opt/homebrew/bin" not in env.get("PATH", ""): + env["PATH"] = f"/opt/homebrew/bin:{env.get('PATH', '')}" + + cmd = "npm test -- Maths/test/LucasSeries.test.js" + proc = subprocess.run(cmd, shell=True, cwd=repo_root, capture_output=True, text=True, env=env) + + output = proc.stdout + "\n" + proc.stderr + passed = proc.returncode == 0 + + test_count = 0 + for line in output.splitlines(): + if "Tests" in line and "passed" in line: + parts = line.strip().split() + for p in parts: + if p.isdigit(): + test_count = int(p) + break + + return { + "command": cmd, + "exit_code": proc.returncode, + "passed": passed, + "test_count": test_count, + "output": output + } + +def run_refactoring_and_contract_checks(repo_root): + env = os.environ.copy() + if "/opt/homebrew/bin" not in env.get("PATH", ""): + env["PATH"] = f"/opt/homebrew/bin:{env.get('PATH', '')}" + + js_code = """ +import { lucas } from './Maths/LucasSeries.js' + +const results = { + checks: [], + allPassed: true +} + +function recordCheck(name, preconditionLine, inputDesc, expected, actual, passed, details = {}) { + results.checks.push({ + name, + precondition_source_line: preconditionLine, + input: inputDesc, + expected, + actual, + passed, + details + }) + if (!passed) { + results.allPassed = false + } +} + +// Precondition Source Lines: +// Maths/LucasSeries.js Line 21: "if (index < 0) throw new TypeError('Index cannot be Negative')" +// Maths/LucasSeries.js Line 24: "if (Math.floor(index) !== index) throw new TypeError('Index cannot be a Decimal')" + +// Check 1: Smallest valid input: index = 0 -> L_0 = 2 +try { + const input = 0 + const expected = 2 + const actual = lucas(input) + const passed = actual === expected + recordCheck( + 'Smallest valid input: index 0 (L_0)', + 'Maths/LucasSeries.js:21', + '0', + String(expected), + String(actual), + passed + ) +} catch (e) { + recordCheck('Smallest valid input', 'Maths/LucasSeries.js:21', '0', '2', e.message, false) +} + +// Check 2: Ordinary input: index = 5 -> L_5 = 11 +try { + const input = 5 + const expected = 11 + const actual = lucas(input) + const passed = actual === expected + recordCheck( + 'Ordinary input: index 5 (L_5)', + 'Maths/LucasSeries.js:21,24', + '5', + String(expected), + String(actual), + passed + ) +} catch (e) { + recordCheck('Ordinary input', 'Maths/LucasSeries.js:21,24', '5', '11', e.message, false) +} + +// Check 3: Ordinary input: index = 20 -> L_20 = 15127 +try { + const input = 20 + const expected = 15127 + const actual = lucas(input) + const passed = actual === expected + recordCheck( + 'Ordinary input: index 20 (L_20)', + 'Maths/LucasSeries.js:21,24', + '20', + String(expected), + String(actual), + passed + ) +} catch (e) { + recordCheck('Ordinary input (20)', 'Maths/LucasSeries.js:21,24', '20', '15127', e.message, false) +} + +// Check 4: Recurrence series correctness for first 8 terms: [2, 1, 3, 4, 7, 11, 18, 29] +try { + const expectedSeries = [2, 1, 3, 4, 7, 11, 18, 29] + const actualSeries = [] + let seriesOk = true + for (let n = 0; n < expectedSeries.length; n++) { + const val = lucas(n) + actualSeries.push(val) + if (val !== expectedSeries[n]) seriesOk = false + } + recordCheck( + 'Recurrence sequence terms 0..7', + 'Maths/LucasSeries.js:27-34', + 'indices 0..7', + JSON.stringify(expectedSeries), + JSON.stringify(actualSeries), + seriesOk + ) +} catch (e) { + recordCheck('Recurrence sequence', 'Maths/LucasSeries.js:27-34', '0..7', 'correct series', e.message, false) +} + +// Check 5: Recurrence Invariant: L_n === L_{n-1} + L_{n-2} for n in [2..20] +try { + let invariantOk = true + for (let n = 2; n <= 20; n++) { + if (lucas(n) !== lucas(n - 1) + lucas(n - 2)) { + invariantOk = false + } + } + recordCheck( + 'Recurrence invariant: L_n === L_{n-1} + L_{n-2} (n = 2..20)', + 'Maths/LucasSeries.js:27-34', + 'n in [2..20]', + 'L_n === L_{n-1} + L_{n-2} for all n', + invariantOk ? 'Invariant held for all 19 steps' : 'Invariant broken', + invariantOk + ) +} catch (e) { + recordCheck('Recurrence invariant', 'Maths/LucasSeries.js:27-34', 'n in 2..20', 'invariant holds', e.message, false) +} + +// Check 6: Precondition enforcement (Negative & Decimal inputs) +try { + let threwNeg = false + try { + lucas(-1) + } catch (e) { + if (e instanceof TypeError && e.message === 'Index cannot be Negative') threwNeg = true + } + + let threwDec = false + try { + lucas(3.5) + } catch (e) { + if (e instanceof TypeError && e.message === 'Index cannot be a Decimal') threwDec = true + } + + const precondOk = threwNeg && threwDec + recordCheck( + 'Precondition enforcement: Negative & Decimal guards', + 'Maths/LucasSeries.js:21,24', + 'index = -1 and index = 3.5', + 'Throws TypeError("Index cannot be Negative") and TypeError("Index cannot be a Decimal")', + `threwNeg: ${threwNeg}, threwDec: ${threwDec}`, + precondOk + ) +} catch (e) { + recordCheck('Precondition enforcement', 'Maths/LucasSeries.js:21,24', 'invalid inputs', 'throws TypeErrors', e.message, false) +} + +console.log(JSON.stringify(results)) +""" + + proc = subprocess.run( + ["node", "--input-type=module", "-e", js_code], + cwd=repo_root, + capture_output=True, + text=True, + env=env + ) + + if proc.returncode != 0: + return { + "allPassed": False, + "error": proc.stderr or proc.stdout, + "checks": [] + } + + try: + return json.loads(proc.stdout) + except Exception as e: + return { + "allPassed": False, + "error": f"Failed to parse JSON output: {str(e)}: {proc.stdout}", + "checks": [] + } + +def main(): + script_dir = os.path.dirname(os.path.abspath(__file__)) + repo_root = os.path.abspath(os.path.join(script_dir, "..", "..")) + + print("=== Running Task R50-T046 Automated Checks ===") + print(f"Repository Root: {repo_root}") + + # 1. Tool versions + tools = get_tool_versions(repo_root) + print("\n--- Tool Versions ---") + for k, v in tools.items(): + print(f" {k}: {v}") + + # 2. Native repository tests + print("\n--- Native Repository Tests ---") + native_res = run_native_tests(repo_root) + print(f" Command: {native_res['command']}") + print(f" Exit Code: {native_res['exit_code']}") + print(f" Passed: {native_res['passed']}") + print(f" Nonzero Test Count: {native_res['test_count']}") + + if not native_res["passed"] or native_res["test_count"] == 0: + print("\nERROR: Native repository tests failed or zero tests found.") + print(native_res["output"]) + sys.exit(1) + + # 3. Refactoring and contract checks + print("\n--- Refactoring & Contract Checks (Citing Source Preconditions) ---") + refactor_res = run_refactoring_and_contract_checks(repo_root) + for c in refactor_res.get("checks", []): + status = "PASS" if c["passed"] else "FAIL" + print(f" [{status}] {c['name']}") + print(f" Precondition Line: {c['precondition_source_line']}") + print(f" Input: {c['input']}") + print(f" Expected: {c['expected']}") + print(f" Actual: {c['actual']}") + + if not refactor_res.get("allPassed", False): + print("\nERROR: Refactoring & contract checks failed.") + sys.exit(1) + + # 4. Save results to work/R50-T046/results.json + results_payload = { + "task_id": "R50-T046", + "timestamp": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()), + "refactoring_summary": { + "source_file": "Maths/LucasSeries.js", + "refactoring_operation": "Replaced generic variables 'a', 'b', and 'temp' with semantic state variables 'current' (L_i) and 'next' (L_{i+1}) to clarify recurrence state transitions.", + "negative_check_line": "21: if (index < 0) throw new TypeError('Index cannot be Negative')", + "decimal_check_line": "24: if (Math.floor(index) !== index) throw new TypeError('Index cannot be a Decimal')" + }, + "tools": tools, + "native_tests": { + "command": native_res["command"], + "exit_code": native_res["exit_code"], + "passed": native_res["passed"], + "test_count": native_res["test_count"] + }, + "contract_checks": refactor_res["checks"], + "all_passed": True + } + + results_path = os.path.join(script_dir, "results.json") + with open(results_path, "w") as f: + json.dump(results_payload, f, indent=2) + + # Prettier format results.json to avoid pre-commit hook failure + env = os.environ.copy() + if "/opt/homebrew/bin" not in env.get("PATH", ""): + env["PATH"] = f"/opt/homebrew/bin:{env.get('PATH', '')}" + subprocess.run(["npx", "prettier", results_path, "--write"], cwd=repo_root, capture_output=True, env=env) + + print(f"\nAll checks passed successfully!") + print(f"Results saved to: {results_path}") + sys.exit(0) + +if __name__ == "__main__": + main() diff --git a/work/R50-T046/results.json b/work/R50-T046/results.json new file mode 100644 index 0000000000..e5864aa07b --- /dev/null +++ b/work/R50-T046/results.json @@ -0,0 +1,80 @@ +{ + "task_id": "R50-T046", + "timestamp": "2026-11-22T07:35:30Z", + "refactoring_summary": { + "source_file": "Maths/LucasSeries.js", + "refactoring_operation": "Replaced generic variables 'a', 'b', and 'temp' with semantic state variables 'current' (L_i) and 'next' (L_{i+1}) to clarify recurrence state transitions.", + "negative_check_line": "21: if (index < 0) throw new TypeError('Index cannot be Negative')", + "decimal_check_line": "24: if (Math.floor(index) !== index) throw new TypeError('Index cannot be a Decimal')" + }, + "tools": { + "python": "3.15.2", + "node": "v26.13.1", + "npm": "11.20.0", + "git_branch": "student/e26b07f0827/R50-T046", + "git_commit": "5c39e87a9a31f279c60f830ad74a845e4788a517" + }, + "native_tests": { + "command": "npm test -- Maths/test/LucasSeries.test.js", + "exit_code": 0, + "passed": true, + "test_count": 10 + }, + "contract_checks": [ + { + "name": "Smallest valid input: index 0 (L_0)", + "precondition_source_line": "Maths/LucasSeries.js:21", + "input": "0", + "expected": "2", + "actual": "2", + "passed": true, + "details": {} + }, + { + "name": "Ordinary input: index 5 (L_5)", + "precondition_source_line": "Maths/LucasSeries.js:21,24", + "input": "5", + "expected": "11", + "actual": "11", + "passed": true, + "details": {} + }, + { + "name": "Ordinary input: index 20 (L_20)", + "precondition_source_line": "Maths/LucasSeries.js:21,24", + "input": "20", + "expected": "15127", + "actual": "15127", + "passed": true, + "details": {} + }, + { + "name": "Recurrence sequence terms 0..7", + "precondition_source_line": "Maths/LucasSeries.js:27-34", + "input": "indices 0..7", + "expected": "[2,1,3,4,7,11,18,29]", + "actual": "[2,1,3,4,7,11,18,29]", + "passed": true, + "details": {} + }, + { + "name": "Recurrence invariant: L_n === L_{n-1} + L_{n-2} (n = 2..20)", + "precondition_source_line": "Maths/LucasSeries.js:27-34", + "input": "n in [2..20]", + "expected": "L_n === L_{n-1} + L_{n-2} for all n", + "actual": "Invariant held for all 19 steps", + "passed": true, + "details": {} + }, + { + "name": "Precondition enforcement: Negative & Decimal guards", + "precondition_source_line": "Maths/LucasSeries.js:21,24", + "input": "index = -1 and index = 3.5", + "expected": "Throws TypeError(\"Index cannot be Negative\") and TypeError(\"Index cannot be a Decimal\")", + "actual": "threwNeg: true, threwDec: true", + "passed": true, + "details": {} + } + ], + "all_passed": true +}