Wednesday, December 6, 2023
HomeSoftware EngineeringHow one can Verify if String is a Palindrome in Python

How one can Verify if String is a Palindrome in Python

The problem

Write a perform that checks if a given string (case insensitive) is a palindrome.

The answer in Python

Choice 1:

def is_palindrome(s):
    s = s.decrease()
    for i, merchandise in enumerate(s):
        if i<len(s)/2:
            if s[i]!=s[len(s)-i-1]:
                return False
    return True

Choice 2:

def is_palindrome(s):
    s = s.decrease()
    return s == s[::-1]

Choice 3:

def is_palindrome(s):
    return s.decrease()==s[::-1].decrease()

Check circumstances to validate our resolution

@take a look at.describe('pattern exams')
def sample_tests():
    take a look at.assert_equals(is_palindrome('a'), True)
    take a look at.assert_equals(is_palindrome('aba'), True)
    take a look at.assert_equals(is_palindrome('Abba'), True)
    take a look at.assert_equals(is_palindrome('malam'), True)
    take a look at.assert_equals(is_palindrome('walter'), False)
    take a look at.assert_equals(is_palindrome('kodok'), True)
    take a look at.assert_equals(is_palindrome('Kasue'), False)


Please enter your comment!
Please enter your name here

Most Popular

Recent Comments