python - Converting a string of numbers to correpsonding letters -
for project working on, need able convert string of numbers letters form correct words. example, need convert 2626 bfbf, how make sure not turn zz instead or vice versa.
i extremely confused because cannot think of way make possible, , need program convert numbers original letters.
any appreciated, since not sure anymore…
thanks you!
edit: seems though impossible without using list of numbers, need find way replace strings lists. problem making public key cryptography encryptor/decryptor , not know how math on numbers without them being string. (it wouldn't work list, example if had square number, 1212, can 1212^2. if list [12,1,2], not know how square same answer 1212^2.)
any great, again!
using chr() can convert int letter, ex. print chr(97)
prints a
. if can use symbol identify groups, maybe '-', between numbers, simple problem. if not run ambiguous mappings mentioned.
for example create number letter dictionary:
alphabet_dict = {} abet in range(1, 27): alphabet_dict[abet] = chr(96+abet) print alphabet_dict
prints:
{1: 'a', 2: 'b', 3: 'c', 4: 'd', 5: 'e', 6: 'f', 7: 'g', 8: 'h', 9: 'i', 10: 'j', 11: 'k', 12: 'l', 13: 'm', 14: 'n', 15: 'o', 16: 'p', 17: 'q', 18: 'r', 19: 's', 20: 't', 21: 'u', 22: 'v', 23: 'w', 24: 'x', 25: 'y', 26: 'z'}
now if decide can use symbol delimiter, can use built in split() break our string list , map values (numbers) alphabet using dictionary above:
s = '2-6-2-6' def convert_num_to_alpha(num_string): num_list = num_string.split('-') abet_string = '' num in num_list: abet_string = abet_string+alphabet_dict[int(num)] return abet_string print convert_num_to_alpha(s) # prints: bfbf print convert_num_to_alpha('26-26') # prints: zz
edit: how convert string of number-number (e.g. '2-6-2-6') int:
this hackish if need arithmetic on number, have stored string can like:
print int('2-6-2-6'.replace('-', '')) # prints: 2626
Comments
Post a Comment