I have converted the following block of codes from a java program. How can I write the name of the countries in Map using their names, instead of their IDs?
from collections import defaultdict
colors = ['Red', 'Yellow', 'Green', 'Blue']
mapColors = defaultdict(str)
def okToColor(Map ,country, color):
for c in Map[country]:
if mapColors[c] == color: return False
return True
def explore(Map, country, color):
if country >= len(Map): return True
if okToColor(Map, country, color):
mapColors[country] = color
for color in colors:
if explore(Map, country + 1, color): return True
return False
def printMap():
for c in mapColors:
print c, mapColors[c]
Map = [[1, 4, 2, 5], [0, 4, 6, 5], [0, 4, 3, 6, 5], [2, 4, 6],
[0, 1, 6, 3, 2], [2, 6, 1, 0], [2, 3, 4, 1, 5]]
result = explore(Map, 0, 'Red')
print result
printMap()
I want’t the Map to be a graph like this:
Map = { 'A':['B', 'C'], 'B':['A','D'], ...}
where A, B, C, D are the name of the countries.
The main idea is to define a mapping between
countriesand numerical indices:Then, with only little changes, you can use your original code. Where before
countrywas a numerical index, now you putcindex[country]when you need the numerical index.And when you need to reverse the mapping,
countries[index]gives you the string name of the country.yields