-
Notifications
You must be signed in to change notification settings - Fork 0
/
find_routes.rb
38 lines (31 loc) · 963 Bytes
/
find_routes.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
def find_routes(routes)
actual_routes = []
# actual_routes << routes[0][0]
# actual_routes << routes[0][1]
# current_route = routes[0][1]
len = routes.length - 1
freq = routes.flatten.tally
smallest_freq = freq.select { |k, v| k if v == 1 }.keys
routes.each do |r|
if smallest_freq.include? r[0]
@initial_route = r[0]
@second_route = r[1]
end
end
actual_routes << @initial_route
actual_routes << @second_route
current_route = @second_route
len.times do
routes.each do |r|
if r[0] == current_route
actual_routes << r[1]
current_route = r[1]
end
end
end
actual_routes.join(', ')
end
routes = [['USA', 'BRA'], ['JPN', 'PHL'], ['BRA', 'UAE'], ['UAE', 'JPN']]
# r = [["MNL", "TAG"], ["CEB", "TAC"], ["TAG", "CEB"], ["TAC", "BOR"]]
r = [["Chicago", "Winnipeg"], ["Halifax", "Montreal"], ["Montreal", "Toronto"], ["Toronto", "Chicago"], ["Winnipeg", "Seattle"]]
find_routes(r)