Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Old Phone Text Messaging

My code is

def search(n):
    lis = []
    pad = {1: ['.', ',''?', '!', ':'], 2: ['A', 'B', 'C'], 3: ['D', 'E', 'F'], 4: ['G', 'H', 'I'], 5: ['J', 'K', 'L']
        , 6: ['M', 'N', 'O'], 7: ['P', 'Q', 'R', 'S'], 8: ['T', 'U', 'V'], 9: ['W', 'X', 'Y', 'Z'], 0: ' '}
    for i in n:
        for key,values in pad.items():
            if i.upper() in values:
                lis.append(key)
    return lis


def main():
     m = input("Enter String")
     print(search(m))
main()

my output is

Enter String - hello world
[4, 3, 5, 5, 6, 0, 9, 6, 7, 5, 3]

It gives output as the single key only. However I want output as [44,33,555,555,666,9,666,777,555,3]

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

This is because you after you find the key, you only append the key once.

You will need to add the logic to duplicate the key string several times.

I only added one line and modified one line of your original code. Hope this helps.

def search(n):
    lis = []
    pad = {1: ['.', ',''?', '!', ':'], 2: ['A', 'B', 'C'], 3: ['D', 'E', 'F'], 4: ['G', 'H', 'I'], 5: ['J', 'K', 'L']
        , 6: ['M', 'N', 'O'], 7: ['P', 'Q', 'R', 'S'], 8: ['T', 'U', 'V'], 9: ['W', 'X', 'Y', 'Z'], 0: ' '}
    for i in n:
        for key, values in pad.items():
            if i.upper() in values:
                temp = str(key) * (values.index(i.upper()) + 1)  // added
                lis.append(temp)   // modified
    return lis


def main():
     m = input("Enter String")
     print(search(m))
main()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading