📑 문제
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
📝 풀이
def solution(s):
count = {}
s = s[2:-2]
arr = s.split('},{')
new_arr = []
for n in arr:
n = list(map(int, n.split(',')))
new_arr.append(n)
for t in new_arr:
for n in t:
if n in count:
count[n]+=1
else:
count[n] = 1
sorted_dict = sorted(count.items(), key = lambda x : x[1], reverse=True)
return list(map(int,(x[0] for x in sorted_dict)))
input | s = {{2},{2,1},{2,1,3},{2,1,3,4}} |
s = s[2:-2] | s = 2},{2,1},{2,1,3},{2,1,3,4 |
arr = s.split('},{') | arr = ['2', '2,1', '2,1,3', '2,1,3,4'] |
new_arr = [] for n in arr: n = list(map(int, n.split(','))) new_arr.append(n) |
new_arr = [[2],[2,1],[2,1,3],[2,1,3,4]] |
다음과 같은 흐름으로 주어진 s를 dict로 변환하기 쉽게 만들어준 다음에 빈도수가 가장 큰 숫자부터 차례대로 출력해주었다.
🚬 쌔벼온 멋진 풀이
def solution(s):
s = Counter(re.findall('\d+', s))
return list(map(int, [k for k, v in sorted(s.items(), key=lambda x: x[1], reverse=True)]))
import re
from collections import Counter
나의 개뻘짓이 정규식 한 줄로 끝나버렸다 진짜 자존감 개박살
정규식 공부의 필요성을 또 한 번 느낀다🥹
728x90
'💡코딩테스트 > 프로그래머스' 카테고리의 다른 글
[python | 프로그래머스] LV.2 게임 맵 최단거리 (0) | 2023.04.05 |
---|---|
[python | 프로그래머스] LV 2. [1차] 프렌즈4블록 (0) | 2023.04.04 |
[프로그래머스 | 2019 카카오 개발자 겨울 인턴십] 크레인 인형뽑기 게임 (0) | 2023.02.07 |
[프로그래머스 | 2020 카카오 인턴십] 키패드 누르기 (0) | 2023.02.07 |
[프로그래머스 | 2021 카카오 채용연계형 인턴십] 숫자 문자열과 영단어 (0) | 2023.02.07 |