Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
3(4) lines solution in Creative category for Chess Knight by CDG.Axel
def chess_knight(start, moves):
res, dirs = set(), [(1, 2), (1, -2), (-1, 2), (-1, -2), (2, 1), (2, -1), (-2, 1), (-2, -1)]
[res.update([nx+ny] + (chess_knight(nx+ny, moves - 1) if moves > 1 else [])) for x, y in dirs
if 'a' <= (nx := chr(ord(start[0]) + x)) <= 'h' and '0' < (ny := chr(ord(start[1]) + y)) < '9']
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!")
Oct. 8, 2021