212k views
2 votes
Write a function that takes a string like 'one five two' and returns the corresponding integer (in this case, 152). A function just like this would be used in a speech recognition system (e.g. automated phone taxi bookings) to convert a spoken number into an integer value. Here is an attempt that won't work. Your task is to replace the function body with something that works:

2 Answers

5 votes

Answer:

Check the explanation

Step-by-step explanation:

def words2number(s):

words = s.split()

numbers = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine']

result = ""

for word in words:

if word in numbers:

result += str(numbers.index(word))

return result

# remove below test line before submitting this code.

print(words2number('one five two'))

Kindly check the attached image below for the code output.

Write a function that takes a string like 'one five two' and returns the corresponding-example-1
User Emilio Numazaki
by
3.5k points
5 votes

Answer:

see explaination

Step-by-step explanation:

def words2number(s):

words = s.split()

numbers = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine']

result = ""

for word in words:

if word in numbers:

result += str(numbers.index(word))

return result

User Reana
by
4.1k points