diff --git a/maths/is_int_palindrome.py b/maths/is_int_palindrome.py index 63dc9e2138e8..08520fec6492 100644 --- a/maths/is_int_palindrome.py +++ b/maths/is_int_palindrome.py @@ -15,7 +15,30 @@ def is_int_palindrome(num: int) -> bool: True >>> is_int_palindrome(120) False + >>> is_int_palindrome(7) + True + >>> is_int_palindrome(1210) + False + >>> is_int_palindrome(1234567890987654321) + True + >>> is_int_palindrome(12345678909876543210) + False + >>> is_int_palindrome("121") + Traceback (most recent call last): + ... + TypeError: Input must be an integer + >>> is_int_palindrome(12.21) + Traceback (most recent call last): + ... + TypeError: Input must be an integer + >>> is_int_palindrome(True) + Traceback (most recent call last): + ... + TypeError: Input must be an integer """ + if not isinstance(num, int) or isinstance(num, bool): + raise TypeError("Input must be an integer") + if num < 0: return False diff --git a/work/R23-T001/PROPERTIES.md b/work/R23-T001/PROPERTIES.md new file mode 100644 index 000000000000..b399b3a5b5ed --- /dev/null +++ b/work/R23-T001/PROPERTIES.md @@ -0,0 +1,35 @@ +# Mathematical Properties and Verification for `is_int_palindrome` + +This document details the property-based testing and verification conducted on `maths/is_int_palindrome.py` for task `R23-T001`. + +## Tested Properties + +### 1. Non-Negative Integer Equivalence Property +- **Formal Statement**: $\forall n \in \mathbb{Z}_{\ge 0},\ \text{is\_int\_palindrome}(n) \iff (\text{str}(n) == \text{str}(n)[::-1])$ +- **Description**: For all non-negative integers, whether an integer is palindromic must match the canonical string representation reading identically forwards and backwards. +- **Verification**: Tested against $\ge 1,000$ random non-negative integer inputs generated by Hypothesis (`st.integers(min_value=0)`). + +### 2. Negative Integer Property +- **Formal Statement**: $\forall n \in \mathbb{Z}_{< 0},\ \text{is\_int\_palindrome}(n) = \text{False}$ +- **Description**: Negative numbers begin with a negative sign (`-`), e.g., `-121`. Because the reverse would have the minus sign at the end (`121-`), negative numbers cannot be palindromic integers. +- **Verification**: Tested against $\ge 1,000$ negative integer inputs generated by Hypothesis (`st.integers(max_value=-1)`). + +### 3. Constructed Palindrome Property (Symmetry Invariant) +- **Formal Statement**: $\forall \text{prefix} \in \mathbb{Z}_{> 0}, d \in \{0, \dots, 9\}$: + - $\text{is\_int\_palindrome}(\text{int}(\text{prefix} + \text{prefix}^{\mathcal{R}})) = \text{True}$ + - $\text{is\_int\_palindrome}(\text{int}(\text{prefix} + d + \text{prefix}^{\mathcal{R}})) = \text{True}$ +- **Description**: Any positive integer constructed by mirroring its digits (both even and odd lengths, including arbitrary-precision integers exceeding 19+ digits) must be recognized as a palindrome. +- **Verification**: Tested across $\ge 1,000$ generated palindromic structures. + +### 4. Type Safety and Input Validation +- **Formal Statement**: $\forall x \notin \mathbb{Z} \lor x \in \text{bool},\ \text{is\_int\_palindrome}(x) \implies \text{raises } \text{TypeError}$ +- **Description**: Since boolean values are subclasses of `int` in Python, they evaluate to 0 or 1 unless explicitly checked. To prevent unintended truthy/falsy behavior or invalid types (floats, strings, None, collections), a `TypeError` must be raised. +- **Verification**: Tested with $\ge 1,000$ varied non-integer inputs generated via `st.one_of`. + +## Boundary Cases Explored + +1. **Zero (`0`)**: Smallest non-negative integer, single digit, trivially palindromic (`True`). +2. **Single-digit numbers (`1` to `9`)**: Always read identically backwards and forwards (`True`). +3. **Trailing zeros (`10`, `120`, `1210`)**: Reversing leaves leading zeros which changes magnitude, making them non-palindromic (`False`). +4. **Large arbitrary-precision integers (19+ digits)**: Tested large numbers like `1234567890987654321` (19 digits) to ensure the numerical extraction loop handles arbitrary size without overflow or precision loss. +5. **Non-integer types**: Strings (e.g., `'121'`), floats (e.g., `12.21`), and booleans (e.g., `True`) raise `TypeError`. diff --git a/work/R23-T001/__init__.py b/work/R23-T001/__init__.py new file mode 100644 index 000000000000..564b59b8af51 --- /dev/null +++ b/work/R23-T001/__init__.py @@ -0,0 +1 @@ +# Package marker for R23-T001 diff --git a/work/R23-T001/test_is_int_palindrome_properties.py b/work/R23-T001/test_is_int_palindrome_properties.py new file mode 100644 index 000000000000..db6086bf2d1c --- /dev/null +++ b/work/R23-T001/test_is_int_palindrome_properties.py @@ -0,0 +1,63 @@ +import pytest +from hypothesis import given, settings +from hypothesis import strategies as st + +from maths.is_int_palindrome import is_int_palindrome + + +@settings(max_examples=1000) +@given(n=st.integers(min_value=0)) +def test_non_negative_integers(n: int) -> None: + """ + Property: For all n >= 0, is_int_palindrome(n) must equal + the string reversal equivalence: str(n) == str(n)[::-1]. + """ + expected = str(n) == str(n)[::-1] + assert is_int_palindrome(n) == expected + + +@settings(max_examples=1000) +@given(n=st.integers(max_value=-1)) +def test_negative_integers(n: int) -> None: + """ + Property: For all n < 0, is_int_palindrome(n) is False. + """ + assert is_int_palindrome(n) is False + + +@settings(max_examples=1000) +@given( + prefix=st.integers(min_value=1, max_value=10**18), + middle=st.integers(min_value=0, max_value=9), + is_odd_length=st.booleans(), +) +def test_constructed_palindromes(prefix: int, middle: int, is_odd_length: bool) -> None: + """ + Property: Any integer constructed by mirroring digits is palindromic. + Includes large ints (up to 37+ digits). + """ + prefix_str = str(prefix) + if is_odd_length: + pal_str = prefix_str + str(middle) + prefix_str[::-1] + else: + pal_str = prefix_str + prefix_str[::-1] + pal_int = int(pal_str) + assert is_int_palindrome(pal_int) is True + + +@settings(max_examples=1000) +@given( + bad_input=st.one_of( + st.floats(allow_nan=True, allow_infinity=True), + st.text(), + st.booleans(), + st.none(), + st.lists(st.integers()), + ) +) +def test_non_integer_inputs_raise_type_error(bad_input: object) -> None: + """ + Property: Calling is_int_palindrome with non-integer inputs raises TypeError. + """ + with pytest.raises(TypeError): + is_int_palindrome(bad_input) # type: ignore[arg-type] diff --git a/work/__init__.py b/work/__init__.py new file mode 100644 index 000000000000..ab207de02532 --- /dev/null +++ b/work/__init__.py @@ -0,0 +1 @@ +# Namespace package marker diff --git a/work/conftest.py b/work/conftest.py new file mode 100644 index 000000000000..cf30143a53c4 --- /dev/null +++ b/work/conftest.py @@ -0,0 +1,7 @@ +import sys +from pathlib import Path + +# Ensure repository root is on sys.path for work/ tests +repo_root = Path(__file__).resolve().parent.parent +if str(repo_root) not in sys.path: + sys.path.insert(0, str(repo_root))