첫번째 코드
사실 첫번째라기에는 연속해서 발음하는 것이 불가능하다는 것을 보지 않았다...
문제를 명확히 보도록 하자!
그리고 수정한 방법으로 아래와 같이 작성하였다.
연속한 경우 발음이 되지 않으므로 해당 문자열을 작성한다.
그리고 해당 단어가 있는 경우 'x'로 대체한다.
똑같은 방법으로 발음이 가능한 경우 해당 값을 삭제한다.
그 결과 모든 값이 없는 경우 발음이 가능한 경우이므로 count에 1을 더한다.
def solution(babbling):
count = 0
can_speak = ['aya', 'ye', 'woo', 'ma']
cant_speak = ['ayaaya', 'yeye', 'woowoo', 'mama']
for i in range(len(babbling)):
word = babbling[i]
for k in cant_speak:
if k in word:
word = word.replace(k, 'x')
for j in can_speak:
if j in word:
word = word.replace(j, '')
if not word:
count += 1
return count
오류
두번째 코드
j가 발음 가능한 경우 해당 단어를 지워버렸더니, 지우고 난 후에 만나서 발음 가능한 단어가 되는 경우가 문제가 되었다.
예를 들어, ["yayae"]의 경우 발음이 되지 않지만 발음이 된다는 결과가 나온다는 것이다.
그러므로 발음이 가능한 경우 다른 값으로 대체한 후 마지막에 해당 값을 바꾸자!
def solution(babbling):
count = 0
can_speak = ['aya', 'ye', 'woo', 'ma']
cant_speak = ['ayaaya', 'yeye', 'woowoo', 'mama']
for i in range(len(babbling)):
word = babbling[i]
for k in cant_speak:
if k in word:
word = word.replace(k, 'x')
for j in can_speak:
if j in word:
word = word.replace(j, ' ')
word = word.replace(' ', '')
if not word:
count += 1
return count
통과!
링크
programmers/프로그래머스/1/133499. 옹알이 (2) at main · ornni/programmers
repository for recording Programmers Algorithm problem solving - ornni/programmers
github.com
'코딩 테스트 > 프로그래머스' 카테고리의 다른 글
[1차] 다트 게임 (0) | 2024.08.23 |
---|---|
숫자 문자열과 영단어 (0) | 2024.08.19 |
문자열 내 p와 y의 개수 (0) | 2024.08.12 |
이어 붙인 수 (0) | 2024.08.09 |
정수 제곱근 판별 (0) | 2024.07.26 |