MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / find_secret_passcode

Function find_secret_passcode

project_euler/problem_079/sol1.py:21–53  ·  view source on GitHub ↗

Returns the shortest possible secret passcode of unknown length. >>> find_secret_passcode(["135", "259", "235", "189", "690", "168", "120", ... "136", "289", "589", "160", "165", "580", "369", "250", "280"]) 12365890 >>> find_secret_passcode(["426", "281", "061", "819" "26

(logins: list[str])

Source from the content-addressed store, hash-verified

19
20
21def find_secret_passcode(logins: list[str]) -> int:
22 """
23 Returns the shortest possible secret passcode of unknown length.
24
25 >>> find_secret_passcode(["135", "259", "235", "189", "690", "168", "120",
26 ... "136", "289", "589", "160", "165", "580", "369", "250", "280"])
27 12365890
28
29 >>> find_secret_passcode(["426", "281", "061", "819" "268", "406", "420",
30 ... "428", "209", "689", "019", "421", "469", "261", "681", "201"])
31 4206819
32 """
33
34 # Split each login by character e.g. '319' -> ('3', '1', '9')
35 split_logins = [tuple(login) for login in logins]
36
37 unique_chars = {char for login in split_logins for char in login}
38
39 for permutation in itertools.permutations(unique_chars):
40 satisfied = True
41 for login in logins:
42 if not (
43 permutation.index(login[0])
44 < permutation.index(login[1])
45 < permutation.index(login[2])
46 ):
47 satisfied = False
48 break
49
50 if satisfied:
51 return int("".join(permutation))
52
53 raise Exception("Unable to find the secret passcode")
54
55
56def solution(input_file: str = "keylog.txt") -> int:

Callers 1

solutionFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected