Skip to content
Open
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
27 changes: 15 additions & 12 deletions String/CheckRearrangePalindrome.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
125 changes: 106 additions & 19 deletions String/test/CheckRearrangePalindrome.test.js
Original file line number Diff line number Diff line change
@@ -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')
})
})
})
Loading