Skip to content

Instantly share code, notes, and snippets.

@syntacticsugar
Created March 18, 2019 21:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save syntacticsugar/f9a01b34f0a6a3ef645457b0196531f3 to your computer and use it in GitHub Desktop.
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
# 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