Computer Knowledge/코테 대비 오답노트
⭐️[python] 옹알이(1) - permutations, replace
l._.been
2023. 6. 1. 12:22
728x90
문제
https://school.programmers.co.kr/learn/courses/30/lessons/120956
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
어려웠던 문제 중 하나...
너무너무 어렵다..
- 옹알이를 하는 아이는 "aya", "ma", "ye", "woo"를 조합한 옹알이를 할 수 있다.
- 아이가 한 옹알이(단어) 갯수를 구하라
인상적인 풀이들
1. itertools를 활용한 풀이
from itertools import permutations
def solution(babbling):
count = 0
# 1, 2, 3, 4가지 경우에 대해 permutations 구하기
basic_babblings = ["aya", "ye", "woo", "ma"]
all_babblings = []
for i in range(1, 5):
perms = list(permutations(basic_babblings, i))
for perm in perms:
s = ''
for element in perm:
s += element
all_babblings.append(s)
for babblings in all_babblings:
for babble in babbling:
if babblings == babble:
count += 1
return count
2. replace를 활용한 풀이
def solution(babbling):
c = 0
for b in babbling:
for w in [ "aya", "ye", "woo", "ma" ]:
if w * 2 not in b:
b = b.replace(w, ' ')
if len(b.strip()) == 0:
c += 1
return c