Created
March 18, 2019 21:29
-
-
Save syntacticsugar/f9a01b34f0a6a3ef645457b0196531f3 to your computer and use it in GitHub Desktop.
Locations. Traveling Salesman problem ? Create a function that returns an array of arrays where the inner arrays are the groups
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# sometimes, we get data that is not fully populated | |
# eg Boston <> New York appears, and so does New York <> Boston, | |
# but Berlin <> Munich appears, though not Munich <> Berlin | |
locations = { | |
'new york' => [ 'boston', 'philly' ], | |
'boston' => [ 'new york' ], | |
'philly' => [ 'new york' ], | |
'paris' => [ 'amsterdam', 'berlin' ], | |
'berlin' => [ 'paris', 'munich' ], | |
'amsterdam' => [], | |
'munich' => [], | |
'osaka' => [] | |
} | |
=begin | |
Create a function that returns an array of arrays where the inner arrays are the groups, eg=> | |
[ [ 'new york', 'boston', 'philly' ] | |
, [ 'paris', 'amsterdam', 'berlin', 'munich' ] | |
, [ 'osaka' ] | |
] | |
The ordering does not matter. | |
=end | |
common = [] | |
# key = "new york" | |
hasConnection = ->(key1,key2) { | |
locations[key1].any? { |loc| loc.eql? key2 } or | |
locations[key2].any? { |loc| loc.eql? key1 } | |
} | |
locations.keys.each_with_index do |key, index| | |
puts "#{key} : #{index} " | |
if hasConnection(key) | |
end | |
for i in locations.keys | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment