Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Chess Knight by B_dur
def chess_knight(start, moves):
#replace this for solution
movable_knight=[(2,1),(1,2),(-1,2),(-2,1),(-2,-1),(-1,-2),(1,-2),(2,-1)]
starts={start}
goals=set()
for i in range(moves):
while starts:
start=starts.pop()
for movable in movable_knight:
goal=calc_cells(start,movable[0],movable[1])
if goal:
goals.add(goal)
starts|=goals
return sorted(list(goals))
def calc_cells(start,vertical,horizontal):
from string import ascii_lowercase
h=ascii_lowercase.index(start[0])+horizontal
v=int(start[1])+vertical
return ascii_lowercase[h]+str(v) if 0<=h<=7 and 1<=v<=8 else None
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!")
Aug. 26, 2020