[백준/Python] 4659번. 비밀번호 발음하기

문제

https://www.acmicpc.net/problem/4659

 

 

 

풀이

단순하게 주어진 조건을 검사하여 출력하면 된다.

 

 

로직은 다음과 같다

단어의 판정 결과를 저장할 isAcceptable 변수를 False로 초기화한다.

연속된 자음과 모음을 세는 카운터 변수를 만들고 0으로 초기화한다

 

단어를 글쇠별로 하나씩 끝까지 검사하는데

글쇠가 모음에 해당되면 모음 카운터를 증가시키며 자음 카운터를 0으로 초기화 하고,
글쇠가 자음에 해당되면 자음 카운터를 증가시키고 모음 카운터를 0으로 초기화 시킨다.

모음은 최소 한개 이상 존재해야 하기 때문에 모음 카운터를 증가할땐 isAcceptable에 True를 저장한다.

카운터가 3 이상이거나 e,o가 아닌 문자가 연속으로 2회 등장시 isAcceptable를 False로 바꾸고 단어 검사를 종료한다.

 

단어 검사가 종료되면되면 isAcceptable 변수에 담긴 판정 결과를 출력한다.

 

 

 

 

정답 코드

while True:
    word = input()
    if word == 'end':
        break

    isAcceptable = False
    vowelsCounter = 0 #모음
    consonantCounter = 0 #자음

    for index, letter in enumerate(word):
        if index > 0 and word[index - 1] == letter:
            if letter not in 'eo':
                isAcceptable = False
                break

        if letter in 'aeiou':
            isAcceptable = True
            vowelsCounter += 1
            consonantCounter = 0
        else:
            consonantCounter += 1
            vowelsCounter = 0
        if vowelsCounter >= 3 or consonantCounter >= 3:
            isAcceptable = False
            break

    if isAcceptable:
        print(f'<{word}> is acceptable.')
    else:
        print(f'<{word}> is not acceptable.')