Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in 3rd party category for Chess Knight by spark856
import numpy as np
rot = [[0,-1],[1,0]]
def chess_knight(start, moves):
if moves==0:return [start]
ans = []
i,j = ord(start[0]),int(start[1])
vec = [1,2]
for n in range(8):
if n==4:vec = vec[::-1]
vec = np.dot(rot,vec)
x,y = vec
if ord("a")<= i+x <=ord("h") and 1<= j+y <=8:
ans.append(chr(i+x)+str(j+y))
ans.extend(chess_knight(chr(i+x)+str(j+y),moves-1))
return sorted(set(ans))
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, 2020