Convert given string to a valid mobile number | Function to print valid and formatted phone number ; Length of given ; Store digits in temp ; Iterate given M ; If any digit : append it to temp ; Find new length of ; If length is not equal to 10 ; Store final result ; Make groups of 3 digits and enclose them within ( ) and separate them with " - " 0 to 2 index 1 st group ; 3 to 5 index 2 nd group ; 6 to 8 index 3 rd group ; 9 to 9 index last group ; Print final result ; Driver Code ; Given ; Function Call
def Validate ( M ) : NEW_LINE INDENT lenn = len ( M ) NEW_LINE temp = " " NEW_LINE for i in range ( lenn ) : NEW_LINE INDENT if ( M [ i ] . isdigit ( ) ) : NEW_LINE INDENT temp += M [ i ] NEW_LINE DEDENT DEDENT nwlenn = len ( temp ) NEW_LINE if ( nwlenn != 10 ) : NEW_LINE INDENT print ( " Invalid " ) NEW_LINE return NEW_LINE DEDENT res = " " NEW_LINE x = temp [ 0 : 3 ] NEW_LINE res += " ( " + x + " ) - " NEW_LINE x = temp [ 3 : 3 + 3 ] NEW_LINE res += " ( " + x + " ) - " NEW_LINE x = temp [ 6 : 3 + 6 ] NEW_LINE res += " ( " + x + " ) - " NEW_LINE x = temp [ 9 : 1 + 9 ] NEW_LINE res += " ( " + x + " ) " NEW_LINE print ( res ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT M = "91 ▁ 234rt5%34*0 ▁ 3" NEW_LINE Validate ( M ) NEW_LINE DEDENT