0

I need to convert a string of numbers into a string of letters to form words Example: if the input to your program is 1920012532082114071825463219200125320615151209190846 it should return stay hungry. stay foolish.

I have a program that converts a word to a string of numbers:

string = raw_input('Please enter your string of lowercase characters: ')

Dictionary = {'a':'00', 'b':'01', 'c':'02', 'd':'03', 'e':'04', 'f':'05',
              'g':'06', 'h':'07', 'i':'08', 'j':'09', 'k':'10', 'l':'11',
              'm':'12', 'n':'13', 'o':'14', 'p':'15', 'q':'16', 'r':'17',
              's':'18', 't':'19', 'u':'20', 'v':'21', 'w':'22', 'x':'23',
              'y':'24', 'z':'25', ' ':'32', '.':'46'}

n = ''

p= 0

for character in string:
    n += Dictionary[string[p]]
    p += 1

print n

So i need to make a program that takes the string of numbers (n) and convert it back to the original words.

Really not sure what to do here so any help would be appreciated!

3
  • 1
    SO is not a "do my homework for me" site.
    – millimoose
    Feb 14, 2012 at 23:25
  • Your dictionary is wrong. a should be 01 and so on. Otherwise that string will say tubz i etc... Feb 14, 2012 at 23:28
  • 2
    whats with the downvotes ? no where in the question it says "do it for me" he clearly asks for any kind of help
    – yurib
    Feb 14, 2012 at 23:33

2 Answers 2

0

create a dictionary with keys and values reversed:

revdict = dict([v,k] for k,v in Dictionary.items())

scan the input two characters at a time and convert using the reversed dictionary. keep in mind that you can use indices to access different parts of a string:

"12345"[0:2] # returns "12"
0

First of all, you shouldn't need to access the characters by index, the character variable defined in your loop actually stores each character as it moves forward:

for character in string:
    n += Dictionary[character]

Then you wouldn't need p either.

Now, on to your question:

The ord() method gets the ascii representation of a given character. Look up an ascii table on Google to get an idea of this.

Then, as has been suggested before, all you need to do is iterate through the string two characters at a time (here it would be useful to iterate by index), convert those values to integer and then convert that integer to a character.

I was going to post a full algorithm. But I then realised that this may be homework. Either way what I've given you here should be enough.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.