Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Colder-Warmer by dig
from math import dist
def checkio(steps):
point_set = {(i % 10, i // 10) for i in range(100)}
for i, step in enumerate(steps[1:], start=1):
for point in point_set.copy():
distance = dist(point, (step[0], step[1]))
prev_distance = dist(point, (steps[i-1][0], steps[i-1][1]))
if step[-1] == 1 and distance > prev_distance:
point_set.remove(point)
elif step[-1] == -1 and distance < prev_distance:
point_set.remove(point)
elif step[-1] == 0 and distance != prev_distance:
point_set.remove(point)
point_set.discard((step[0], step[1]))
point_set.discard((steps[i-1][0], steps[i-1][1]))
return list(point_set.pop())
if __name__ == "__main__":
# This part is using only for self-checking and not necessary for auto-testing
from math import hypot
MAX_STEP = 12
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 4, 2023