Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Chess Knight by Elena_Korljukova
w = ' abcdefgh '
q = ' 12345678 '
def knight(p):
t = []
for x in p:
k, n = w.index(x[0]), q.index(x[1])
t += [w[i] + q[j] for (i, j) in ((k - 2, n - 1), (k - 1, n - 2), (k - 2, n + 1), (k - 1, n + 2), (k + 1, n - 2), (k + 2, n - 1), (k + 1, n + 2), (k + 2, n + 1))]
return [i for i in t if ' ' not in i]
def chess_knight(start, moves):
a = knight([start])
for i in range(2, moves + 1):
a += knight(a)
return sorted(set(a), key = lambda x : (x[0], x[1]))
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. 22, 2020