Group consecutive characters of same type in a string | Function to return the modified string ; Store original string ; Remove all white spaces ; To store the resultant string ; Traverse the string ; Group upper case characters ; Group numeric characters ; Group arithmetic operators ; Return the resultant string ; Driver code
def groupCharacters ( s , l ) : NEW_LINE INDENT temp = " " NEW_LINE for i in range ( l ) : NEW_LINE INDENT if ( s [ i ] != ' ▁ ' ) : NEW_LINE INDENT temp = temp + s [ i ] NEW_LINE DEDENT DEDENT l = len ( temp ) NEW_LINE ans = " " NEW_LINE i = 0 NEW_LINE while ( i < l ) : NEW_LINE INDENT if ( ord ( temp [ i ] ) >= ord ( ' A ' ) and ord ( temp [ i ] ) <= ord ( ' Z ' ) ) : NEW_LINE INDENT while ( i < l and ord ( temp [ i ] ) >= ord ( ' A ' ) and ord ( temp [ i ] ) <= ord ( ' Z ' ) ) : NEW_LINE INDENT ans = ans + temp [ i ] NEW_LINE i += 1 NEW_LINE DEDENT ans = ans + " ▁ " NEW_LINE DEDENT elif ( ord ( temp [ i ] ) >= ord ( '0' ) and ord ( temp [ i ] ) <= ord ( '9' ) ) : NEW_LINE INDENT while ( i < l and ord ( temp [ i ] ) >= ord ( '0' ) and ord ( temp [ i ] ) <= ord ( '9' ) ) : NEW_LINE INDENT ans = ans + temp [ i ] NEW_LINE i += 1 NEW_LINE DEDENT ans = ans + " ▁ " NEW_LINE DEDENT else : NEW_LINE INDENT while ( i < l and ord ( temp [ i ] ) >= ord ( ' * ' ) and ord ( temp [ i ] ) <= ord ( ' / ' ) ) : NEW_LINE INDENT ans = ans + temp [ i ] NEW_LINE i += 1 NEW_LINE DEDENT ans = ans + " ▁ " NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = "34FTG234 + ▁ + - ▁ * " NEW_LINE l = len ( s ) NEW_LINE print ( groupCharacters ( s , l ) ) NEW_LINE DEDENT