From f400640b058b2edfd9ae1433b45f1c5f1b999cef Mon Sep 17 00:00:00 2001 From: beam Date: Tue, 27 Oct 2026 13:29:28 +0000 Subject: [PATCH] beam: verify Floyd duplicate candidate occurs more than once --- wizard-work/beam/find_duplicate.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/wizard-work/beam/find_duplicate.py b/wizard-work/beam/find_duplicate.py index a39bee0..ceefac4 100644 --- a/wizard-work/beam/find_duplicate.py +++ b/wizard-work/beam/find_duplicate.py @@ -33,7 +33,12 @@ def find_duplicate(nums: List[int]) -> int: while slow2 != slow: slow2 = nums[slow2] slow = nums[slow] - return slow + candidate = slow + # Verify the candidate actually occurs more than once; a permutation + # (no duplicate) also has cycles, so Floyd alone is not sufficient. + if nums.count(candidate) < 2: + raise ValueError("no duplicate present in nums") + return candidate def _self_test() -> None: