Add Palindrome Check Algorithm in Python (Idiomatic Python) (#96)

pull/98/head
Goutham Krishna 2021-03-04 00:25:42 +05:30 committed by GitHub
parent 327a2ad594
commit 6fe5b94526
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 0 deletions

View File

@ -22,3 +22,7 @@
2. [All subsequences](java/sequence.java)
3. [KMP String Searching](java/kmp.cpp)
4. [Rabin Karp String Searching](java/rabin-karp.cpp)
### Python
1. [Palindrome Check](python/palindrome.py)

View File

@ -0,0 +1,34 @@
#!/usr/bin/python3
# Palindrome Check Function on Python 3
# The Palindrome Algorithm
# this takes in a string and returns a boolean equal to the result of
# whether the program is a palindrome or not.
def palindrome(s: str) -> bool:
# Reverse string using idiomatic python
reversed_string = s[::-1]
# return the answer, by comparing string and its reverse
return s == reversed_string
# A utility function to output the result of palindromes
def is_palindrome(s: str):
# if string is palindrome
if palindrome(s):
print(f"{s} is a palindrome")
else:
print(f"{s} is not a palindrome")
# main program
if __name__ == "__main__":
# string 1
s1 = "abba"
# string 2
s2 = "abbcccbba"
# string 3
s3 = "abbccbbba"
# call is_palindrome (internally calls palindrome) for each string
is_palindrome(s1)
is_palindrome(s2)
is_palindrome(s3)