###PYTHON 3 QUESTIONS## This is an interview challenge questions that I failed.
ID: 3911481 • Letter: #
Question
###PYTHON 3 QUESTIONS##
This is an interview challenge questions that I failed. Can anyone tell me how to solve this in PYTHON 3. Thank you
Yelp can recommend businesses based on a distance you're willing to travel Given a distance input and a connected acyclic graph of businesses with edges as distances, return the list of names of businesses within the distance input Args: starting_business: a Business object to start from distance: int Output: list of str: A list of Business names that are within the given distance of the starting Business Distance is inclusive, meaning if a business is 5 away, then a distance input of 5 means that business IS reachable. The return value should NOT have the name of the starting business. Therefore, if no businesses are within the given distance, return an empty list. The return value is NOT required to be sortedExplanation / Answer
Following steps needed to solve given problem:
1)First,Run FloydWarshall algoritm for the given graph (Adjacency matrix).
2)it will find out minimum path from each node to every other node;suppose this output is stored in a dist[][]matrix.
3)where each (i,j)pair denote a path from vertex i to vertex j.
4)make an empty reachable bussiness array reachable[]={};
5)now, in find_reachable_bussiness function,for given starting bussiness as a row input for dist[][]matrix compare each j as if
(dist[starting bussiness][j]<=distance) ,add j to rechable bussiness array.
6)Finally,print reachable array.
//Function to Run FloydWarshall Algorithm
def Warshall(graph):
dist = map(lambda i : map(lambda j : j , i) , graph)
for k in range(V):
for i in range(V):
for j in range(V):
dist[i][j] = min(dist[i][j] ,
dist[i][k]+ dist[k][j])
# A utility function to find the solution
def find_reachable_bussiness(starting_bussiness,distance):
reachable_bussiness=[];
for i in range(V):
if(dist[starting_bussiness][j] <= distance):
reachable_bussiness.append(i)
print_solution(reachable_bussiness)
def print_solution(reachable):
print "reachable bussiness are:"
for item in reachable:
print (item)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.