0

I want to create a function that takes an integer (say 234) and returns it as letters (cde).

I have managed to form some code that takes the number and separates it into its numeric components

def toLetter(n):
    x = str(n)
    for elem in x:
        print(elem)
    d = {0 : 'a', 1 : 'b', 2 : 'c', 3 : 'd', 4 : 'e', 5 : 'f', 6 : 'g', 7 : 'h', 8 : 'i', 9 : 'j'}
    for n in x:
        d[n]

toLetter(234)

But I am really struggling with;

  1. how to map the dictionary onto the number and
  2. get it to return it as:
cde 

rather than

c
d
e

Any help would be greatly appreciated. I am new so this may be trivial but I have come here as last resort.

001
  • 13,291
  • 5
  • 35
  • 66
constar99
  • 25
  • 6
  • 1
    Does this answer your question? [How to print without a newline or space](https://stackoverflow.com/questions/493386/how-to-print-without-a-newline-or-space) – Mr. T Feb 18 '22 at 14:13

2 Answers2

1

You should use strings as keys in your dictionary.

A simple way, use a generator/comprehension:

def toLetter(num):
    d = dict(zip('0123456789', 'abcdefghij'))
    return ''.join(d[i] for i in str(num))

Even better, use a translation table:

def toLetter(num):
    t = str.maketrans('0123456789', 'abcdefghij')
    return str(num).translate(t)

example:

toLetter(123)
# 'bcd'
mozway
  • 194,879
  • 13
  • 39
  • 75
0

So, in order to select the elements in the dictionary you have to convert the digits back to integers. Also, to create the final answer I would simply append each letter to a string and print the final string:

def toLetter(n):
    d = {0 : 'a', 1 : 'b', 2 : 'c', 3 : 'd', 4 : 'e', 5 : 'f', 6 : 'g', 7 : 'h', 8 : 'i', 9 : 'j'}
    
    x = str(n)
    result = ''
    
    for elem in x:
        result = result + d[int(elem)]
    
    print(result)

toLetter(234)

Output:

cde
JANO
  • 2,995
  • 2
  • 14
  • 29
  • @constar99 it is however a bit wasteful to perform the double conversion… directly use strings as keys, also repeated string concatenation is not recommended – mozway Feb 18 '22 at 14:32
  • @mozway Of course you are right, but I think in this case an understandable and readable solution is more helpful than the most efficient one. – JANO Feb 18 '22 at 14:38
  • I disagree here, it is never too early to learn good practices, this is precisely the time to learn those things when you're a beginner ;) – mozway Feb 18 '22 at 14:40
  • Regarding the complexity and taking my example of the translation table, how is this more complicated and less understandable that requiring the concept of dictionary, keys, types (here), loop, concatenation… ;) When you learn the right way early, this becomes the simple/understandable way – mozway Feb 18 '22 at 14:42
  • I agree with you that it is good to learn these practices early. Also, I have to correct myself and say that I find your solutions readable and understandable. I tried to provide a solution that is similar to the code in the question. The new thing was the `int()` conversion as the question was how to map the number and the string concatenation. Your answer introduced `zip()`, `join()`, and a generator. My only point is that it can get overwhelming really fast. – JANO Feb 18 '22 at 15:01