{"record":{"id":"60ee6c8cacacda28","repo":"TheAlgorithms/Python","slug":"unable-to-find-the-secret-passcode","errorCode":null,"errorMessage":"Unable to find the secret passcode","messagePattern":"Unable to find the secret passcode","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"project_euler/problem_079/sol1.py","lineNumber":53,"sourceCode":"    split_logins = [tuple(login) for login in logins]\n\n    unique_chars = {char for login in split_logins for char in login}\n\n    for permutation in itertools.permutations(unique_chars):\n        satisfied = True\n        for login in logins:\n            if not (\n                permutation.index(login[0])\n                < permutation.index(login[1])\n                < permutation.index(login[2])\n            ):\n                satisfied = False\n                break\n\n        if satisfied:\n            return int(\"\".join(permutation))\n\n    raise Exception(\"Unable to find the secret passcode\")\n\n\ndef solution(input_file: str = \"keylog.txt\") -> int:\n    \"\"\"\n    Returns the shortest possible secret passcode of unknown length\n    for successful login attempts given by `input_file` text file.\n\n    >>> solution(\"keylog_test.txt\")\n    6312980\n    \"\"\"\n    logins = Path(__file__).parent.joinpath(input_file).read_text().splitlines()\n\n    return find_secret_passcode(logins)\n\n\nif __name__ == \"__main__\":\n    print(f\"{solution() = }\")\n","sourceCodeStart":35,"sourceCodeEnd":71,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/project_euler/problem_079/sol1.py#L35-L71","documentation":"Raised by the internal passcode search in project_euler/problem_079/sol1.py when no permutation of the digits satisfies every login attempt's ordering constraints. It is a bare Exception (not ValueError), signaling the exhausted search space of itertools.permutations. In practice it is nearly unreachable: the supplied keylog data has a valid answer (73162890 ordering), so hitting it usually means the input file was modified or the login-parsing pipeline changed.","triggerScenarios":"Calling the search helper (e.g. find_passcode) with a fabricated login list whose constraints are contradictory, such as ['12', '21'] style cycles in 3-char form like ['123', '321']. Modifying keylog.txt to include cyclic constraints. Feeding a truncated/garbled file so constraints become inconsistent.","commonSituations":"Custom keylog experiments with hand-written attempts; unit tests probing unsatisfiable inputs; edits to the constraint check or permutation space that drop the real solution.","solutions":["Restore the original keylog file (git checkout -- project_euler/problem_079/keylog.txt) and retest.","If testing custom inputs, first verify the constraints are acyclic (topological sort of the digit graph must succeed).","Catch Exception narrowly around this call if you accept untrusted keylogs, since it is a bare Exception.","Re-run the doctest: python -m doctest project_euler/problem_079/sol1.py -v."],"exampleFix":"# before\npasscode = find_passcode(contradictory_logins)  # may raise bare Exception\n\n# after\ntry:\n    passcode = find_passcode(logins)\nexcept Exception as exc:\n    raise ValueError(\"keylog constraints are unsatisfiable\") from exc","handlingStrategy":"try-catch","validationCode":"from collections import defaultdict\n\ndef constraints_are_acyclic(logins: list[str]) -> bool:\n    graph = defaultdict(set)\n    digits = set()\n    for login in logins:\n        digits.update(login)\n        for a, b in zip(login, login[1:]):\n            graph[a].add(b)\n    # Kahn's algorithm\n    indeg = {d: 0 for d in digits}\n    for outs in graph.values():\n        for b in outs:\n            indeg[b] += 1\n    queue = [d for d, k in indeg.items() if k == 0]\n    seen = 0\n    while queue:\n        d = queue.pop()\n        seen += 1\n        for b in graph[d]:\n            indeg[b] -= 1\n            if indeg[b] == 0:\n                queue.append(b)\n    return seen == len(digits)","typeGuard":null,"tryCatchPattern":"try:\n    passcode = find_passcode(logins)\nexcept Exception as exc:  # bare Exception in the library\n    raise ValueError(\"keylog constraints are unsatisfiable\") from exc","preventionTips":["Validate the keylog graph is acyclic (topological sort succeeds) before searching.","Keep the original keylog.txt intact; restore from git if edited.","Catch Exception (not ValueError) around this call since the library raises bare Exception."],"tags":["project-euler","search-exhaustion","exception","data-integrity"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}