[파이썬/python] 프로그래머스 : 카드 뭉치(Lv.1)

 

https://school.programmers.co.kr/learn/courses/30/lessons/159994

def solution(c1, c2, goal):
    for i in goal:
        if c1[0] == i:
            c1.pop(0)
            c1.append(0)
        elif c2[0] == i:
            c2.pop(0)
            c2.append(0)
        else:
            return "No"
    return "Yes"

나는 index error가 나지 않도록 pop을 해준 다음 다시 append를 해 주었는데

def solution(cards1, cards2, goal):
    for g in goal:
        if len(cards1) > 0 and g == cards1[0]:
            cards1.pop(0)       
        elif len(cards2) >0 and g == cards2[0]:
            cards2.pop(0)
        else:
            return "No"
    return "Yes"

이런 식으로 먼저 길이를 체크한다면 굳이 append를 하지 않아도 될 것이다

 

comment