Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Colder-Warmer by blabaster
from math import hypot
def checkio(steps):
visited = set((y, x) for y, x, _ in steps)
field = [(y, x) for x in range(10) for y in range(10) if (y, x) not in visited]
for (y0, x0, _), (y1, x1, dif) in zip(steps, steps[1:]):
if dif < 0:
y0, x0, y1, x1 = y1, x1, y0, x0
field = [(y, x) for y, x in field if (hypot(x - x0, y - y0) > hypot(x - x1, y - y1)) == bool(dif)]
midy, midx = (sum(yx[_] for yx in field) / len(field) for _ in range(2))
return min(field, key=lambda yx: hypot(yx[0] - midy, yx[1] - midx))
def check_solution(func, goal, start):
prev_steps = [start]
for step in range(MAX_STEP):
row, col = func([s[:] for s in prev_steps])
if [row, col] == goal:
return True
if 10 <= row or 0 > row or 10 <= col or 0 > col:
print("You gave wrong coordinates.")
return False
prev_distance = hypot(prev_steps[-1][0] - goal[0], prev_steps[-1][1] - goal[1])
distance = hypot(row - goal[0], col - goal[1])
alteration = 0 if prev_distance == distance else (1 if prev_distance > distance else -1)
prev_steps.append([row, col, alteration])
print("Too many steps")
return False
assert check_solution(checkio, [7, 7], [5, 5, 0]), "1st example"
assert check_solution(checkio, [5, 6], [0, 0, 0]), "2nd example"
May 12, 2014