Created
October 24, 2017 15:40
-
-
Save tlkahn/71e9dcf1e80d4dace8efb26df41b2088 to your computer and use it in GitHub Desktop.
monkey_pass_puzzle.py
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
# you can write to stdout for debugging purposes, e.g. | |
# print("this is a debug message") | |
def checkPass(lst, length, d): | |
maxD = 0 | |
lst = sorted(lst, key=lambda x: x[0]) | |
print(lst) | |
for i in xrange(1, len(lst)): | |
if abs(lst[i][0] - lst[i-1][0]) > maxD: | |
maxD = abs(lst[i][0] - lst[i-1][0]) | |
print("maxD", maxD) | |
maxD = max(maxD, abs(length - lst[-1][0]), (abs(lst[0][0]) + 1)) | |
print("maxD", maxD) | |
return maxD <= d | |
def solution(A, D): | |
# write your code in Python 2.7 | |
aList = [] | |
for i in xrange(len(A)): | |
aList.append((i, A[i])) | |
aList = sorted(aList, key = lambda x: x[1]) | |
aList = [x for x in aList if x[1]>-1] | |
if D > len(aList): | |
return 1 | |
for i in xrange(1, len(aList)+1): | |
if checkPass(aList[:i], len(A), D): | |
return aList[i-1][1] | |
return -1 | |
print solution([3, 2, 1], 1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment