Modify a sentence by reversing order of occurrences of all Palindrome Words | Function to check if a string S is a palindrome ; Function to print the modified string after reversing teh order of occurrences of all palindromic words in the sentence ; Stores the palindromic words ; Stores the words in the list ; Traversing the list ; If current word is a palindrome ; Update newlist ; Reverse the newlist ; Traverse the list ; If current word is a palindrome ; Update lis [ i ] ; Increment j ; Print the updated sentence ; Driver Code
def palindrome ( string ) : NEW_LINE INDENT if ( string == string [ : : - 1 ] ) : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT def printReverse ( sentence ) : NEW_LINE INDENT newlist = [ ] NEW_LINE lis = list ( sentence . split ( ) ) NEW_LINE for i in lis : NEW_LINE INDENT if ( palindrome ( i ) ) : NEW_LINE INDENT newlist . append ( i ) NEW_LINE DEDENT DEDENT newlist . reverse ( ) NEW_LINE j = 0 NEW_LINE for i in range ( len ( lis ) ) : NEW_LINE INDENT if ( palindrome ( lis [ i ] ) ) : NEW_LINE INDENT lis [ i ] = newlist [ j ] NEW_LINE j = j + 1 NEW_LINE DEDENT DEDENT for i in lis : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE DEDENT DEDENT sentence = " mom ▁ and ▁ dad ▁ went ▁ to ▁ eye ▁ hospital " NEW_LINE printReverse ( sentence ) NEW_LINE