Convert numeric words to numbers
Last Updated :
19 May, 2023
Given a string S, containing numeric words, the task is to convert the given string to the actual number.
Example:
Input: S = “zero four zero one”
Output: 0401
Input: S = “four zero one four”
Output: 4014
Method #1: Using loop + join() + split()
One of the ways to solve this problem is to use a map, where one can map the numeric with words and then split the strings and rejoin using mapping to numbers.
Python3
help_dict = {
'one' : '1' ,
'two' : '2' ,
'three' : '3' ,
'four' : '4' ,
'five' : '5' ,
'six' : '6' ,
'seven' : '7' ,
'eight' : '8' ,
'nine' : '9' ,
'zero' : '0'
}
test_str = "zero four zero one"
print ( "The original string is : " + test_str)
res = ''.join(help_dict[ele] for ele in test_str.split())
print ( "The string after performing replace : " + res)
|
Output
The original string is : zero four zero one
The string after performing replace : 0401
Time complexity: O(n)
Auxiliary space: O(n), where n is length of string.
Method #2: Using word2number library
This problem can also be solved using PyPI library word2number. It has inbuilt functions that convert words to numbers.
Python3
from word2number import w2n
test_str = "zero four zero one"
print ( "The original string is : " + test_str)
res = w2n.word_to_num(test_str)
print ( "The string after performing replace : " + str (res))
|
Time complexity: O(n)
Auxiliary space: O(n)
Approach 2: Using Regular Expressions (re)
We can use the re module in Python to find all the word occurrences in the string and then use a dictionary to map the words to numbers. After that, we can use the re.sub function to replace all the word occurrences with their corresponding numbers.
Python3
import re
def convert_to_numbers(s):
words_to_numbers = {
'one' : '1' ,
'two' : '2' ,
'three' : '3' ,
'four' : '4' ,
'five' : '5' ,
'six' : '6' ,
'seven' : '7' ,
'eight' : '8' ,
'nine' : '9' ,
'zero' : '0'
}
pattern = re. compile (r '\b(' + '|' .join(words_to_numbers.keys()) + r ')\b' )
return re.sub(pattern, lambda x: words_to_numbers[x.group()], s)
s = "zero four zero one"
print (convert_to_numbers(s))
|
Time complexity: O(n)
Auxiliary space: O(n)
Method 4: Using dictionary
Steps:
- Split the input string into words using the split() method, and store the resulting list of words in variable words.
- We use a list comprehension to map each word in the list words to its corresponding digit using the word_to_digit dictionary and store the resulting list of digits in variable digits.
- Join the list of digits into a string using the join() method, and store the resulting string in a variable result.
- Print the original string and the result.
Python3
word_to_digit = {
'zero' : '0' ,
'one' : '1' ,
'two' : '2' ,
'three' : '3' ,
'four' : '4' ,
'five' : '5' ,
'six' : '6' ,
'seven' : '7' ,
'eight' : '8' ,
'nine' : '9'
}
test_str = 'zero four zero one'
words = test_str.split()
digits = [word_to_digit[word] for word in words]
result = ''.join(digits)
print ( 'The original string is:' , test_str)
print ( 'The string after performing replace:' , result)
|
Output
The original string is: zero four zero one
The string after performing replace: 0401
Time complexity: O(n), where n is the length of the input string test_str, because we need to traverse the string once to split it into words and once more to map each word to its corresponding digit.
Auxiliary space: O(1), because we only need to store the dictionary, the list of words, the list of digits, and the resulting string, which do not depend on the size of the input.
Method 5: Using list comprehension
This approach uses a list comprehension to iterate through the words in the input string and convert each word to its corresponding digit using a series of if/else statements. The resulting digits are concatenated to form the output string.
Steps:
- Split the input string into individual words using list comprehension, iterate through the words in the string, and convert each word to its corresponding digit using if/else statements.
- Concatenate the resulting digits to form the output string.
- Print the output string.
Python3
original_string = 'zero four zero one'
result = ' '.join([' 0 ' if word == ' zero ' else ' 1 ' if word == ' one ' else ' 2 ' if word == ' two ' else ' 3 ' if word == ' three ' else ' 4 ' if word == ' four ' else ' 5 ' if word == ' five ' else ' 6 ' if word == ' six ' else ' 7 ' if word == ' seven ' else ' 8 ' if word == ' eight ' else ' 9 ' for word in original_string.split()])
print (result)
|
Time complexity: O(n), where n is the number of words in the input string.
Auxiliary Space: O(n), where n is the number of words in the input string.
Method 6: Using a list of tuples
Python3
word_digit_pairs = [
( 'zero' , '0' ),
( 'one' , '1' ),
( 'two' , '2' ),
( 'three' , '3' ),
( 'four' , '4' ),
( 'five' , '5' ),
( 'six' , '6' ),
( 'seven' , '7' ),
( 'eight' , '8' ),
( 'nine' , '9' )
]
test_str = 'zero four zero one'
for word, digit in word_digit_pairs:
test_str = test_str.replace(word, digit)
print ( 'The original string is:' , test_str)
|
Output
The original string is: 0 4 0 1
Time complexity: O(K) where K is the length of the input string and the number of word-digit pairs in the list.
Auxiliary space: O(1) since we are not using any additional data structures that scale with the input size.
Please Login to comment...