Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Color Map by jcg
#color-map
def color_map(regions):
neighbours = getNeighbours(regions) # all the regions with their neighbours
if neighbours == {} :
return [1]
region = 0 # first region
colors = [0] * len(neighbours) # table of colors
while region >= 0: # if region
colors[region] += 1 # we try next color
if colors[region] > 4: # if over
colors[region] = 0 # reste color for that region
region -= 1 # backwards to previous region continue
# if compatible
elif all(colors[other]!=colors[region] for other in neighbours[region]):
# if last region we found a solution
if region+1 == len(colors):
return colors
else: # next region
region += 1
return "If you see this text, you are looking the source"
def getNeighbours(theMap) :
""" get the neignbours for each region
>>> voisins((
(1, 1, 1, 2, 1, 1),
(1, 1, 1, 1, 1, 1),
(1, 1, 0, 1, 1, 1),
(1, 0, 0, 0, 1, 1),
(1, 1, 0, 4, 3, 1),
(1, 1, 1, 3, 3, 3),
(1, 1, 1, 1, 3, 5),
))
{0: {1, 4}, 1: {0, 2, 3}, 2: {1}, 3: {1, 4, 5}, 4: {0, 3}, 5: {3}}
"""
theNeighbours = {}
for l in range(len(theMap)) :
for c in range(len(theMap[l])):
regionA = theMap[l][c]
for d in ((0,1),(1,0)): #symetric, no need of other directions
dx, dy = d
nl, nc = l + dx, c + dy
if nl < len(theMap) and nc < len(theMap[0]):
regionB = theMap[nl][nc]
if regionA == regionB: # same region
continue
else:
theNeighbours[regionA] = theNeighbours.get(regionA, set())|{regionB}
theNeighbours[regionB] = theNeighbours.get(regionB, set())|{regionA}
return theNeighbours
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
NEIGHS = ((-1, 0), (1, 0), (0, 1), (0, -1))
COLORS = (1, 2, 3, 4)
ERROR_NOT_FOUND = "Didn't find a color for the country {}"
ERROR_WRONG_COLOR = "I don't know about the color {}"
def checker(func, region):
user_result = func(region)
if not isinstance(user_result, (tuple, list)):
print("The result must be a tuple or a list")
return False
country_set = set()
for i, row in enumerate(region):
for j, cell in enumerate(row):
country_set.add(cell)
neighbours = []
if j < len(row) - 1:
neighbours.append(region[i][j + 1])
if i < len(region) - 1:
neighbours.append(region[i + 1][j])
try:
cell_color = user_result[cell]
except IndexError:
print(ERROR_NOT_FOUND.format(cell))
return False
if cell_color not in COLORS:
print(ERROR_WRONG_COLOR.format(cell_color))
return False
for n in neighbours:
try:
n_color = user_result[n]
except IndexError:
print(ERROR_NOT_FOUND.format(n))
return False
if cell != n and cell_color == n_color:
print("Same color neighbours.")
return False
if len(country_set) != len(user_result):
print("Excess colors in the result")
return False
return True
assert checker(color_map, (
(0, 0, 0),
(0, 1, 1),
(0, 0, 2),
)), "Small"
assert checker(color_map, (
(0, 0, 2, 3),
(0, 1, 2, 3),
(0, 1, 1, 3),
(0, 0, 0, 0),
)), "4X4"
assert checker(color_map, (
(1, 1, 1, 2, 1, 1),
(1, 1, 1, 1, 1, 1),
(1, 1, 0, 1, 1, 1),
(1, 0, 0, 0, 1, 1),
(1, 1, 0, 4, 3, 1),
(1, 1, 1, 3, 3, 3),
(1, 1, 1, 1, 3, 5),
)), "6 pack"
Oct. 16, 2014