[프로그래머스] 튜플

Gaanii·2025년 5월 9일
0

Problem Solving

목록 보기
192/210
post-thumbnail

아래 프로그래머스 로고를 클릭하면 해당 문제로 이동합니다 😀

프로그래머스로고



풀이과정



사실 이 문제는 우리 스터디 팀원이 아이디어를 다 내어줬다 ,,, 나는 이런 생각조차 못했다 ,,, 샤라웃 투 서라씨 ,,,

이 문제는 아이디어를 생각하느냐 + 정규식을 쓸 줄 아느냐가 핵심인 것 같다.

정규식으로 숫자만 걸러낸 후, 각 숫자의 빈도수를 체크한다.
그리고 내림차순으로 정렬하고 그 숫자를 배열로 리턴해주면 된다.

정규식에 대한 정리는 추후에 ,, 추가 ,, 예정 ,,

코드


1. Python

import re
from collections import Counter
def solution(s):
    numbers = re.findall(r'\d+', s)
    
    count = Counter(numbers)
    sorted_nums = sorted(count.items(), key = lambda x: -x[1])
    
    return [int(num) for num, _ in sorted_nums]

2. JS

function solution(s) {
    const numbers = s.match(/\d+/g);
    
    const count = {};
    numbers.forEach((num) => {
        count[num] = (count[num] || 0) + 1;
    });
    
    const sorted = Object.entries(count).sort((a, b) => b[1] - a[1]);
    return sorted.map(([num]) => +num)
}


결과


정답

0개의 댓글