코딩 테스트/COS PRO Python 2급
[COS PRO Python 기출 - 1차(한줄)] 8. 팰린드롬 판단하기
알밤바
2022. 7. 14. 10:34
728x90
반응형
goorm
구름은 클라우드 기술을 이용하여 누구나 코딩을 배우고, 실력을 평가하고, 소프트웨어를 개발할 수 있는 클라우드 소프트웨어 생태계입니다.
goorm.co
한 줄 수정 전
def solution(sentence):
str = ''
for c in sentence:
if c != '.' or c != ' ':
str += c
size = len(str)
for i in range(size // 2):
if str[i] != str[size - 1 - i]:
return False
return True
수정 후 코드
def solution(sentence):
str = ''
for c in sentence:
if c != '.' and c != ' ': # 마침표와 공백이 둘 다 아니어야 함
str += c
size = len(str)
for i in range(size // 2):
if str[i] != str[size - 1 - i]:
return False
return True
sentence1 = "never odd or even."
ret1 = solution(sentence1)
print("solution 함수의 반환 값은", ret1, "입니다.")
sentence2 = "palindrome"
ret2 = solution(sentence2)
print("solution 함수의 반환 값은", ret2, "입니다.")
if c != '.' and c != ' ':
▶ sentence에 있는 문자열 중 공백(" ")과 마침표(".") 모두 아닌 문자가 들어가야 하기 때문에 or이 아니라 and임
or일 경우, 둘 중 하나만 만족해도 되기에 문자 뿐만 아니라 다른 것들도 추가될 것임.
728x90
반응형