Notice
Recent Posts
Recent Comments
Link
«   2025/05   »
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 29 30 31
Archives
Today
Total
관리 메뉴

:)

[프로그래머스]튜플, [프로그래머스]스킬트리 본문

Algorithm(python)

[프로그래머스]튜플, [프로그래머스]스킬트리

mihee 2022. 10. 5. 18:17

https://school.programmers.co.kr/learn/courses/30/lessons/64065?language=python3 

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

1) string으로 들어오는 input 값 리스트로 값 넣어주기

2) 리스트에서 첫번째 원소는 n개, 두번째 원소는 n-1개, N번째 원소는 1개 데이터 존재

3) collections 모듈 Counter 사용해 가장 많이 나온 데이터 찾기(most_common()) 

from collections import Counter

def solution(s):
    for c in '{},':
        s = s.replace(c, ' ')
    
    arr = [int(n) for n in s.split()]

    counter = Counter(arr)
    answer = [num for num, _cnt in counter.most_common()]

    return answer

정규표현식으로 replace(), split() 쉽게 하기

from collections import Counter
from re import findall

def solution(s):
    return [int(num) for num, _ in Counter(findall('\d+', s)).most_common()]

 

 

https://school.programmers.co.kr/learn/courses/30/lessons/49993

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

구현

스킬트리를 따르는지 for , if ~ 로 판단

def solution(skill, skill_trees):
    ans = 0
    for skill_tree in skill_trees:
        i = 0
        for s in skill_tree:
            if s in skill:
                if s == skill[i]:
                    i +=1
                else:
                    break
        else:
            ans +=1
                    
    return ans

정규표현식 사용 풀이

 

- skill이 아닌 문자는 ''으로 치환 --> re.sub('[^' + skill + ']', '', st)

- startswith 로 스킬트리를 따르는지 판별

import re

def solution(skill, skill_trees):
    return sum(skill.startswith(re.sub('[^' + skill + ']', '', st)) for st in skill_trees)
Comments