본문 바로가기
스터디/알고리즘

튜플

by 세졍 2024. 6. 18.

문제이해

 튜플 
(a1, a2, a3, ..., an)
이 주어질 때

{{a1}, {a1, a2}, {a1, a2, a3}, {a1, a2, a3, a4}, ... {a1, a2, a3, a4, ..., an}}
표현할 수 있다
위의 식이 주어질때 튜플의 원소 구하기


1. 주어진 s를 잘 가공하는게 핵심

2.각 요소의 길이를 가지고 오름차순정렬하기

3. answer배열에 없으면 담기 (중복되는 숫자는 빼고 담기)

 

 

def solution(s):
    answer = []
    s = s[2:-2].split('},{')
    s = sorted(s,key=lambda x:len(x))
    for i in s:
        numbers = i.split(',')
        for num in numbers:
            if int(num) not in answer:
                answer.append(int(num))
    return answer



s = "{{20,111},{111}}"
print(solution(s))

'스터디 > 알고리즘' 카테고리의 다른 글

거스름돈(dp)  (0) 2024.06.23
*지형이동  (0) 2024.06.18
배달  (1) 2024.06.12
양과늑대  (0) 2024.06.01
미로 탈출  (0) 2024.05.31