일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- programmers
- 파이썬
- CodingTest
- BFS
- 코딩테스트
- 완전탐색
- 시간복잡도
- sql
- Level2
- python
- import re
- coding
- 코딩
- lv4
- 프로그래머스
- 데이터분석
- level4
- coding test
- join
- counter
- collections
- Stack
- itertools
- 코테
- 조합
- lambda
- time complexity
- Queue
- mysql
- 연습문제
- Today
- Total
ror_coding
[Programmers] 튜플 - 64065 본문
728x90
import re를 활용해서 숫자만 추출한 후에 Counter로 개수 세기!
Question
특정 튜플을 표현하는 집합이 담긴 문자열 s가 매개변수로 주어질 때, s가 표현하는 튜플을 배열에 담아 return 하도록 solution 함수를 완성해주세요.
Point
- Counter를 사용 -> 여러 번 나온 수가 먼저 등장한 숫자 => 따라서 등장 횟수가 큰 숫자부터 저장.
Code 1 : 정규표현식, Counter 사용.
import re
from collections import Counter
def solution(s):
s = Counter(re.findall('\d+', s))
return [int(v) for v, cnt in s.most_common()]
Code 2 : mine
def solution(s):
answer, lst = [], []
for i in s[2:-2].split('},{'): lst.append(i.split(','))
sorted_s = sorted(lst, key = lambda x : len(x))
answer.append(sorted_s[0][0])
for sub in sorted_s[1:] :
print(set(sub) - set(answer))
rest = (set(sub) - set(answer))
r = (list(rest)[0])
answer.append(r)
return [int(i) for i in answer]
![](https://t1.daumcdn.net/keditor/emoticon/friends1/large/008.gif)
now me
On my github
728x90
'Algorithm > Python' 카테고리의 다른 글
[Programmers] 타겟 넘버 - 43165 (0) | 2024.12.05 |
---|---|
[Programmers] 프로세스 - 42587 (0) | 2024.12.04 |
[Python] re (정규표현식) (0) | 2024.12.04 |
[Python] Permutations, Combination, Product (Itertools) (0) | 2024.12.04 |
[Programmers] 피로도 - 87946 (3) | 2024.12.04 |