Thursday, September 28, 2023

# Digit To Word And Vice Versa (Python)

How can we convert in a program from words like “eight” to the number 8? How can we convert the number 10 to the letters “Ten”? Here, I show you multiple ways to do it bi-directionally…from letters to digits, and digit to letters. I’ve seen several implementations and many rely on ASCII ordinals. But I decided to not rely on that because even if ASCII table changes (which won’t in my lifetime), my implementation will ALWAYS work. I also show you how to do it using Dictionary object (both directions), and Lists/Arrays (both directions).

Caveat: This blog is only going to cover zero to ten (0-10). I have code where we can actually handle hundreds and thousands, as in “881” would spit out “Eight hundred and eight one”, and so on. But that’s much more complex and I’ll reserve it for another blog.

So, let’s just focus on “zero” to “ten”, or 0 to 10. And vice versa. You’re with me?

I’ll use Python here, only because I’m tweaking with it (although I’m more familiar with C/Javascript/MFC, etc.) and it’s actually simpler than the other languages especially for those without CS background.

I’ll start with a DICTIONARY object in Python and use it to transform both ways (letters to digit, and digit to letters). Then I’ll implement the same using Lists, also both ways. Let’s go!

• # using dictionary object
# Digit->WORD. For converting a digit to a word (KEY is the one to search for using index). KEY is a digit.
dic1={0:’ZERO’, 1:’ONE’, 2:’TWO’, 3: ‘THREE’, 4: ‘FOUR’,5:’FIVE’, 6:’SIX’,7:’SEVEN’, 8:’EIGHT’, 9:’NINE’,10:’TEN’}
# WORD -> Digit. For converting a word to a digit representation (KEY is the one to search for using index). KEY is the word string.
dic2={‘ZERO’:0, ‘ONE’: 1, ‘TWO’: 2, ‘THREE’:3, ‘FOUR’: 4, ‘FIVE’:5, ‘SIX’:6, ‘SEVEN’:7, ‘EIGHT’:8, ‘NINE’:9, ‘TEN’:10}
# Word -> Digit
userinput = input(“Type a number (0 to 10) as a WORD, not a digit! (e.g.’one’) And we will transform it into a digit.\r\n “);
userinput = userinput.upper(); # make it case-insensitive

# Digit -> Word
userinput = input(“Enter a number (0 to 10) as a NUMBER. And we will transform it into a word.\r\n “);
userinput = int(userinput); # convert to type as in dic…integer

What Just Happened?

I created two dictionary objects dic1, dic2. A dictionary object is a key-value pair. Look it up (this blog is not about teaching specific syntax). A key matches specifically to a value. So, for example, in dic1, 0 matches with the word ‘ZERO’. And in dic2, ‘ZERO’ matches with 0 (the digit). So I can cover both directions…letters to a digit, and digit to letters.

Ok, next I just get the input from user using input() function. The comments preceded with “#” explains what each statement is doing.

The first part is taking in a word (string/letters) as input and outputting the corresponding number. So, if you typed “five”, the output will be: 5

The second part is taking in a number as input and outputting the corresponding english word for it. So, if you typed “3”, the output will be: THREE

Look up the dictionary object in Python if you want to know more, but it works because I simply take the input and match it with the KEY value of the dictionary object and find its associated VALUE. So, 6 is matched with “SIX” in dic1 object.

Conversely, the dic2 object has the same idea except the KEY value is the string/word. So, “ONE” is the KEY and its value is 1.

I output the answers using index for both dictionary objects. Done.

Sample output (from a Python console):

```Type a number (0 to 10) as a WORD, not a digit! (e.g.'one') And we will tranform it into a digit.
four
Enter a number (0 to 10) as a NUMBER. And we will transform it into a word.
4

>>> USING LISTS (ARRAYS) IN BIDIRECTIONAL WAYS <<<

Enter a number (0 to 10) as a NUMBER. And we will transform it into a word.
9
Enter a number (0 to 10) as a WORD (e.g. 'one'). And we will transform it into a digit.
eight

Another Way (using List object):```

This time, I use arrays (those more familiar with C/C++/Java/JavaScript) or lists in Python. I define 2 lists, one with digits and another with the english words and they’re aligned (index-wise) so an index of nums[] would be another representation of it in numsword[]. nums == numswords. Or, “ONE” would match up with the number 1 in nums[].  Here’s the code:

```nums=[0,1,2,3,4,5,6,7,8,9,10];
numswords=['ZERO','ONE','TWO','THREE', 'FOUR', 'FIVE', 'SIX','SEVEN', 'EIGHT', 'NINE', 'TEN'];

userinput = input("Enter a number (0 to 10) as a NUMBER. And we will transform it into a word.\r\n ");
userinput = int(userinput); # convert to int for index

userinput = input("Enter a number (0 to 10) as a WORD (e.g. 'one'). And we will transform it into a digit.\r\n ");
userinput = userinput.upper(); # make it case-insensitive for searching
i = numswords.index(userinput); # get the index where the input matches in array
#print(i);

So, if you type in “nine”, the output will be 9. If you type in 9, the output will be “NINE”.

Exercise:

• There’s no error checking code included here for brevity. How would you check if the entry was numeric or not in Python? Or if it’s out of the range (10/TEN)?
• How would you handle beyond 10? As in hundreds and thousands…? (there are some examples out there in the Net but don’t copy/paste…understand it and do it your way).
• Can you implement 0-10 in C/C++ or GoLang or JavaScript?

Cheers!