[BOJ_Python] 4659번 비밀번호 발음하기 (실버 5)

황준성·2024년 10월 29일
0

BOJ_Python

목록 보기
9/70

문제 4659

입력

입력은 여러개의 테스트 케이스로 이루어져 있다.

각 테스트 케이스는 한 줄로 이루어져 있으며, 각 줄에 테스트할 패스워드가 주어진다.

마지막 테스트 케이스는 end이며, 패스워드는 한글자 이상 20글자 이하의 문자열이다. 또한 패스워드는 대문자를 포함하지 않는다.

출력

각 테스트 케이스를 '예제 출력'의 형태에 기반하여 품질을 평가하여라.

입력 예시 1

a
tv
ptoui
bontres
zoggax
wiinq
eep
houctuh
end

출력 예시 1

<a> is acceptable.
<tv> is not acceptable.
<ptoui> is not acceptable.
<bontres> is not acceptable.
<zoggax> is not acceptable.
<wiinq> is not acceptable.
<eep> is acceptable.
<houctuh> is acceptable.

내가 짠 코드

# BOJ 4659번 비밀번호 발음하기

lst = ["a", "e", "i", "o", "u"]

while True:

    password = input()

    if (password == "end"):
        break
    
    # 모음 하나 포함하는지 확인
    count = 0
    for i in password:
        if i in lst:
            count += 1
    
    if count < 1:
        print(f"<{password}> is not acceptable.")
        continue

    # 모음이 3개 혹은 자음이 3개 연속인지 확인
    count2 = 0
    for i in range(len(password)-2):
        if (password[i] in lst and password[i+1] in lst and password[i+2] in lst):
            count2 = 1
        elif not(password[i] in lst) and not(password[i+1] in lst) and not(password[i+2] in lst):
            count2 = 1
    
    if count2 == 1:
        print(f"<{password}> is not acceptable.")
        continue

    # 같은 글자가 연속적으로 두번 오는지 확인
    count3 = 0
    for i in range(len(password)-1):
        if (password[i] == password[i+1]):
            if password[i] == "e" or password[i] == "o":
                continue
            else:
                count3 = 1
    
    if count3 == 1:
        print(f"<{password}> is not acceptable.")
        continue

    print(f"<{password}> is acceptable.")

풀이 설명

첫번째 조건에 대한 반복문

# BOJ 4659번 비밀번호 발음하기

lst = ["a", "e", "i", "o", "u"]

# 모음 하나 포함하는지 확인
count = 0
for i in password:
	if i in lst:
		count += 1
    
if count < 1:
	print(f"<{password}> is not acceptable.")
	continue

따로 확인해줄 모음을 리스트에 모아서 하나씩 확인하는 과정에서 배울점이 많았다. i라는 값을 통일해서 i 값이 lst에 있는지 확인할 수 있단 것이 정말 놀라웠다.
for 반복문의 범위를 문자열로 설정해서 하나씩 뽑아서 (if i in lst)라는 조건문을 통해서 확인한다. in이라는 도구(?)를 통해서 간결하게 코드를 짤 수 있다.

아래에는 내가 이것을 알기전에 짰던 코드이다.

# BOJ 4659번 비밀번호 발음하기

lst = ["a", "e", "i", "o", "u"]

# 모음 하나 포함하는지 확인
count = 0
for i range(len(password)):
	for j in lst:
    	if password[i] == j:
			count += 1
    
if count < 1:
	print(f"<{password}> is not acceptable.")
	continue

위 처럼 이중 반복문을 사용해서 하나하나씩 비교를 했는데 같은 i로 통일하면 반복문 하나로도 구현할 수 있다. 이런 디테일을 문제를 풀면서 배워가는 것 같다.

두번째 조건에 대한 반복문

# 모음이 3개 혹은 자음이 3개 연속인지 확인
    count2 = 0
    for i in range(len(password)-2):
        if (password[i] in lst and password[i+1] in lst and password[i+2] in lst):
            count2 = 1
        elif not(password[i] in lst) and not(password[i+1] in lst) and not(password[i+2] in lst):
            count2 = 1

이것도 in lst를 이용해서 비교적 간편하게 값을 구할 수 있다. c언어라면 아마 이중 반복문을 통해서 확인을 해야겠지만, 파이썬으로는 엄청 간결하게 구현이 가능하다.

profile
Make progress

0개의 댓글