Possible words from Phone digits Python

PROGRAM TO PRINT ALL POSSIBLE WORDS FROM PHONE DIGITS



hashTable = ["", "", "abc", "def", "ghi", "jkl",
             "mno", "pqrs", "tuv", "wxyz"]
 
# A recursive function to print all
# possible words that can be obtained
# by input number[] of size n. The
# output words are one by one stored
# in output[]
 
 
def printWordsUtil(number, curr, output, n):
    if(curr == n):
        print(output)
        return
 
    # Try all 3 possible characters
    # for current digir in number[]
    # and recur for remaining digits
    for i in range(len(hashTable[number[curr]])):
        output.append(hashTable[number[curr]][i])
        printWordsUtil(number, curr + 1, output, n)
        output.pop()
        if(number[curr] == 0 or number[curr] == 1):
            return
 
# A wrapper over printWordsUtil().
# It creates an output array and
# calls printWordsUtil()
 
 
def printWords(number, n):
    printWordsUtil(number, 0, [], n)
 
 
# Driver function
if __name__ == '__main__':
    number = [2, 3, 4]
    n = len(number)
    printWords(number, n)


OUTPUT
adg adh adi aeg aeh aei afg afh afi bdg 
bdh bdi beg beh bei bfg bfh bfi cdg cdh 
cdi ceg ceh cei cfg cfh cfi
Process returned 0 (0x0)   execution time : 0.025 s
Press any key to continue.

Comments

Popular posts from this blog

Solve the Sudoku Python

Solve the Sudoku Java

Find Duplicates Java