3613번: Java vs C++
Java 예찬론자 김동규와 C++ 옹호가 김동혁은 서로 어떤 프로그래밍 언어가 최고인지 몇 시간동안 토론을 하곤 했다. 동규는 Java가 명확하고 에러가 적은 프로그램을 만든다고 주장했고, 동혁이는
www.acmicpc.net
코드
input_str = input()
if input_str[0] == '_' or input_str[0].isupper() or input_str[-1] == '_':
print("Error!")
exit()
if input_str.count('_') > 0: # C++ => Java
new_str = ''
underbar = False
for i in range(len(input_str)):
if input_str[i].isupper():
new_str = "Error!"
break
if input_str[i].isalpha():
if underbar:
new_str += input_str[i].upper()
underbar = False
else:
new_str += input_str[i]
elif not underbar and input_str[i] == '_':
underbar = True
else:
new_str = "Error!"
break
else: # Java => C++
new_str = ''
for i in range(len(input_str)):
if input_str[i].isupper():
new_str += ('_'+input_str[i].lower())
elif input_str[i].islower():
new_str += input_str[i]
else:
new_str = "Error!"
break
print(new_str)
Error! 결과가 나오는 경우를 잘 처리해야 하는 문제이다.
1. _asd와 같이 맨 앞에 '_'가 들어가는 경우
2. asd_와 같이 맨 뒤에 '_'가 들어가는 경우
3. asd__asd와 같이 '_'가 연속하여 2개인 경우
4. asd_Asd와 같이 C++과 Java가 섞인 형태
5. AbcDef와 같이 맨 앞이 대문자인 경우
위와 같은 에러들을 처리해주기만 하면 파이썬으로는 쉽게 풀 수 있는 문제였다.
'문제풀이 > 문자열' 카테고리의 다른 글
[Python/파이썬] 백준 14405번 피카츄 (0) | 2024.06.24 |
---|---|
[Python/파이썬] 백준 5052번 전화번호 목록 (0) | 2024.03.25 |
[Python/파이썬] 백준 19948번 음유시인 영재 (0) | 2024.03.14 |
[Python/파이썬] 백준 1251번 단어 나누기 (0) | 2024.03.08 |
[Python/파이썬] 백준 11656번 접미사 배열 (0) | 2024.01.19 |