Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
set.union solution in Clear category for Chess Knight by Sim0000
def chess_knight(start, moves):
toNum = lambda s: (ord(s[0]) - 96, int(s[1]))
toStr = lambda pos: chr(96 + pos[0]) + str(pos[1])
def move(start):
y, x = start
return {(y+dy, x+dx) for dy,dx in DIR if 1<=y+dy<=8 and 1<=x+dx<=8}
DIR = [(-1, -2), (-2, -1), (-2, 1), (-1, 2), (1, 2), (2, 1), (2, -1), (1, -2)]
result = {toNum(start)}
for _ in range(moves):
result |= set.union(*[move(p) for p in result])
if moves < 2: result -= {toNum(start)}
return sorted(map(toStr, 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!")
April 24, 2018