Convert a Mobile Numeric Keypad sequence to equivalent sentence | Function to convert mobile numeric keypad sequence into its equivalent string ; Store the mobile keypad mappings ; Traverse the str1ing str1 ; If the current character is ' . ' , then continue to the next iteration ; Stores the number of continuous clicks ; Iterate a loop to find the count of same characters ; 2 , 3 , 4 , 5 , 6 and 8 keys will have maximum of 3 letters ; 7 and 9 keys will have maximum of 4 keys ; Handle the end condition ; Check if the current pressed key is 7 or 9 ; Else , the key pressed is either 2 , 3 , 4 , 5 , 6 or 8 ; Driver Code
def printSentence ( str1 ) : NEW_LINE INDENT nums = [ " " , " " , " ABC " , " DEF " , " GHI " , " JKL " , " MNO " , " PQRS " , " TUV " , " WXYZ " ] NEW_LINE i = 0 NEW_LINE while ( i < len ( str1 ) ) : NEW_LINE INDENT if ( str1 [ i ] == ' . ' ) : NEW_LINE INDENT i += 1 NEW_LINE continue NEW_LINE DEDENT count = 0 NEW_LINE while ( i + 1 < len ( str1 ) and str1 [ i + 1 ] and str1 [ i ] == str1 [ i + 1 ] ) : NEW_LINE INDENT if ( count == 2 and ( ( str1 [ i ] >= '2' and str1 [ i ] <= '6' ) or ( str1 [ i ] == '8' ) ) ) : NEW_LINE INDENT break NEW_LINE DEDENT elif ( count == 3 and ( str1 [ i ] == '7' or str1 [ i ] == '9' ) ) : NEW_LINE INDENT break NEW_LINE DEDENT count += 1 NEW_LINE i += 1 NEW_LINE if ( i < len ( str ) ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT if ( str1 [ i ] == '7' or str1 [ i ] == '9' ) : NEW_LINE INDENT print ( nums [ ord ( str1 [ i ] ) - 48 ] [ count % 4 ] , end = " " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( nums [ ord ( str1 [ i ] ) - 48 ] [ count % 3 ] , end = " " ) NEW_LINE DEDENT i += 1 NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str1 = "234" NEW_LINE printSentence ( str1 ) NEW_LINE DEDENT