7 Rollback to Revision 5 edited Nov 13 '16 at 4:43 200_success 136k2121 gold badges175175 silver badges446446 bronze badges EDITED: def answer(times,time_limit): n=len(times) mindic=min(min(times)) dist = mapn=len(lambdatimes) i : map(lambda j : jdef find_path(times, i)start, end,time, timespath=[]): for k in range(n): if start != 0 and forstart i!= inend range(n): forif jstart-1 in range(n)path: dist[i][j]pass = min(dist[i][j] ,dist[i][k]+ dist[k][j]) def find_path(times, start, end,time, path=[])else: if start != 0 and start != end : path = path + [start-1] if start == end and len(path)== n-2 : return path if start == end and time-min(times[start][0:len(times[start])-1]) < mindic: return path best=None for i in range(n): if time-times[start][i] >= mindic and start != i and itimes[start][i] != 0 and i-1 not in path: newpath = find_path(times, i, end,time-times[start][i], path) if newpath != None : if not best or len(newpath) > len(best): best=newpath return best return find_pathres=find_path(disttimes, 0, n-1,time_limit) return res  EDITED: def answer(times,time_limit): n=len(times) mindic=min(min(times)) dist = map(lambda i : map(lambda j : j , i) , times) for k in range(n): for i in range(n): for j in range(n): dist[i][j] = min(dist[i][j] ,dist[i][k]+ dist[k][j]) def find_path(times, start, end,time, path=[]): if start != 0 and start != end : path = path + [start-1] if start == end and len(path)== n-2 : return path if start == end and time-min(times[start][0:-1]) < mindic: return path best=None for i in range(n): if time-times[start][i] >= mindic and start != i and i != 0 and i-1 not in path: newpath = find_path(times, i, end,time-times[start][i], path) if newpath != None : if not best or len(newpath) > len(best): best=newpath return best return find_path(dist, 0, n-1,time_limit)  def answer(times,time_limit): mindic=min(min(times)) n=len(times) def find_path(times, start, end,time, path=[]): if start != 0 and start != end : if start-1 in path: pass else: path = path + [start-1] if start == end and time-min(times[start][0:len(times[start])-1]) < mindic: return path best=None for i in range(n): if time-times[start][i] >= mindic and times[start][i] != 0 : newpath = find_path(times, i, end,time-times[start][i], path) if newpath != None : if not best or len(newpath) > len(best): best=newpath return best res=find_path(times,0,n-1,time_limit) return res  6 added 224 characters in body edited Nov 13 '16 at 1:31 Ouail Bendidi 3866 bronze badges EDITED: def answer(times,time_limit): n=len(times) mindic=min(min(times)) n=len(times) dist = map(lambda i def: find_pathmap(times,lambda start,j end: j ,time i) , path=[]times): for k in range(n): if start != 0 and startfor !=i endin range(n): iffor start-1j in pathrange(n): pass dist[i][j] = min(dist[i][j] ,dist[i][k]+ dist[k][j]) def find_path(times, start, end,time, elsepath=[]): if start != 0 and start != end : path = path + [start-1] if start == end and len(path)== n-2 : return path if start == end and time-min(times[start][0:len(times[start])-1]) < mindic: return path best=None for i in range(n): if time-times[start][i] >= mindic and times[start][i]start != i and i != 0 and i-1 not in path: newpath = find_path(times, i, end,time-times[start][i], path) if newpath != None : if not best or len(newpath) > len(best): best=newpath return best res=find_pathreturn find_path(timesdist, 0, n-1,time_limit) return res  def answer(times,time_limit): mindic=min(min(times)) n=len(times) def find_path(times, start, end,time, path=[]): if start != 0 and start != end : if start-1 in path: pass else: path = path + [start-1] if start == end and time-min(times[start][0:len(times[start])-1]) < mindic: return path best=None for i in range(n): if time-times[start][i] >= mindic and times[start][i] != 0 : newpath = find_path(times, i, end,time-times[start][i], path) if newpath != None : if not best or len(newpath) > len(best): best=newpath return best res=find_path(times,0,n-1,time_limit) return res  EDITED: def answer(times,time_limit): n=len(times) mindic=min(min(times)) dist = map(lambda i : map(lambda j : j , i) , times) for k in range(n): for i in range(n): for j in range(n): dist[i][j] = min(dist[i][j] ,dist[i][k]+ dist[k][j]) def find_path(times, start, end,time, path=[]): if start != 0 and start != end : path = path + [start-1] if start == end and len(path)== n-2 : return path if start == end and time-min(times[start][0:-1]) < mindic: return path best=None for i in range(n): if time-times[start][i] >= mindic and start != i and i != 0 and i-1 not in path: newpath = find_path(times, i, end,time-times[start][i], path) if newpath != None : if not best or len(newpath) > len(best): best=newpath return best return find_path(dist, 0, n-1,time_limit)  5 added 1366 characters in body edited Nov 11 '16 at 12:32 Ouail Bendidi 3866 bronze badges GivenYou and your rescued prisoners need to get out of this collapsing death trap - and fast! Unfortunately, some of the prisonners have been weakened by their long imprisonment and can't run very fast. Their friends are trying to help them, but this escape would go a lot faster if you also pitched in. The exit doors have begun to close, and if you don't make it through in time, you'll be trapped! You need to grab as many prisoners as you can and get through the exit doors before they close. The time it takes to move from your starting point to all of the prisoners and to the exit door will be given to you in a square matrix of integers with each. Each row containingwill tell you the time it takes to get to the start, first personprisoner, second personprisoner, ..., last personprisoner, and the exit door in that order. The order of the rows follows the same pattern (start, each personprisoner, exit door).Picking the persons The prisoners can jump into your arms, so picking them up is instantaneous,it's possible and arriving at the exit door at the same time as it seals still allows for a successful, if dramatic, escape. (Don't worry, any prisoner you don't pick up will be able to escape with you since they no longer have to carry the ones you did pick up.) You can revisit different spots if you wish, and moving to the exit door doesn't mean you have to immediately leave - it's possible toyou can move to and from the exit door to pick up additional personsprisoners if time permits.Some In addition to spending time traveling between prisoners, some paths interact with security checkpoints and add time back to the clock. Adding time to the clock will delay the closing of the exit doors, and if the time goes back up to 0 or a positive number after the doors have already closed, it triggers the exitdoors to reopen. Therefore, it might be possible to walk in a circle and keep gaining time: that is, each time a path is traversed, the same amount of time is used or added. The goal is to writeWrite a function that calculatesof the form answer(times, time_limit) to calculate the most persons oneprisoners you can pick up and which personsprisoners they are, while still escaping through the exit doors before the door closesthey close for good.The function should return If there are multiple sets of prisoners of the same number, return the indexesset of prisoners with the saved personslowest prisoner IDs (as indexes) in sorted order. The prisoners are represented as a sorted list by prisoner ID, with the first prisoner being 0. There are at most 5 prisoners, and time_limit is a non-negative integer that is at most 999. It will only be able to save the personsprisoners with index 0 and index 1.[ [0, 2, 2, 2, -1], # 0 = Start [9, 0, 2, 2, -1], # 1 = personprisoner 0 [9, 3, 0, 2, -1], # 2 = personprisoner 1 [9, 3, 2, 0, -1], # 3 = personprisoner 2 [9, 3, 2, 2, 0], # 4 = exit door ]  and a time limit of 1, the five inner array rows designate the starting point, personprisoner 0, personprisoner 1, personprisoner 2, and the exit door respectively. You could take the path: With this solution, you would pick up personprisoner 1 and 2. This is the best combination for this case, so the answer is [1, 2]. Given a square matrix of integers with each row containing the time it takes to get to the start, first person, second person, ..., last person, and the exit in that order. The order of the rows follows the same pattern (start, each person, exit).Picking the persons up is instantaneous,it's possible to revisit different spots , and moving to the exit doesn't mean you have to immediately leave - it's possible to move to and from the exit to pick up additional persons if time permits.Some paths add time back to the clock. Adding time to the clock will delay the closing of the exit doors, and if the time goes back up to 0 or a positive number after the doors have already closed, it triggers the exit to reopen. Therefore, it might be possible to walk in a circle and keep gaining time: that is, each time a path is traversed, the same amount of time is used or added. The goal is to write a function that calculates the most persons one can pick up and which persons they are, while still escaping through the exit before the door closes for good.The function should return return the indexes of the saved persons in sorted order. It will only be able to save the persons with index 0 and index 1.[ [0, 2, 2, 2, -1], # 0 = Start [9, 0, 2, 2, -1], # 1 = person 0 [9, 3, 0, 2, -1], # 2 = person 1 [9, 3, 2, 0, -1], # 3 = person 2 [9, 3, 2, 2, 0], # 4 = exit door ]  and a time limit of 1, the five inner array rows designate the starting point, person 0, person 1, person 2, and the exit door respectively. You could take the path: With this solution, you would pick up person 1 and 2. This is the best combination for this case, so the answer is [1, 2]. You and your rescued prisoners need to get out of this collapsing death trap - and fast! Unfortunately, some of the prisonners have been weakened by their long imprisonment and can't run very fast. Their friends are trying to help them, but this escape would go a lot faster if you also pitched in. The exit doors have begun to close, and if you don't make it through in time, you'll be trapped! You need to grab as many prisoners as you can and get through the exit doors before they close. The time it takes to move from your starting point to all of the prisoners and to the exit door will be given to you in a square matrix of integers. Each row will tell you the time it takes to get to the start, first prisoner, second prisoner, ..., last prisoner, and the exit door in that order. The order of the rows follows the same pattern (start, each prisoner, exit door). The prisoners can jump into your arms, so picking them up is instantaneous, and arriving at the exit door at the same time as it seals still allows for a successful, if dramatic, escape. (Don't worry, any prisoner you don't pick up will be able to escape with you since they no longer have to carry the ones you did pick up.) You can revisit different spots if you wish, and moving to the exit door doesn't mean you have to immediately leave - you can move to and from the exit door to pick up additional prisoners if time permits. In addition to spending time traveling between prisoners, some paths interact with security checkpoints and add time back to the clock. Adding time to the clock will delay the closing of the exit doors, and if the time goes back up to 0 or a positive number after the doors have already closed, it triggers the doors to reopen. Therefore, it might be possible to walk in a circle and keep gaining time: that is, each time a path is traversed, the same amount of time is used or added. Write a function of the form answer(times, time_limit) to calculate the most prisoners you can pick up and which prisoners they are, while still escaping through the exit doors before they close for good. If there are multiple sets of prisoners of the same number, return the set of prisoners with the lowest prisoner IDs (as indexes) in sorted order. The prisoners are represented as a sorted list by prisoner ID, with the first prisoner being 0. There are at most 5 prisoners, and time_limit is a non-negative integer that is at most 999. It will only be able to save the prisoners with index 0 and index 1.[ [0, 2, 2, 2, -1], # 0 = Start [9, 0, 2, 2, -1], # 1 = prisoner 0 [9, 3, 0, 2, -1], # 2 = prisoner 1 [9, 3, 2, 0, -1], # 3 = prisoner 2 [9, 3, 2, 2, 0], # 4 = exit door ]  and a time limit of 1, the five inner array rows designate the starting point, prisoner 0, prisoner 1, prisoner 2, and the exit door respectively. You could take the path: With this solution, you would pick up prisoner 1 and 2. This is the best combination for this case, so the answer is [1, 2]. 4 added 1366 characters in body edited Nov 11 '16 at 12:19 Ouail Bendidi 3866 bronze badges 3 added 25 characters in body edited Nov 7 '16 at 21:13 Ouail Bendidi 3866 bronze badges 2 deleted 7 characters in body; edited title edited Nov 7 '16 at 21:06 Jamal♦ 31.9k1212 gold badges123123 silver badges230230 bronze badges 1 asked Nov 7 '16 at 21:00 Ouail Bendidi 3866 bronze badges