문제 풀이/백준
[Python] 백준 1264 : 모음의 개수
동까의 코딩
2024. 3. 5. 14:01
반응형
오늘은 기본 구현문제를 풀어보았습니다.
while 반복문을 통해 계속해서 루프를 돌게 만들고, '#'이 입력으로 들어오면 반복문을 나가준다.
조건문을 통해 count를 추가해 주는 코드를 세워 주었다.
while True:
string_input = input()
cnt = 0
if string_input == '#':
break
for st in string_input:
if st == 'a' or st == 'A':
cnt += 1
elif st == 'e'or st == 'E':
cnt += 1
elif st == 'i' or st == 'I':
cnt += 1
elif st == 'o' or st == 'O':
cnt += 1
elif st == 'U' or st == 'u':
cnt += 1
print(cnt)
해당 코드는 정답으로 처리가 되었지만 더 간결한 코드가 있을 것 같아 자료를 찾아보니 같은 구현이라도 더 간단하게 만든 코드가 존재하였다.
collection = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']
while True:
cnt = 0
string_input = input()
if string_input == '#':
break
for st in string_input:
if st in collection:
cnt += 1
print(cnt)
collection으로 묶고 해주니 코드가 더 간결해졌다.
반응형