Skip to content

Commit e179844

Browse files
author
Joseph Luce
authored
Create Longest_movie_title.md
1 parent d352f8e commit e179844

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# SOLUTION
2+
```
3+
import copy
4+
5+
def concat_longest_title(titles):
6+
adj_list = create_adj_list(titles)
7+
global_visit = set()
8+
global_title = list()
9+
for title in adj_list.keys():
10+
if title not in global_visit:
11+
local_visit = set()
12+
local_title = list()
13+
visit_title(title,
14+
local_title,
15+
global_title,
16+
adj_list,
17+
local_visit,
18+
global_visit)
19+
return ' '.join(global_title)
20+
21+
def create_adj_list(titles):
22+
firstWord_to_titles_hash = dict()
23+
result = dict()
24+
for title in titles:
25+
words = title.split()
26+
if len(words) != 0:
27+
first_word = words[0]
28+
if first_word not in firstWord_to_titles_hash:
29+
firstWord_to_titles_hash[first_word] = list()
30+
firstWord_to_titles_hash[first_word].append(title)
31+
for title in titles:
32+
words = title.split()
33+
if len(words) != 0:
34+
last_word = words[-1]
35+
if last_word in firstWord_to_titles_hash:
36+
titles = firstWord_to_titles_hash[last_word]
37+
result[title] = titles
38+
else:
39+
result[title] = list()
40+
return result
41+
42+
def visit_title(title, stack, longest_title, adj_list, local_visit, global_visit):
43+
print longest_title
44+
result = list()
45+
if title not in local_visit:
46+
local_visit.add(title)
47+
stack.append(title)
48+
if len(stack) > len(longest_title):
49+
longest_title = copy.deepcopy(stack)
50+
neighbors = adj_list[title]
51+
for next_title in neighbors:
52+
visit_title(next_title,
53+
stack,
54+
longest_title,
55+
adj_list,
56+
local_visit,
57+
global_visit)
58+
stack.pop()
59+
60+
Input = ['OF MICE AND MEN', 'BLACK MASS', 'MEN IN BLACK']
61+
62+
print concat_longest_title(Input)
63+
```

0 commit comments

Comments
 (0)