{"record":{"id":"a35caf9c101e1ec3","repo":"TheAlgorithms/Python","slug":"the-elements-inside-the-sequence-must-contains-onl","errorCode":null,"errorMessage":"The elements inside the sequence must contains only {colors} values","messagePattern":"The elements inside the sequence must contains only (.+?) values","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"sorts/dutch_national_flag_sort.py","lineNumber":87,"sourceCode":"        return []\n    if len(sequence) == 1:\n        return list(sequence)\n    low = 0\n    high = len(sequence) - 1\n    mid = 0\n    while mid <= high:\n        if sequence[mid] == colors[0]:\n            sequence[low], sequence[mid] = sequence[mid], sequence[low]\n            low += 1\n            mid += 1\n        elif sequence[mid] == colors[1]:\n            mid += 1\n        elif sequence[mid] == colors[2]:\n            sequence[mid], sequence[high] = sequence[high], sequence[mid]\n            high -= 1\n        else:\n            msg = f\"The elements inside the sequence must contains only {colors} values\"\n            raise ValueError(msg)\n    return sequence\n\n\nif __name__ == \"__main__\":\n    import doctest\n\n    doctest.testmod()\n\n    user_input = input(\"Enter numbers separated by commas:\\n\").strip()\n    unsorted = [int(item.strip()) for item in user_input.split(\",\")]\n    print(f\"{dutch_national_flag_sort(unsorted)}\")\n","sourceCodeStart":69,"sourceCodeEnd":99,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/sorts/dutch_national_flag_sort.py#L69-L99","documentation":"Raised by dutch_national_flag_sort in sorts/dutch_national_flag_sort.py when the three-way partition loop encounters an element that is not one of the three sentinel values in colors = (0, 1, 2). DNF sort is a single-pass partition for exactly three distinct values (red=0, white=1, blue=2); any other value — 3, -1, 1.1, a character — falls into the else branch and raises ValueError. Empty and single-element sequences return early and never trigger it.","triggerScenarios":"dutch_national_flag_sort([3, 2, 3, 1, 3, 0, 3]); dutch_national_flag_sort([-1, 2, -1, 1]); dutch_national_flag_sort([1.1, 2, 1]); dutch_national_flag_sort('abacab') (string elements never equal 0/1/2).","commonSituations":"Using DNF sort as a general 3-value bucket sort on categorical data without mapping to 0/1/2; dirty datasets with out-of-range codes; the classic 'sort colors' (LeetCode 75) problem where input guarantees are violated.","solutions":["Map your three categories to exactly 0, 1, 2 before sorting, then map back after.","Sanitize input: if not set(sequence) <= {0, 1, 2}: raise/filter before calling.","If more than three distinct values exist, use counting sort or a general comparison sort instead."],"exampleFix":"# before\ndutch_national_flag_sort(['red', 'blue', 'white'])  # ValueError\n\n# after\nrank = {'red': 0, 'white': 1, 'blue': 2}\nrev = {v: k for k, v in rank.items()}\nsorted_keys = dutch_national_flag_sort([rank[c] for c in colors_list])\nsorted_colors = [rev[k] for k in sorted_keys]","handlingStrategy":"validation","validationCode":"ALLOWED = {0, 1, 2}\nif set(sequence) - ALLOWED:\n    raise ValueError(f'values outside {ALLOWED}: {set(sequence) - ALLOWED}')\ndutch_national_flag_sort(sequence)","typeGuard":"def is_dnf_sortable(seq) -> bool:\n    return all(x in (0, 1, 2) for x in seq)","tryCatchPattern":"try:\n    result = dutch_national_flag_sort(seq)\nexcept ValueError as e:\n    raise ValueError(f'not a 3-value sequence: {e}') from e","preventionTips":["Map categories to 0/1/2 explicitly before DNF sort; never pass raw strings or floats.","DNF sort is only optimal for exactly three distinct values — more values need another sort.","Set comprehension validation (set(seq) <= {0,1,2}) is cheap and precise."],"tags":["sorting","dutch-national-flag","partition","precondition","enum-values"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}