Python challenge_11

Palindrome

level of challenge 4/10

A string is a palindrome when it is the same
when read backwards.

For example:
the string “bob” is a palindrome So is “abba”.
But the string “abcd” is not a palindrome, because “abcd” != “dcba”…



Palindrome



level of challenge 4/10

A string is a palindrome when it is the same
when read backwards.

For example:
the string “bob” is a palindrome So is “abba”.
But the string “abcd” is not a palindrome, because “abcd” != “dcba”.

Write a function named palindrome that takes a single string as its parameter.
Your function should return True if the string is a palindrome, and False otherwise.



My solution

def palindrome(string):
    pal_string = list( string.casefold() )
    rev_string = list( reversed(pal_string) )

    if pal_string == rev_string:
        return True
    else:
        return False

print(palindrome("aba"))



Another solution

def palindrome(string):
    while len(string) > 1:
      head = string[0]
      tail = string[-1]
      string = string[1:-1]
      if head != tail:
        return False
    return True
def palindrome(string):
  if len(string) < 2:
    return True
  return string[0] == string[-1] and palindrome(string[1:-1])
def palindrome(string):
  return string == string[::-1]

I hope this challenge improve python skills 🧐😎


Print Share Comment Cite Upload Translate
APA
Mahmoud EL-kariouny | Sciencx (2024-03-29T12:12:48+00:00) » Python challenge_11. Retrieved from https://www.scien.cx/2021/11/22/python-challenge_11/.
MLA
" » Python challenge_11." Mahmoud EL-kariouny | Sciencx - Monday November 22, 2021, https://www.scien.cx/2021/11/22/python-challenge_11/
HARVARD
Mahmoud EL-kariouny | Sciencx Monday November 22, 2021 » Python challenge_11., viewed 2024-03-29T12:12:48+00:00,<https://www.scien.cx/2021/11/22/python-challenge_11/>
VANCOUVER
Mahmoud EL-kariouny | Sciencx - » Python challenge_11. [Internet]. [Accessed 2024-03-29T12:12:48+00:00]. Available from: https://www.scien.cx/2021/11/22/python-challenge_11/
CHICAGO
" » Python challenge_11." Mahmoud EL-kariouny | Sciencx - Accessed 2024-03-29T12:12:48+00:00. https://www.scien.cx/2021/11/22/python-challenge_11/
IEEE
" » Python challenge_11." Mahmoud EL-kariouny | Sciencx [Online]. Available: https://www.scien.cx/2021/11/22/python-challenge_11/. [Accessed: 2024-03-29T12:12:48+00:00]
rf:citation
» Python challenge_11 | Mahmoud EL-kariouny | Sciencx | https://www.scien.cx/2021/11/22/python-challenge_11/ | 2024-03-29T12:12:48+00:00
https://github.com/addpipe/simple-recorderjs-demo