diff --git a/String/CheckRearrangePalindrome.js b/String/CheckRearrangePalindrome.js index c3feb59f16..0056586592 100644 --- a/String/CheckRearrangePalindrome.js +++ b/String/CheckRearrangePalindrome.js @@ -1,10 +1,21 @@ /** * What is a palindrome? https://en.wikipedia.org/wiki/Palindrome - * Receives a string and returns whether it can be rearranged to become a palindrome or not - * The string can only be a palindrome if the count of ALL characters is even or if the ONLY ONE character count is odd - * Input is a string + * Receives a string and returns whether it can be rearranged to become a palindrome or not. + * The string can only be a palindrome if the count of ALL characters is even or if at most ONE character count is odd. * - **/ + * Determinism & Purity Contract: + * - Pure Function: `palindromeRearranging` has zero external dependencies (no time, randomness, network, or mutable globals). + * - Input Isolation & Repeatability: Repeated invocations with identical inputs yield identical outputs without side effects. + * - Normalization Policy: Character normalization (case-folding or whitespace trimming) is NOT performed by this function. + * Characters are counted by exact Unicode character / case sensitivity (e.g. 'A' !== 'a'). + * + * Preconditions: + * - Line 19: `if (typeof str !== 'string')` returns sentinel string `'Not a string'`. + * - Line 23: `if (!str)` returns sentinel string `'Empty string'`. + * + * @param {string} str - Input string to evaluate. + * @returns {boolean|string} True if characters can form a palindrome, false otherwise; or sentinel string on invalid/empty input. + */ export const palindromeRearranging = (str) => { // check that input is a string @@ -27,11 +38,3 @@ export const palindromeRearranging = (str) => { Object.values(charCounts).filter((count) => count % 2 !== 0).length <= 1 ) } - -// testing - -// > palindromeRearranging('aaeccrr') -// true - -// > palindromeRearranging('leve') -// false diff --git a/String/test/CheckRearrangePalindrome.test.js b/String/test/CheckRearrangePalindrome.test.js index 09429b7257..baee35a516 100644 --- a/String/test/CheckRearrangePalindrome.test.js +++ b/String/test/CheckRearrangePalindrome.test.js @@ -1,25 +1,112 @@ import { palindromeRearranging } from '../CheckRearrangePalindrome' -test('palindromeRearranging(apple) -> false', () => { - const word = 'apple' - const res = palindromeRearranging(word) - expect(res).toBeFalsy() -}) +describe('palindromeRearranging', () => { + describe('Existing tests', () => { + test('palindromeRearranging(apple) -> false', () => { + const word = 'apple' + const res = palindromeRearranging(word) + expect(res).toBeFalsy() + }) -test('palindromeRearranging(aapplle) -> true', () => { - const word = 'aapplle' - const res = palindromeRearranging(word) - expect(res).toBeTruthy() -}) + test('palindromeRearranging(aapplle) -> true', () => { + const word = 'aapplle' + const res = palindromeRearranging(word) + expect(res).toBeTruthy() + }) -test('palindromeRearranging(value) -> false', () => { - const word = 'value' - const res = palindromeRearranging(word) - expect(res).toBeFalsy() -}) + test('palindromeRearranging(value) -> false', () => { + const word = 'value' + const res = palindromeRearranging(word) + expect(res).toBeFalsy() + }) + + test('palindromeRearranging(aaeccrr) -> true', () => { + const word = 'aaeccrr' + const res = palindromeRearranging(word) + expect(res).toBeTruthy() + }) + }) + + describe('Stated Fixture: ["", "a", "aba", "ab", "abba"]', () => { + it('handles empty string "" returning "Empty string" sentinel (Precondition line 23: !str)', () => { + const input = '' + const expected = 'Empty string' + const result = palindromeRearranging(input) + expect(result).toBe(expected) + }) + + it('handles single character "a" returning true', () => { + const input = 'a' + const expected = true + const result = palindromeRearranging(input) + expect(result).toBe(expected) + }) + + it('handles odd palindrome "aba" returning true', () => { + const input = 'aba' + const expected = true + const result = palindromeRearranging(input) + expect(result).toBe(expected) + }) + + it('handles non-rearrangeable "ab" returning false', () => { + const input = 'ab' + const expected = false + const result = palindromeRearranging(input) + expect(result).toBe(expected) + }) + + it('handles even palindrome "abba" returning true', () => { + const input = 'abba' + const expected = true + const result = palindromeRearranging(input) + expect(result).toBe(expected) + }) + }) + + describe('Normalization Specification: No case-folding or trimming performed', () => { + it('treats uppercase and lowercase characters distinctly (e.g., "Aa" has 2 odd counts -> false)', () => { + expect(palindromeRearranging('Aa')).toBe(false) + expect(palindromeRearranging('AAaa')).toBe(true) + }) + + it('treats whitespace characters as distinct characters', () => { + expect(palindromeRearranging('a a')).toBe(true) + expect(palindromeRearranging('a b')).toBe(false) + expect(palindromeRearranging('a b ')).toBe(false) + }) + }) + + describe('Deterministic Execution, Repeatability & Input Isolation', () => { + it('runs the same fixture twice in fresh isolated state and once after another valid input, asserting identical results', () => { + const fixtureInputs = ['', 'a', 'aba', 'ab', 'abba'] + const expectedOutputs = ['Empty string', true, true, false, true] + + // Run 1: initial run + const run1 = fixtureInputs.map((s) => palindromeRearranging(s)) + expect(run1).toEqual(expectedOutputs) + + // Run 2: immediate repeated run + const run2 = fixtureInputs.map((s) => palindromeRearranging(s)) + expect(run2).toEqual(run1) + + // Interleaved run with different valid inputs + palindromeRearranging('racecar') + palindromeRearranging('random_unrelated_string') + palindromeRearranging('xyzzyx') + + // Run 3: run after interleaved execution + const run3 = fixtureInputs.map((s) => palindromeRearranging(s)) + expect(run3).toEqual(run1) + }) -test('palindromeRearranging(aaeccrr) -> true', () => { - const word = 'aaeccrr' - const res = palindromeRearranging(word) - expect(res).toBeTruthy() + it('returns "Not a string" sentinel on non-string inputs (Precondition line 19: typeof str !== "string")', () => { + expect(palindromeRearranging(0)).toBe('Not a string') + expect(palindromeRearranging(121)).toBe('Not a string') + expect(palindromeRearranging(null)).toBe('Not a string') + expect(palindromeRearranging(undefined)).toBe('Not a string') + expect(palindromeRearranging({})).toBe('Not a string') + expect(palindromeRearranging([])).toBe('Not a string') + }) + }) }) diff --git a/work/R50-T045/check.py b/work/R50-T045/check.py new file mode 100644 index 0000000000..6c17370239 --- /dev/null +++ b/work/R50-T045/check.py @@ -0,0 +1,291 @@ +#!/usr/bin/env python3 +""" +Automated check runner for Task R50-T045: +Make verification repeatable for palindromeRearranging (CheckRearrangePalindrome.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 -- String/test/CheckRearrangePalindrome.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_repeatability_and_purity_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 { palindromeRearranging } from './String/CheckRearrangePalindrome.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: +// String/CheckRearrangePalindrome.js Line 19: "if (typeof str !== 'string') return 'Not a string'" +// String/CheckRearrangePalindrome.js Line 23: "if (!str) return 'Empty string'" + +// 1. Stated Fixture checks: ["", "a", "aba", "ab", "abba"] +const fixtureCases = [ + { input: '', expected: 'Empty string', line: 'String/CheckRearrangePalindrome.js:23', desc: 'empty string' }, + { input: 'a', expected: true, line: 'String/CheckRearrangePalindrome.js:19,23', desc: 'single character "a"' }, + { input: 'aba', expected: true, line: 'String/CheckRearrangePalindrome.js:19,23', desc: 'odd palindrome "aba"' }, + { input: 'ab', expected: false, line: 'String/CheckRearrangePalindrome.js:19,23', desc: 'non-palindrome "ab"' }, + { input: 'abba', expected: true, line: 'String/CheckRearrangePalindrome.js:19,23', desc: 'even palindrome "abba"' } +] + +for (const fc of fixtureCases) { + try { + const actual = palindromeRearranging(fc.input) + const passed = actual === fc.expected + recordCheck( + `Stated Fixture: ${fc.desc}`, + fc.line, + JSON.stringify(fc.input), + JSON.stringify(fc.expected), + JSON.stringify(actual), + passed + ) + } catch (e) { + recordCheck(`Stated Fixture: ${fc.desc}`, fc.line, JSON.stringify(fc.input), JSON.stringify(fc.expected), e.message, false) + } +} + +// 2. Normalization Specification Verification +// Normalization is NOT performed: case is preserved, so 'Aa' (1 'A', 1 'a') has 2 odd counts -> false +try { + const actualAa = palindromeRearranging('Aa') + const actualAAaa = palindromeRearranging('AAaa') + const passedNorm = actualAa === false && actualAAaa === true + recordCheck( + 'Normalization Policy: Case-sensitive, no normalization performed', + 'String/CheckRearrangePalindrome.js:28-33', + '["Aa", "AAaa"]', + 'Aa -> false, AAaa -> true', + `Aa -> ${actualAa}, AAaa -> ${actualAAaa}`, + passedNorm + ) +} catch (e) { + recordCheck('Normalization Policy', 'String/CheckRearrangePalindrome.js:28-33', 'Aa', 'false', e.message, false) +} + +// 3. Repeatability & Input Isolation: Run twice fresh, once after interleaved +const fixtureInputs = ['', 'a', 'aba', 'ab', 'abba'] +const run1 = fixtureInputs.map(s => palindromeRearranging(s)) +const run2 = fixtureInputs.map(s => palindromeRearranging(s)) + +// Interleaved execution with distinct inputs +palindromeRearranging('racecar') +palindromeRearranging('not_a_palindrome_12345') +palindromeRearranging('xyzzyx') + +const run3 = fixtureInputs.map(s => palindromeRearranging(s)) + +const run1VsRun2 = JSON.stringify(run1) === JSON.stringify(run2) +const run1VsRun3 = JSON.stringify(run1) === JSON.stringify(run3) +const repeatabilityPassed = run1VsRun2 && run1VsRun3 + +recordCheck( + 'Repeatability & Input Isolation: 3 consecutive & interleaved executions', + 'String/CheckRearrangePalindrome.js:28-35', + 'fixture run1 (fresh), run2 (immediate repeat), run3 (after interleaved calls)', + 'run1 === run2 === run3 (identical outputs)', + `run1 == run2: ${run1VsRun2}, run1 == run3: ${run1VsRun3}`, + repeatabilityPassed, + { run1, run2, run3 } +) + +// 4. Precondition sentinel checks on non-string inputs +const nonStringCases = [0, 121, null, undefined, {}, []] +let allNonStringsPassed = true +for (const ns of nonStringCases) { + if (palindromeRearranging(ns) !== 'Not a string') { + allNonStringsPassed = false + } +} + +recordCheck( + 'Precondition enforcement: Non-string inputs return "Not a string" sentinel', + 'String/CheckRearrangePalindrome.js:19', + '[0, 121, null, undefined, {}, []]', + '"Not a string" for all non-strings', + allNonStringsPassed ? 'Returned "Not a string" for all' : 'Unexpected return value', + allNonStringsPassed +) + +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-T045 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. Repeatability, purity & fixture checks + print("\n--- Repeatability & Purity Checks (Citing Source Preconditions) ---") + purity_res = run_repeatability_and_purity_checks(repo_root) + for c in purity_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 purity_res.get("allPassed", False): + print("\nERROR: Repeatability & purity checks failed.") + sys.exit(1) + + # 4. Save results to work/R50-T045/results.json + results_payload = { + "task_id": "R50-T045", + "timestamp": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()), + "preconditions": { + "source_file": "String/CheckRearrangePalindrome.js", + "type_check_line": "19: if (typeof str !== 'string') return 'Not a string'", + "empty_check_line": "23: if (!str) return 'Empty string'", + "normalization_policy": "No normalization performed (case-sensitive exact character counting)" + }, + "tools": tools, + "native_tests": { + "command": native_res["command"], + "exit_code": native_res["exit_code"], + "passed": native_res["passed"], + "test_count": native_res["test_count"] + }, + "repeatability_checks": purity_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-T045/results.json b/work/R50-T045/results.json new file mode 100644 index 0000000000..e6cc17c27c --- /dev/null +++ b/work/R50-T045/results.json @@ -0,0 +1,102 @@ +{ + "task_id": "R50-T045", + "timestamp": "2026-11-22T06:41:37Z", + "preconditions": { + "source_file": "String/CheckRearrangePalindrome.js", + "type_check_line": "19: if (typeof str !== 'string') return 'Not a string'", + "empty_check_line": "23: if (!str) return 'Empty string'", + "normalization_policy": "No normalization performed (case-sensitive exact character counting)" + }, + "tools": { + "python": "3.15.2", + "node": "v26.13.1", + "npm": "11.20.0", + "git_branch": "student/e26b07f0827/R50-T045", + "git_commit": "5c39e87a9a31f279c60f830ad74a845e4788a517" + }, + "native_tests": { + "command": "npm test -- String/test/CheckRearrangePalindrome.test.js", + "exit_code": 0, + "passed": true, + "test_count": 13 + }, + "repeatability_checks": [ + { + "name": "Stated Fixture: empty string", + "precondition_source_line": "String/CheckRearrangePalindrome.js:23", + "input": "\"\"", + "expected": "\"Empty string\"", + "actual": "\"Empty string\"", + "passed": true, + "details": {} + }, + { + "name": "Stated Fixture: single character \"a\"", + "precondition_source_line": "String/CheckRearrangePalindrome.js:19,23", + "input": "\"a\"", + "expected": "true", + "actual": "true", + "passed": true, + "details": {} + }, + { + "name": "Stated Fixture: odd palindrome \"aba\"", + "precondition_source_line": "String/CheckRearrangePalindrome.js:19,23", + "input": "\"aba\"", + "expected": "true", + "actual": "true", + "passed": true, + "details": {} + }, + { + "name": "Stated Fixture: non-palindrome \"ab\"", + "precondition_source_line": "String/CheckRearrangePalindrome.js:19,23", + "input": "\"ab\"", + "expected": "false", + "actual": "false", + "passed": true, + "details": {} + }, + { + "name": "Stated Fixture: even palindrome \"abba\"", + "precondition_source_line": "String/CheckRearrangePalindrome.js:19,23", + "input": "\"abba\"", + "expected": "true", + "actual": "true", + "passed": true, + "details": {} + }, + { + "name": "Normalization Policy: Case-sensitive, no normalization performed", + "precondition_source_line": "String/CheckRearrangePalindrome.js:28-33", + "input": "[\"Aa\", \"AAaa\"]", + "expected": "Aa -> false, AAaa -> true", + "actual": "Aa -> false, AAaa -> true", + "passed": true, + "details": {} + }, + { + "name": "Repeatability & Input Isolation: 3 consecutive & interleaved executions", + "precondition_source_line": "String/CheckRearrangePalindrome.js:28-35", + "input": "fixture run1 (fresh), run2 (immediate repeat), run3 (after interleaved calls)", + "expected": "run1 === run2 === run3 (identical outputs)", + "actual": "run1 == run2: true, run1 == run3: true", + "passed": true, + "details": { + "run1": ["Empty string", true, true, false, true], + "run2": ["Empty string", true, true, false, true], + "run3": ["Empty string", true, true, false, true] + } + }, + { + "name": "Precondition enforcement: Non-string inputs return \"Not a string\" sentinel", + "precondition_source_line": "String/CheckRearrangePalindrome.js:19", + "input": "[0, 121, null, undefined, {}, []]", + "expected": "\"Not a string\" for all non-strings", + "actual": "Returned \"Not a string\" for all", + "passed": true, + "details": {} + } + ], + "all_passed": true +}