Created
May 4, 2016 02:33
-
-
Save bowbahdoe/c0ef30b6138e253db6e10f827be02f02 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def makeTree(files,parents): | |
| tree = [] | |
| for e,i in enumerate(files): | |
| index = e | |
| tree.append([i]) | |
| while True: | |
| if parents[index] == "-1": | |
| tree[e].append("-1") | |
| break | |
| else: | |
| tree[e].append(parents[index]) | |
| index = files.index(parents[index]) | |
| return tree | |
| def findClosest(branch_1,branch_2): | |
| distances = [] | |
| for f,i in enumerate(branch_1): | |
| for j, e in enumerate(branch_2): | |
| if e == i: | |
| distances.append([e, abs(j-f)]) | |
| lowest = 10 | |
| for i in distances: | |
| if i[1] < lowest: | |
| lowest = i[1] | |
| answer = i[0] | |
| return answer | |
| def closestCommonParent(files, parents, file1, file2): | |
| tree = makeTree(files, parents) | |
| for i in tree: | |
| if i[0] == file1: | |
| branch_1 = i | |
| if i[0] == file2: | |
| branch_2 = i | |
| return findClosest(branch_1, branch_2) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment