Skip to content

Commit c6a3113

Browse files
committed
[PGS] 키패드누르기 / Level1 / 30분 / 힌트, 성공
1 parent a555d7d commit c6a3113

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
def solution(numbers, hand):
2+
answer = ''
3+
pad = {'1': (0, 0), '2': (0, 1), '3': (0, 2),
4+
'4': (1, 0), '5': (1, 1), '6': (1, 2),
5+
'7': (2, 0), '8': (2, 1), '9': (2, 2),
6+
'*': (3, 0), '0': (3, 1), '#': (3, 2)
7+
}
8+
9+
left = pad['*']
10+
right = pad['#']
11+
12+
for num in numbers:
13+
# 왼손이 눌러야 하는 번호
14+
if num in (1, 4, 7):
15+
answer += 'L'
16+
left = pad[str(num)]
17+
# 오른손이 눌러야 하는 번호
18+
elif num in (3, 6, 9):
19+
answer += 'R'
20+
right = pad[str(num)]
21+
# 가운데 번호일 경우 (2, 5, 8, 0)
22+
else:
23+
24+
# 해당 번호와 왼손 거리
25+
left_dist = abs(left[0] - pad[str(num)][0]) + abs(left[1] - pad[str(num)][1])
26+
# 해당 번호와 오른손 거리
27+
right_dist = abs(right[0] - pad[str(num)][0]) + abs(right[1] - pad[str(num)][1])
28+
29+
# 더 가까운 거리
30+
if left_dist < right_dist:
31+
answer += 'L'
32+
left = pad[str(num)]
33+
elif left_dist > right_dist:
34+
answer += 'R'
35+
right = pad[str(num)]
36+
# 왼손과 오른손 거리가 같을 경우
37+
else:
38+
if hand == 'right':
39+
answer += 'R'
40+
right = pad[str(num)]
41+
else:
42+
answer += 'L'
43+
left = pad[str(num)]
44+
45+
return answer

0 commit comments

Comments
 (0)