โถ ๋ฌธ์ :
โถ ์ฝ๋ :
keyPad = {
'1' : (0,0),
'2' : (0,1),
'3' : (0,2),
'4' : (1,0),
'5' : (1,1),
'6' : (1,2),
'7' : (2,0),
'8' : (2,1),
'9' : (2,2),
'*' : (3,0),
'0' : (3,1),
'#' : (3,2)
}
def solution(numbers, hand):
answer=''
left = '*'
right = '#'
for num in numbers:
if num in [1, 4, 7] :
answer += 'L'
left = str(num)
elif num in [3, 6, 9] :
answer += 'R'
right= str(num)
else:
leftLength = abs(keyPad[left][0] - keyPad[str(num)][0]) + abs(keyPad[left][1] - keyPad[str(num)][1])
# ํ์ฌ ์ผ์์์น์์ ๋ชฉํ๊น์ง ๊ฑฐ๋ฆฌ
rightLength = abs(keyPad[right][0] - keyPad[str(num)][0]) + abs(keyPad[right][1] - keyPad[str(num)][1])
# ํ์ฌ ์ค๋ฅธ์์์น์์ ๋ชฉํ๊น์ง ๊ฑฐ๋ฆฌ
if leftLength < rightLength:
answer += 'L'
left = str(num)
elif leftLength > rightLength:
answer+='R'
right = str(num)
else:
if hand == 'left':
answer+='L'
left = str(num)
else:
answer+='R'
right = str(num)
return answer
โถ ๋ฌธ์ ํด์ค :
1. ๊ฐ๊ฐ์ ํคํจ๋์ ๋ํ ์ขํ๋ฅผ ํด์ํ ์ด๋ธ์ ์ ์ฅ => (0,0) ์ 1๋ฒ
2. ์ดํ ์ฃผ์ด์ง ๋ฌธ์์ด์ ๋ํด ๊ฐ๊ฐ์ ๋ฒํธ์ธ ๊ฒฝ์ฐ์ ๋ํ ์ฒ๋ฆฌ๋ฅผ ํด์ฃผ์์
'๐ฏ CodingTest > Programmers' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Programmers] ์คํจ์จ (0) | 2022.04.18 |
---|---|
[Programmers] ํฌ๋ ์ธ ์ธํ๋ฝ๊ธฐ ๊ฒ์ (0) | 2022.04.18 |
[Programmers] ์ซ์ ๋ฌธ์์ด๊ณผ ์๋จ์ด (0) | 2022.04.11 |
[Programmers] ์ ๊ณ ๊ฒฐ๊ณผ ๋ฐ๊ธฐ (0) | 2022.04.11 |
[Programmers] ์ซ์ ๋ฌธ์์ด๊ณผ ์๋จ์ด (0) | 2022.04.09 |