You Are Here:

Community: Developer Discussion Boards

#1 Old Convert numbers to characters - 2009-05-04, 17:03

Join Date: Jan 2009
Posts: 7
aneville
Offline
Registered User
Hey,

I want to write a small application to convert key presses to characters like what happens when you're writing an SMS. For this, i wrote a quick script that converts an array of numbers back to characters using a keyMap. The following is the code:

Code:
import sys
 
keyMap = {
    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']
    }

#                  h     e        y 
#keysPressed = [4, 4, 3, 3, 9, 9, 9]
#                 h     e        l        l        o
keysPressed = [4, 4, 3, 3, 5, 5, 5, 5, 5, 5, 6, 6, 6] 

prevkey = ''
counter = 0
counter2 = 0;
for key in keysPressed:
        if (prevkey == key):
                counter += 1
        else:
                if (prevkey != ''):
                        sys.stdout.write(keyMap[prevkey][counter - 3])
                counter = 0
                prevkey = key
 
print keyMap[prevkey][counter]
However, this doesn't handle double characters i.e. 555 / 555 which should be "ll" and not 5 / 55 / 555 etc = 'j', 'k', 'l'. Perhaps I should use some timeout feature (e.g if flightTime > datetime.timedelta(seconds=1))?

Thanks,

A
Reply With Quote
Reply « Previous Thread | Next Thread »
Display Modes
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules

You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump
Similar Threads
Thread Thread Starter Forum Replies Last Post
I would like to convert numbers from binary system to decimal sirmic General Symbian C++ 4 2008-03-04 12:15

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia