From 34dc4258638fa6900483a0a909da4f794d4a9fcf Mon Sep 17 00:00:00 2001 From: hack-to-fest <57044733+hack-to-fest@users.noreply.github.com> Date: Sat, 26 Oct 2019 23:12:20 +0530 Subject: [PATCH] palindrome code --- palindrom/palindrom.py | 54 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 palindrom/palindrom.py diff --git a/palindrom/palindrom.py b/palindrom/palindrom.py new file mode 100644 index 0000000..875d676 --- /dev/null +++ b/palindrom/palindrom.py @@ -0,0 +1,54 @@ +def checkPalindrome(string): + + # Returns true if str is palindrome, + # else false + length = len(string) + length -= 1 + for i in range(length): + if string[i] != string[length]: + return False + length -= 1 + return True + +def printSolution(partitions): + for i in range(len(partitions)): + for j in range(len(partitions[i])): + print(partitions[i][j], end = " ") + print() + +def addStrings(v, s, temp, index): + + # Goes through all indexes and + # recursively add remaining partitions + # if current string is palindrome. + length = len(s) + string = "" + + current = temp[:] + + if index == 0: + temp = [] + for i in range(index, length): + string += s[i] + if checkPalindrome(string): + temp.append(string) + if i + 1 < length: + addStrings(v, s, temp[:], i + 1) + else: + v.append(temp) + temp = current + +def partition(s, v): + + # Generates all palindromic partitions + # of 's' and stores the result in 'v'. + temp = [] + addStrings(v, s, temp[:], 0) + printSolution(v) + +# Driver Code +if __name__ == "__main__": + s = "geeks" + partitions = [] + partition(s, partitions) +