[실패]
def solution(cards1, cards2, goal):
for word in goal:
if word == cards1[0]:
cards1 = cards1.pop()
goal.pop()
elif word == cards2[0]:
cards2 = cards2.pop()
goal.pop()
else:
return 'No'
break
return 'Yes'
각 리스트를 한개씩 지워가는 방식으로 코드를 짰는데
계속 코드를 작성하다 보니까 다른 때도 그렇고, 되도록이면 받은 리스트를 지우거나 건드리지 않고 인덱스를 설정해서 순회를 통해 답을 찾아가는 방식이 좋은 것 같다.
[성공]
def solution(cards1, cards2, goal):
idx1 = 0
idx2 = 0
for word in goal:
if idx1 < len(cards1) and word == cards1[idx1]:
idx1 += 1
elif idx2 < len(cards2) and word == cards2[idx2]:
idx2 += 1
else:
return 'No'
break
return 'Yes''파이썬 > 백준' 카테고리의 다른 글
| 프로그래머스 파이썬 길 찾기 게임 (0) | 2023.11.06 |
|---|---|
| 프로그래머스 | 파이썬 과제 진행하기 (1) | 2023.11.04 |
| 프로그래머스 | 달리기 경주 (python) (0) | 2023.07.13 |
| 프로그래머스 _ sqrt와 조합 사용한 문제 (0) | 2023.04.30 |
| python ) 백준 #1541 잃어버린 괄호 (0) | 2022.10.17 |