하루에 하나씩 공부하기
코딩테스트 입문 - 옹알이(1) 본문
- 문제

- 배운 점
문자열 순열 생성
1. 순열 : nPr 순서O 중복X
from itertools import permutations
permutations(a,b) : aPb
2. 조합 : nCr 순서X 중복X
from itertools import combinations
combinations(a,b) : aCb
- 코드
from itertools import permutations
def solution(babbling):
answer = 0
bab = ["aya", "ye", "woo", "ma"]
results=[]
# pertmutations 사용해 순열 생성 (babPi)
for i in range(1, len(bab)+1):
for p in permutations(bab, i):
results.append(''.join(p))
for j in babbling :
if j in results:
answer+=1
return answer
정규표현식 사용
- 첫문자가 aya, ye, woo, ma로 시작하고(^) 끝나는($) 단어
- +로 한번 이상 반복
import re
def solution(babbling):
regex = re.compile('^(aya|ye|woo|ma)+$')
cnt=0
for e in babbling:
if regex.match(e):
cnt+=1
return cnt'파이썬' 카테고리의 다른 글
| 프로그래머스 LV1 - 유연근무제 (0) | 2025.06.27 |
|---|---|
| K번째수-LV1 (0) | 2025.03.09 |
| 베스트앨범-LV3 (1) | 2025.03.09 |
| 의상-LV2 (0) | 2025.02.28 |
| 전화번호 목록-LV2 (0) | 2025.02.27 |