Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
set object solution in Clear category for Chess Knight by kurosawa4434
from itertools import chain
def chess_knight(start, moves):
all_cells = {r+f for r in 'abcdefgh' for f in '12345678'}
def knight_move_list(cell):
rk, fi = ord(cell[0]), int(cell[1])
kml = ((-2, 1), (-1, 2), (1, 2), (2, 1),
(-2, -1), (-1, -2), (1, -2), (2, -1))
return {chr(rk+r) + str(fi+f) for r, f in kml} & all_cells
nexts = set([start])
result = set()
for _ in range(moves):
nexts = set(chain(*(map(knight_move_list, nexts)))) - result
result |= nexts
return sorted(result)
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 3, 2018
Comments: