Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Locations. Traveling Salesman problem ? Create a function that returns an array of arrays where the inner arrays are the groups
# 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