문제링크
https://programmers.co.kr/learn/courses/30/lessons/64065
코딩테스트 연습 - 튜플
"{{2},{2,1},{2,1,3},{2,1,3,4}}" [2, 1, 3, 4] "{{1,2,3},{2,1},{1,2,4,3},{2}}" [2, 1, 3, 4] "{{4,2,3},{3},{2,3,4,1},{2,3}}" [3, 2, 4, 1]
programmers.co.kr
풀이
- 맨 앞 '{{', 맨 뒤 '}}' 제거
- '},{' 기준으로 잘라서 각 집합 얻어냄
- 길이 기준으로 정렬
- 집합 별로 ',' 기준으로 잘라서 숫자만 얻어낸 후, 새로운 숫자면 answer에 추가
def solution(s):
s = s[2:-2]
s = s.split('},{')
s.sort(key=len)
answer = []
for i in s:
tmp = i.split(',')
for num in tmp:
if int(num) not in answer:
answer.append(int(num))
return answer
'개발 > algorithm' 카테고리의 다른 글
[프로그래머스][level3] 가장 먼 노드 -python (0) | 2021.12.29 |
---|---|
[프로그래머스] [level2] 소수 찾기 - python (0) | 2021.12.29 |
[프로그래머스][level2] 방문 길이 -python (0) | 2021.12.28 |
[프로그래머스][level2] 올바른 괄호 -python (0) | 2021.12.28 |
[프로그래머스][level2] 삼각 달팽이 - python (0) | 2021.12.28 |