Python

python isPalindrome 함수

bitcoder 2022. 6. 2. 09:02
728x90

회문(回文) 또는 팰린드롬(palindrome)은 거꾸로 읽어도 제대로 읽는 것과 같은 문장이나 낱말, 숫자, 문자열(sequence of characters)을 말합니다.

 

파이썬프로그래밍언어를 이용하여 회문을 검사하는 파이썬 함수를 소개합니다.

 

다음은 소스코드입니다.

def isPalindrome(word):
  for i in range(0, int(len(word)/2)):
    if word[i] != word[len(word)-1 - i]:
      return False
  return True

print(isPalindrome("기러기"))
print(isPalindrome("소주 주소"))
print(isPalindrome("토마토"))
print(isPalindrome("eye"))
print(isPalindrome("level"))
print(isPalindrome("바나나"))
print(isPalindrome("회문"))
print(isPalindrome("palindrome"))

 

다음은 소스코드의 실행결과입니다.

>python main.py
True
True
True
True
True
False
False
False

 

이상으로 회문(回文)을 검사하는 파이썬 함수를 알아보았습니다.

728x90