0

I'm trying to use the following code to convert a denary string into characters, but I'm having trouble converting them correctly.

# Set to true if testing. Will show lists that show the process.
testing = True

# Asks for the denary.
text = list(input('Enter the denary: '))

# Defining the list. 0 is there to allow the remover to work correctly.
doublesList = ['0']

# Split the numbers into doubles and append them to doublesList.
for i in range(len(text) - 1):
    current_item = text[i]
    next_item = text[i + 1]
    doubles = current_item + next_item
    doublesList.append(doubles)

if testing:
    print('\nunedited:\n', doublesList,'\n')

# Remove unnecessary numbers.
for item in doublesList:
    doublesList.remove(item)

if testing:
    print('edited:\n', doublesList,'\n')

# Replace the numbers with their letters.

for item in doublesList:
    if item == '01':
        doublesList[doublesList.index('01')] = 'a'
    elif item == '02':
        doublesList[doublesList.index('02')] = 'b'
    elif item == '03':
        doublesList[doublesList.index('03')] = 'c'
    elif item == '04':
        doublesList[doublesList.index('04')] = 'd'
    elif item == '05':
        doublesList[doublesList.index('05')] = 'e'
    elif item == '06':
        doublesList[doublesList.index('06')] = 'f'
    elif item == '07':
        doublesList[doublesList.index('07')] = 'g'
    elif item == '08':
        doublesList[doublesList.index('08')] = 'h'
    elif item == '09':
        doublesList[doublesList.index('09')] = 'i'
    elif item == '10':
        doublesList[doublesList.index('10')] = 'j'
    elif item == '11':
        doublesList[doublesList.index('11')] = 'k'
    elif item == '12':
        doublesList[doublesList.index('12')] = 'l'
    elif item == '13':
        doublesList[doublesList.index('13')] = 'm'
    elif item == '14':
        doublesList[doublesList.index('14')] = 'n'
    elif item == '15':
        doublesList[doublesList.index('15')] = 'o'
    elif item == '16':
        doublesList[doublesList.index('16')] = 'p'
    elif item == '17':
        doublesList[doublesList.index('17')] = 'q'
    elif item == '18':
        doublesList[doublesList.index('18')] = 'r'
    elif item == '19':
        doublesList[doublesList.index('19')] = 's'
    elif item == '20':
        doublesList[doublesList.index('20')] = 't'
    elif item == '21':
        doublesList[doublesList.index('21')] = 'u'
    elif item == '22':
        doublesList[doublesList.index('22')] = 'v'
    elif item == '23':
        doublesList[doublesList.index('23')] = 'w'
    elif item == '24':
        doublesList[doublesList.index('24')] = 'x'
    elif item == '25':
        doublesList[doublesList.index('25')] = 'y'
    elif item == '26':
        doublesList[doublesList.index('26')] = 'z'
    elif item == '27':
        doublesList[doublesList.index('27')] = ' '
    elif item == '28':
        doublesList[doublesList.index('28')] = '.'
    elif item == '29':
        doublesList[doublesList.index('29')] = ''
    elif item == '30':
        doublesList[doublesList.index('30')] = '\n'

# Print the finished list as a string.
print(''.join(doublesList))

Using the above code, entering the denary number "2908051212152830132527140113052709192702150228" should return "hello.\nmy name is bob.". However, as you can see, it returns "hello.my nam\ne is bob.":

Enter the denary: 2908051212152830132527140113052709192702150228

unedited:
 ['0', '29', '90', '08', '80', '05', '51', '12', '21', '12', '21', '15', '52', '28', '83', '30', '01', '13', '32', '25', '52', '27', '71', '14', '40', '01', '11', '13', '30', '05', '52', '27', '70', '09', '91', '19', '92', '27', '70', '02', '21', '15', '50', '02', '22', '28'] 

edited:
 ['29', '08', '05', '12', '12', '15', '28', '13', '25', '27', '14', '01', '13', '30', '05', '27', '09', '19', '27', '02', '15', '02', '28'] 

hello.my nam
e is bob.

Could anyone suggest a possible fix for this? Thanks. Apologies for the large amount of code.

This isn't a duplicate...

2 Answers2

1

Loop over the input in pairs; you can use a map string to convert to characters from there

characters = ' abcdefghijklmnopqrstuvwxyz . \n'

result = []

for pair in zip(text[::2], text[1::2]):
    codepoint = int(''.join(pair))
    if not (1 <= codepoint <= 30) or codepoint == 29: continue
    result.append(characters[codepoint])
print(''.join(result))

Demo:

>>> text = '2908051212152830132527140113052709192702150228'
>>> characters = ' abcdefghijklmnopqrstuvwxyz . \n'
>>> result = []
>>> for pair in zip(text[::2], text[1::2]):
...     codepoint = int(''.join(pair))
...     if not (1 <= codepoint <= 30) or codepoint == 29: continue
...     result.append(characters[codepoint])
... 
>>> result
['h', 'e', 'l', 'l', 'o', '.', '\n', 'm', 'y', ' ', 'n', 'a', 'm', 'e', ' ', 'i', 's', ' ', 'b', 'o', 'b', '.']
>>> print(''.join(result))
hello.
my name is bob.
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Martijn's solution is correct, as usual for his answers. That said, this whole design is weird - is there a good reason that you're using your own custom set of codepoint mappings rather than using an existing standard? ASCII or any superset could easily support your target character set, and would work better with standard libraries. – Peter DeGlopper Nov 30 '13 at 09:54
0

Your big problem is actually in the removing duplicates part. The remove method actually removes the first appearance of a value, and since the value "30" appears more than once, you accidentally remove it from the right place.

Instead of iterating over the values:

# Remove unnecessary numbers.
for item in doublesList:
    doublesList.remove(item)

You should just access every second value (the following line assumes you keep "0" in the beginning:

doublesList = doublesList[1::2]

However, it is more recommended just to split the list directly to pairs, and not having to deal with the "middle pairs".

tmrlvi
  • 2,235
  • 17
  • 35