Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
recursive 8 lines solution in Clear category for Chess Knight by CDG.Axel
def chess_knight(start, moves):
res = set()
# cycle through potential directions
for x, y in [(1, 2), (1, -2), (-1, 2), (-1, -2), (2, 1), (2, -1), (-2, 1), (-2, -1)]:
np = chr(ord(start[0]) + x) + chr(ord(start[1]) + y)
# if new point is on board and not already taken
if 'a' <= np[0] <= 'h' and '0' < np[1] < '9' and np not in res:
res.add(np)
if moves > 1:
res.update(chess_knight(np, moves - 1))
return sorted(res)
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!")
Sept. 15, 2021