Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2020
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. import time
  2.  
  3. def fillActors(actor):
  4. movies={}
  5. f1=open("C:/Users/Ante/Desktop/movies.txt","r");
  6. line=f1.readline()
  7. while(line!=""):
  8. getMovie(movies,line)
  9. line = f1.readline()
  10.  
  11. bfs(movies,actor)
  12.  
  13.  
  14. def getMovie(movies,line):
  15. isMovie= True
  16. actors=[]
  17. end = len(line)
  18.  
  19. while True:
  20. start = line.find(";")
  21.  
  22. if(start!= -1):
  23. if(isMovie):
  24. movieName=line[0:start]
  25. line=line[start+1:end]
  26. isMovie=False
  27. else:
  28. glumac=line[0:start]
  29. actors.append(glumac)
  30. line=line[start+1:end]
  31. else:
  32. actors.append(line.strip("\n"))
  33.  
  34. break;
  35.  
  36. movies[movieName]=actors
  37.  
  38. def findActors(actor,movies):
  39. connected=[]
  40. for key,value in movies:
  41. if(actor in value):
  42. for i in value:
  43. if(i == actor):
  44. continue
  45. else:
  46. connected.append(i)
  47. else:
  48. continue
  49.  
  50. def bfs(movies,actor):
  51. visited = []
  52. S = str(actor)
  53. T=str("Kevin Bacon")
  54. q = [[actor]]
  55. if S == T:
  56. return visited
  57. while q:
  58. path = q.pop(0)
  59. top = path[-1]
  60. if top not in visited:
  61. connected_actors = findActors(top,movies)
  62. for find in connected_actors:
  63. otherpath = list(path)
  64. otherpath.append(find)
  65. q.append(otherpath)
  66. if find == T:
  67. return otherpath
  68.  
  69. visited.append(top)
  70. return -1
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78. fillActors("Steve Martin")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement