Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Chess Knight by mortonfox
DIRS = [(-1, -2), (-2, -1), (1, -2), (2, -1), (-1, 2), (-2, 1), (1, 2), (2, 1)]
gen_moves = lambda start: [
movx + movy
for movx, movy in (
(chr(ord(start[0]) + dirx), chr(ord(start[1]) + diry)) for dirx, diry in DIRS)
if movx >= 'a' and movx <= 'h' and movy >= '1' and movy <= '8']
def chess_knight(start, moves):
movelist = gen_moves(start)
nextmoves = sum((chess_knight(cell, moves - 1) for cell in movelist), []) if moves > 1 else []
return sorted(set(movelist + nextmoves))
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!")
May 5, 2018
Comments: