이것이 취업을 위한 코딩 테스트다 with 파이썬을 공부하면서 정리한 내용입니다.
a1
2
c2
6
# 현재 나이트의 위치 입력받기
input_data = input()
row = int(input_data[1])
column = ord(input_data[0]) - ord('a') + 1
# 나이트가 이동할 수 있는 8가지 방향
steps = ((-2, -1), (-1, -2), (1, -2), (2, -1), (2, 1), (1, 2), (-1, 2), (-2, 1))
# 8가지 방향에 대해 각 위치로 이동이 가능한지 확인
result = 0
for step in steps:
# 이동하고자 하는 위치 확인
next_row = row + step[0]
next_column = column + step[1]
# 해당 위치로 이동이 가능하면 카운트 증가
if 1 <= next_row <= 8 and 1 <= next_column <= 8:
result += 1
print(result)
import sys
# 나이트의 현재 위치 입력받기
location = list(sys.stdin.readline().rstrip())
init_x, init_y = location[0], int(location[1])
if init_x == 'a':
init_x = 1
elif init_x == 'b':
init_x = 2
elif init_x == 'c':
init_x = 3
elif init_x == 'd':
init_x = 4
elif init_x == 'e':
init_x = 5
elif init_x == 'f':
init_x = 6
elif init_x == 'g':
init_x = 7
elif init_x == 'h':
init_x = 8
# x, y는 나이트를 움직였을 때의 위치
x, y = init_x, init_y
count = 0
# 나이트를 8가지 방향으로 움직이면서 가능한 경우 세기
# 왼쪽으로 2번 이동
if (x := init_x - 2) >= 1:
# 위쪽으로 1번 이동
if (y := init_y - 1) >= 1:
count += 1
# 아래쪽으로 1번 이동
if (y := init_y - 1) <= 8:
count += 1
# 오른쪽으로 2번 이동
if (x := init_x + 2) <= 8:
# 위쪽으로 1번 이동
if (y := init_y - 1) >= 1:
count += 1
# 아래쪽으로 1번 이동
if (y := init_y + 1) <= 8:
count += 1
# 위쪽으로 2번 이동
if (y := init_y - 2) >= 1:
# 왼쪽으로 1번 이동
if (x := init_x - 1) >= 1:
count += 1
# 오른쪽으로 1번 이동
if (x := init_x + 1) <= 8:
count += 1
# 아래쪽으로 2번 이동
if (y := init_y + 2) >= 1:
# 왼쪽으로 1번 이동
if (x := init_x - 1) >= 1:
count += 1
# 오른쪽으로 1번 이동
if (x := init_x + 2) <= 8:
count += 1
print(count)