Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Convert to coords and back solution in Clear category for Chess Knight by kudinov.feodor
from itertools import product, chain
LETTERS = "abcdefgh"
def possible_moves(x, y):
deltas = chain(product([1, -1], [2, -2]), product([2, -2], [1, -1]))
return set((x + _x, y + _y) for (_x, _y) in deltas if 0 <= x + _x <= 7 and 0 <= y + _y <= 7)
def chess_knight(start, moves):
start = LETTERS.index(start[0]), int(start[1]) - 1 # convert to coords
all_moves = possible_moves(*start) # first move
for i in range(moves - 1):
all_moves |= set(chain.from_iterable(possible_moves(*move) for move in all_moves))
return sorted(LETTERS[x] + str(y + 1) for x, y in all_moves) # convert back to chess format
if __name__ == '__main__':
print("Example:")
print(chess_knight('a1', 1))
#These "asserts" using only for self-checking and not necessary for auto-testing
assert chess_knight('a1', 1) == ['b3', 'c2']
assert chess_knight('h8', 2) == ['d6', 'd8', 'e5', 'e7', 'f4', 'f7', 'f8', 'g5', 'g6', 'h4', 'h6', 'h8']
print("Coding complete? Click 'Check' to earn cool rewards!")
June 22, 2020
Comments: