The T9 typing predictor helps with suggestions for possible word combinations on an old-style numeric keypad phone. Each digit in the keypad (2-9) represents a group of 3-4 letters. To type a letter, press once the key which corresponds to the letter group that contains the required letter. Typing words is done by typing letters of the word in sequence.

The letter groups and corresponding digits are as follows:
```
 -----------------
|  1  |  2  |  3  |
|     | ABC | DEF |
|-----|-----|-----|
|  4  |  5  |  6  |
| GHI | JKL | MNO |
|-----|-----|-----|
|  7  |  8  |  9  |
| PQRS| TUV | WXYZ|
 -----------------
```

The prediction algorithm tries to match the input sequence against a predefined dictionary of words. The combinations which appear in the dictionary are considered valid words and are shown as suggestions.

Given a list of words as a reference dictionary, and a non-empty string (of digits 2-9) as input, complete the function which returns suggestions based on the string of digits, which are found in the reference dictionary.

For example:
```python
T9(['hello', 'world'], '43556') returns ['hello']
T9(['good', 'home', 'new'], '4663') returns ['good', 'home']
```

Note that the dictionary must be case-insensitive (`'hello'` and `'Hello'` are same entries). The list returned must contain the word as it appears in the dictionary (along with the case).

Example:
```python
T9(['Hello', 'world'], '43556') returns ['Hello']
```

If there is no prediction available from the given dictionary, then return the string containing first letters of the letter groups, which correspond to the input digits.

For example:
```python
T9([], '43556') returns ['gdjjm']
T9(['gold', 'word'], '4663') returns ['gmmd']
```

Difficulty: introductory

Starter code:

def T9(words, seq):
	

Solutions:
FROM   = "abc def ghi jkl mno pqrs tuv wxyz".split()
TO_NUM = "222 333 444 555 666 7777 888 9999".split()

TABLE_TO_NUM  = str.maketrans( *map(''.join, (FROM, TO_NUM)) )
TABLE_TO_CHAR = str.maketrans( *map(lambda lst: ''.join(x[0] for x in lst), (TO_NUM, FROM)))


def T9(words, seq):
    return ( [w for w in words if seq == w.lower().translate(TABLE_TO_NUM)]
                or [seq.translate(TABLE_TO_CHAR)] )

CHR_TO_NUM = str.maketrans("abcdefghijklmnopqrstuvwxyz", "22233344455566677778889999")
NUM_TO_CHR = str.maketrans("23456789", "adgjmptw")

def T9(words, seq):
    return [word for word in words if word.lower().translate(CHR_TO_NUM) == seq] \
            or [seq.translate(NUM_TO_CHR)]

D = {c:i for c, i in zip('abcdefghijklmnopqrstuvwxyz', '22233344455566677778889999')}

def T9(words, seq):
    r = [w for w in words if ''.join(D[c] for c in w.lower()) == seq]
    return r if r else [''.join({i:c for i, c in zip('23456789', 'adgjmptw')}[i] for i in seq)]

def T9(words,seq):
    ans=[]
    for i in words:
        if i.lower().translate(str.maketrans('abcdefghijklmnopqrstuvwxyz','22233344455566677778889999')) == seq:
            ans += [i]
    return ans if ans else [seq.translate(str.maketrans('23456789','adgjmptw'))]


d = dict(zip('123456789',[' ']+'ABC DEF GHI JKL MNO PQRS TUV WXYZ'.split()))          
T9=lambda w,p:[i for i in w if all(k in d[l] for k,l in zip(i.upper(),p))]or[''.join(d[i][0] for i in p).lower()]

def T9(words, seq):
    X, res = ['', '', 'abc', 'def', 'ghi', 'jkl', 'mno', 'pqrs', 'tuv', 'wxyz'], ""
    for i,c in enumerate(map(int, seq)):
        words = [w for w in words if w[i].lower() in X[c]]
        res += X[c][0]
    return words or [res]

layout = ['', '', 'abc', 'def', 'ghi', 'jkl', 'mno', 'pqrs', 'tuv', 'wxyz']
letters = {c: str(n) for n, cs in enumerate(layout) for c in cs}

def T9(words, seq):
    return ([word for word in words if ''.join(letters[c] for c in word.lower()) == seq]
            or [''.join(layout[i][0] for i in map(int, seq))])

def T9(words,seq):
    result = []
    seq = list(seq)
    numToword(int(seq[0]))
    for word in words:
        if (isWord(word,seq)):
            result.append(word)
    if(len(result)):
        return result
    red = ""
    for i in seq:
        red += numToword(int(i)-1)[0]
    result.append(red.lower())
    return result
        
def numToword(num):
    return ([" ","ABC","DEF","GHI","JKL","MNO","PQRS","TUV","WXYZ",""])[num]
# Return true if we can write this word with this leters
def isWord(word,num):
    if(len(word) != len(num)):
        return False
    for i_n in range(len(word)):
        if (word[i_n].upper() in numToword(int(num[i_n])-1))==False:
            return False
    return True

def T9(words, seq):
    d = {}
    sym = list(map(chr, range(97,123)))
    for x in range(2, 10):
        q = sym[:(3, 4)[x in(7, 9)]]
        sym = sym[(3, 4)[x in(7, 9)]:]
        d[str(x)] = q
    s = zip(words,map(iter, [x.lower()for x in words]))
    return [w for w, i in s if all(next(i)in d[x] for x in seq)] \
           or [''.join(d[x][0] for x in seq)]

mapping = {'2': "abc", 
           '3': "def", 
           '4': "ghi",
           '5': "jkl",
           '6': "mno",
           '7': "pqrs",
           '8': "tuv",
           '9': "wxyz"}

def T9(words, seq):
    return [word for word in words if len(word) >= len(seq) and all(word[i].lower() in mapping[n] for i, n in enumerate(seq))] or ["".join(mapping[i][0] for i in seq)]