Sort an alphanumeric string such that the positions of alphabets and numbers remain unchanged | Function that returns the string s in sorted form such that the positions of alphabets and numeric digits remain unchanged ; String to character array ; Sort the array ; Count of alphabets and numbers ; Get the index from where the alphabets start ; Now replace the string with sorted string ; If the position was occupied by an alphabet then replace it with alphabet ; Else replace it with a number ; Return the sorted string ; Driver Code
def sort ( s ) : NEW_LINE INDENT c , s = list ( s ) , list ( s ) NEW_LINE c . sort ( ) NEW_LINE al_c = 0 NEW_LINE nu_c = 0 NEW_LINE while ord ( c [ al_c ] ) < 97 : NEW_LINE INDENT al_c += 1 NEW_LINE DEDENT for i in range ( len ( s ) ) : NEW_LINE INDENT if s [ i ] < ' a ' : NEW_LINE INDENT s [ i ] = c [ nu_c ] NEW_LINE nu_c += 1 NEW_LINE DEDENT else : NEW_LINE INDENT s [ i ] = c [ al_c ] NEW_LINE al_c += 1 NEW_LINE DEDENT DEDENT return ' ' . join ( s ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = " d4c3b2a1" NEW_LINE print ( sort ( s ) ) NEW_LINE DEDENT