react 공부와 캡스톤 디자인 프로젝트 때문에 코테를 잠시 못했다😂
04.26에 푼 문제임당🌷
키패드 누르기
def solution(numbers, hand):
answer = ''
lPhone=[1,4,7,'*']
rPhone=[3,6,9,'#']
mP=[2,5,8,0]
lH=True
rH=False
lindex=3
rindex=3
for i in numbers:
#왼손 키패트
if i==1 or i==4 or i==7:
answer+='L'
lH=True
#왼손의 위치를 갱신
lindex=lPhone.index(i)
continue
#오른손 키패드
elif i==3 or i==6 or i==9:
answer+='R'
rH=True
#오른손의 위치를 갱신
rindex=rPhone.index(i)
continue
#가운데 키패드
else :
index=mP.index(i)
rlen=abs(rindex-index)
llen= abs(lindex-index)
#둘 중 한손이 가운데 키패드가 아닐때
if rH != lH:
if rH : rlen +=1
if lH : llen +=1
if llen < rlen:
answer+='L'
lH=False
lindex=index
elif llen > rlen:
answer+='R'
rH=False
rindex=index
elif llen == rlen:
if hand=='right':
answer+='R'
rH=False
rindex=index
else:
answer+='L'
lH=False
lindex=index
return answer
몇개 틀린 테스트가 발생한다....😭😭😭😭
def solution(numbers, hand):
answer = ''
phone=[[3,1],[0,0],[0,1],[0,2], #0,1,2,3
[1,0],[1,1],[1,2], #4,5,6
[2,0],[2,1],[2,2], #7,8,9
]
lh=[3,0] #'*'
rh=[3,2] #'#'
for i in numbers:
if i==1 or i==4 or i==7:
answer+='L'
lh=phone[i]
elif i==3 or i==6 or i==9:
answer+='R'
rh=phone[i]
else:
now=phone[i]
l_dist=abs(now[0]-lh[0])+abs(now[1]-lh[1])
r_dist=abs(now[0]-rh[0])+abs(now[1]-rh[1])
if l_dist < r_dist :
answer+='L'
lh=now
elif l_dist > r_dist:
answer+='R'
rh=now
else :
if hand=='right':
answer+='R'
rh=now
else:
answer+='L'
lh=now
return answer
왼손 키패드, 오른손 키패드를 나누지 않고 폰 index를 폰 번호로 하고 값을 위치로 선언한다.
훨씬 효율성도 좋고 정확도도 100% 나온 코드다👍🐰🐰
코드에 오류가 있을 때 새로 갈아 엎는 것도 좋은 방법인 것 같다,,ㅎㅎㅎ